123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.97
- */
- import { proxyCustomElement, HTMLElement, createEvent, h, Host, Build } from '@stencil/core/internal/client/index.js';
- import { c as getElementDir } from './dom.js';
- import { c as connectLabel, d as disconnectLabel } from './label2.js';
- import { a as afterConnectDefaultValueSet, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
- import { u as updateHostInteraction } from './interactive.js';
- const radioGroupCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host{display:flex;background-color:var(--calcite-ui-foreground-1);inline-size:-moz-fit-content;inline-size:fit-content;outline:1px solid var(--calcite-ui-border-input);outline-offset:-1px}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([layout=vertical]){flex-direction:column;align-items:flex-start;align-self:flex-start}:host([width=full]){inline-size:100%;min-inline-size:-moz-fit-content;min-inline-size:fit-content}:host([width=full]) ::slotted(calcite-radio-group-item){flex:1 1 auto}:host([width=full][layout=vertical]) ::slotted(calcite-radio-group-item){justify-content:flex-start}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
- const RadioGroup = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
- constructor() {
- super();
- this.__registerHost();
- this.__attachShadow();
- this.calciteRadioGroupChange = createEvent(this, "calciteRadioGroupChange", 6);
- //--------------------------------------------------------------------------
- //
- // Properties
- //
- //--------------------------------------------------------------------------
- /** Specifies the appearance style of the component. */
- this.appearance = "solid";
- /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
- this.disabled = false;
- /**
- * When `true`, the component must have a value in order for the form to submit.
- *
- * @internal
- */
- this.required = false;
- /** Defines the layout of the component. */
- this.layout = "horizontal";
- /** Specifies the size of the component. */
- this.scale = "m";
- /** The component's `selectedItem` value. */
- this.value = null;
- /** Specifies the width of the component. */
- this.width = "auto";
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- this.handleClick = (event) => {
- if (event.target.localName === "calcite-radio-group-item") {
- this.selectItem(event.target, true);
- }
- };
- }
- valueHandler(value) {
- const items = this.getItems();
- items.forEach((item) => (item.checked = item.value === value));
- }
- handleSelectedItemChange(newItem, oldItem) {
- this.value = newItem === null || newItem === void 0 ? void 0 : newItem.value;
- if (newItem === oldItem) {
- return;
- }
- const items = this.getItems();
- const match = Array.from(items)
- .filter((item) => item === newItem)
- .pop();
- if (match) {
- this.selectItem(match);
- }
- else if (items[0]) {
- items[0].tabIndex = 0;
- }
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- componentWillLoad() {
- const items = this.getItems();
- const lastChecked = Array.from(items)
- .filter((item) => item.checked)
- .pop();
- if (lastChecked) {
- this.selectItem(lastChecked);
- }
- else if (items[0]) {
- items[0].tabIndex = 0;
- }
- }
- componentDidLoad() {
- afterConnectDefaultValueSet(this, this.value);
- }
- connectedCallback() {
- connectLabel(this);
- connectForm(this);
- }
- disconnectedCallback() {
- disconnectLabel(this);
- disconnectForm(this);
- }
- componentDidRender() {
- updateHostInteraction(this);
- }
- render() {
- return (h(Host, { onClick: this.handleClick, role: "radiogroup" }, h("slot", null), h(HiddenFormInputSlot, { component: this })));
- }
- handleSelected(event) {
- event.preventDefault();
- this.selectItem(event.target);
- event.stopPropagation();
- }
- handleKeyDown(event) {
- const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
- const { key } = event;
- const { el, selectedItem } = this;
- if (keys.indexOf(key) === -1) {
- return;
- }
- let adjustedKey = key;
- if (getElementDir(el) === "rtl") {
- if (key === "ArrowRight") {
- adjustedKey = "ArrowLeft";
- }
- if (key === "ArrowLeft") {
- adjustedKey = "ArrowRight";
- }
- }
- const items = this.getItems();
- let selectedIndex = -1;
- items.forEach((item, index) => {
- if (item === selectedItem) {
- selectedIndex = index;
- }
- });
- switch (adjustedKey) {
- case "ArrowLeft":
- case "ArrowUp":
- event.preventDefault();
- const previous = selectedIndex < 1 ? items.item(items.length - 1) : items.item(selectedIndex - 1);
- this.selectItem(previous, true);
- return;
- case "ArrowRight":
- case "ArrowDown":
- event.preventDefault();
- const next = selectedIndex === -1 ? items.item(1) : items.item(selectedIndex + 1) || items.item(0);
- this.selectItem(next, true);
- return;
- case " ":
- event.preventDefault();
- this.selectItem(event.target, true);
- return;
- default:
- return;
- }
- }
- // --------------------------------------------------------------------------
- //
- // Methods
- //
- // --------------------------------------------------------------------------
- /** Sets focus on the component. */
- async setFocus() {
- var _a;
- (_a = (this.selectedItem || this.getItems()[0])) === null || _a === void 0 ? void 0 : _a.focus();
- }
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- onLabelClick() {
- this.setFocus();
- }
- getItems() {
- return this.el.querySelectorAll("calcite-radio-group-item");
- }
- selectItem(selected, emit = false) {
- if (selected === this.selectedItem) {
- return;
- }
- const items = this.getItems();
- let match = null;
- items.forEach((item) => {
- const matches = item.value === selected.value;
- if ((matches && !item.checked) || (!matches && item.checked)) {
- item.checked = matches;
- }
- item.tabIndex = matches ? 0 : -1;
- if (matches) {
- match = item;
- if (emit) {
- this.calciteRadioGroupChange.emit(match.value);
- }
- }
- });
- this.selectedItem = match;
- if (Build.isBrowser && match) {
- match.focus();
- }
- }
- get el() { return this; }
- static get watchers() { return {
- "value": ["valueHandler"],
- "selectedItem": ["handleSelectedItemChange"]
- }; }
- static get style() { return radioGroupCss; }
- }, [1, "calcite-radio-group", {
- "appearance": [513],
- "disabled": [516],
- "required": [516],
- "layout": [513],
- "name": [513],
- "scale": [513],
- "value": [1025],
- "selectedItem": [1040],
- "width": [513],
- "setFocus": [64]
- }, [[0, "calciteInternalRadioGroupItemChange", "handleSelected"], [0, "keydown", "handleKeyDown"]]]);
- function defineCustomElement$1() {
- if (typeof customElements === "undefined") {
- return;
- }
- const components = ["calcite-radio-group"];
- components.forEach(tagName => { switch (tagName) {
- case "calcite-radio-group":
- if (!customElements.get(tagName)) {
- customElements.define(tagName, RadioGroup);
- }
- break;
- } });
- }
- defineCustomElement$1();
- const CalciteRadioGroup = RadioGroup;
- const defineCustomElement = defineCustomElement$1;
- export { CalciteRadioGroup, defineCustomElement };
|