@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
235 lines (230 loc) • 11 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { proxyCustomElement, HTMLElement, createEvent, h, Host, Build } from '@stencil/core/internal/client';
import { a as getElementDir } from './dom.js';
import { c as connectLabel, d as disconnectLabel } from './label.js';
import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
import { u as updateHostInteraction } from './interactive.js';
const radioGroupCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:-ms-flexbox;display:flex;background-color:var(--calcite-ui-foreground-1);width:-moz-fit-content;width:-webkit-fit-content;width:fit-content;outline:1px solid var(--calcite-ui-border-input);outline-offset:-1px}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([layout=vertical]){-ms-flex-direction:column;flex-direction:column;-ms-flex-align:start;align-items:flex-start;-ms-flex-item-align:start;align-self:flex-start}:host([width=full]){width:100%;min-width:-moz-fit-content;min-width:-webkit-fit-content;min-width:fit-content}:host([width=full]) ::slotted(calcite-radio-group-item){-ms-flex:1 1 auto;flex:1 1 auto}:host([width=full][layout=vertical]) ::slotted(calcite-radio-group-item){-ms-flex-pack:start;justify-content:flex-start}::slotted(calcite-radio-group-item[checked]),::slotted(calcite-radio-group-item:focus){z-index:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !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", 7);
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/** specify the appearance style of the radio group, defaults to solid. */
this.appearance = "solid";
/** is the radio group disabled */
this.disabled = false;
/**
* When true, makes the component required for form-submission.
*
* @internal
*/
this.required = false;
/** specify the layout of the radio group, defaults to horizontal */
this.layout = "horizontal";
/** The scale of the radio group */
this.scale = "m";
/** The value of the selectedItem */
this.value = null;
/** specify the width of the group, defaults to auto */
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;
}
}
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.stopPropagation();
event.preventDefault();
this.selectItem(event.target);
}
handleKeyDown(event) {
const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
const key = event.key;
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": [1],
"scale": [513],
"value": [1025],
"selectedItem": [1032, "selected-item"],
"width": [513],
"setFocus": [64]
}, [[0, "calciteRadioGroupItemChange", "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 };