@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
208 lines (204 loc) • 11.5 kB
JavaScript
import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8fd57462.js';
import { g as getElementDir } from './dom-d9ba1da4.js';
import { g as guid } from './guid-09142681.js';
const calciteRadioButtonCss = "@-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)}}.sc-calcite-radio-button:root{--calcite-popper-transition:150ms ease-in-out}[hidden].sc-calcite-radio-button-h{display:none}[theme=dark].sc-calcite-radio-button-h,[theme=dark] .sc-calcite-radio-button-h{--calcite-ui-blue-1:#00A0FF;--calcite-ui-blue-2:#0087D7;--calcite-ui-blue-3:#47BBFF;--calcite-ui-green-1:#36DA43;--calcite-ui-green-2:#11AD1D;--calcite-ui-green-3:#44ED51;--calcite-ui-yellow-1:#FFC900;--calcite-ui-yellow-2:#F4B000;--calcite-ui-yellow-3:#FFE24D;--calcite-ui-red-1:#FE583E;--calcite-ui-red-2:#F3381B;--calcite-ui-red-3:#FF7465;--calcite-ui-background:#202020;--calcite-ui-foreground-1:#2b2b2b;--calcite-ui-foreground-2:#353535;--calcite-ui-foreground-3:#404040;--calcite-ui-text-1:#ffffff;--calcite-ui-text-2:#bfbfbf;--calcite-ui-text-3:#9f9f9f;--calcite-ui-border-1:#4a4a4a;--calcite-ui-border-2:#404040;--calcite-ui-border-3:#353535;--calcite-ui-border-4:#757575;--calcite-ui-border-5:#9f9f9f}.sc-calcite-radio-button-h{cursor:pointer;display:inline-block}[labeled].sc-calcite-radio-button-h{--calcite-label-margin-bottom:0;-ms-flex-align:center;align-items:center;display:-ms-inline-flexbox;display:inline-flex;gap:8px}[disabled].sc-calcite-radio-button-h{cursor:default}.sc-calcite-radio-button-h[disabled]>.sc-calcite-radio-button-s>calcite-label{cursor:default}[scale=s][labeled].sc-calcite-radio-button-h{line-height:1.33;margin-bottom:8px;margin-right:12px}@supports not (display: grid){[scale=s][labeled].sc-calcite-radio-button-h calcite-radio.sc-calcite-radio-button{margin-right:8px}}[scale=m][labeled].sc-calcite-radio-button-h{line-height:1.15;margin-bottom:8px;margin-right:16px}@supports not (display: grid){[scale=m][labeled].sc-calcite-radio-button-h calcite-radio.sc-calcite-radio-button{margin-right:8px}}[scale=l][labeled].sc-calcite-radio-button-h{gap:12px;line-height:1.2;margin-bottom:10px;margin-right:20px}@supports not (display: grid){[scale=l][labeled].sc-calcite-radio-button-h calcite-radio.sc-calcite-radio-button{margin-right:12px}}[hidden].sc-calcite-radio-button-h{display:none}";
const CalciteRadioButton = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteRadioButtonChange = createEvent(this, "calciteRadioButtonChange", 7);
this.calciteRadioButtonCheckedChange = createEvent(this, "calciteRadioButtonCheckedChange", 7);
this.calciteRadioButtonFocusedChange = createEvent(this, "calciteRadioButtonFocusedChange", 7);
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/** The checked state of the radio button. */
this.checked = false;
/** The disabled state of the radio button. */
this.disabled = false;
/** The focused state of the radio button. */
this.focused = false;
/** The radio button's hidden status. When a radio button is hidden it is not focusable or checkable. */
this.hidden = false;
/** The hovered state of the radio button. */
this.hovered = false;
/** Requires that a value is selected for the radio button group before the parent form will submit. */
this.required = false;
/** The scale (size) of the radio button. <code>scale</code> is passed as a property automatically from <code>calcite-radio-button-group</code>. */
this.scale = "m";
/** The color theme of the radio button, <code>theme</code> is passed as a property automatically from <code>calcite-radio-button-group</code>. */
this.theme = "light";
this.formResetHandler = () => {
var _a;
this.checked = this.initialChecked;
this.initialChecked && ((_a = this.input) === null || _a === void 0 ? void 0 : _a.setAttribute("checked", ""));
};
this.onInputBlur = () => {
this.focused = false;
this.calciteRadioButtonFocusedChange.emit();
};
this.onInputFocus = () => {
this.focused = true;
this.calciteRadioButtonFocusedChange.emit();
};
}
checkedChanged(newChecked) {
if (newChecked) {
this.uncheckOtherRadioButtonsInGroup();
}
if (this.input) {
this.input.checked = newChecked;
}
this.calciteRadioButtonCheckedChange.emit(newChecked);
}
disabledChanged(disabled) {
this.input.disabled = disabled;
}
focusedChanged(focused) {
if (!this.input)
return;
if (focused && !this.el.hasAttribute("hidden")) {
this.input.focus();
}
else {
this.input.blur();
}
}
hiddenChanged(newHidden) {
this.input.hidden = newHidden;
}
nameChanged(newName) {
if (this.name === newName) {
return;
}
if (this.input) {
this.input.name = newName;
}
this.checkLastRadioButton();
const currentValue = this.rootNode.querySelector(`input[name="${this.name}"]:checked`);
if (!(currentValue === null || currentValue === void 0 ? void 0 : currentValue.value)) {
this.uncheckAllRadioButtonsInGroup();
}
}
requiredChanged(required) {
this.input.required = required;
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
checkLastRadioButton() {
const radioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button")).filter((radioButton) => radioButton.name === this.name);
const checkedRadioButtons = radioButtons.filter((radioButton) => radioButton.checked);
if ((checkedRadioButtons === null || checkedRadioButtons === void 0 ? void 0 : checkedRadioButtons.length) > 1) {
const lastCheckedRadioButton = checkedRadioButtons[checkedRadioButtons.length - 1];
checkedRadioButtons
.filter((checkedRadioButton) => checkedRadioButton !== lastCheckedRadioButton)
.forEach((checkedRadioButton) => {
checkedRadioButton.checked = false;
checkedRadioButton.emitCheckedChange();
});
}
}
/** @internal */
async emitCheckedChange() {
this.calciteRadioButtonCheckedChange.emit();
}
uncheckAllRadioButtonsInGroup() {
const otherRadioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button")).filter((radioButton) => radioButton.name === this.name);
otherRadioButtons.forEach((otherRadioButton) => {
if (otherRadioButton.checked) {
otherRadioButton.checked = false;
otherRadioButton.focused = false;
}
});
}
uncheckOtherRadioButtonsInGroup() {
const otherRadioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button")).filter((radioButton) => radioButton.name === this.name && radioButton.guid !== this.guid);
otherRadioButtons.forEach((otherRadioButton) => {
if (otherRadioButton.checked) {
otherRadioButton.checked = false;
otherRadioButton.focused = false;
}
});
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
check(event) {
// Prevent parent label from clicking the first radio when calcite-radio-button is clicked
if (this.el.closest("label") && (event.target === this.el || event.target === this.radio)) {
event.preventDefault();
}
if (!this.disabled && !this.hidden) {
this.uncheckOtherRadioButtonsInGroup();
this.checked = true;
this.focused = true;
this.calciteRadioButtonChange.emit();
}
}
mouseenter() {
this.hovered = true;
}
mouseleave() {
this.hovered = false;
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
this.rootNode = this.el.getRootNode();
this.guid = this.el.id || `calcite-radio-button-${guid()}`;
this.initialChecked = this.checked;
if (this.name) {
this.checkLastRadioButton();
}
const form = this.el.closest("form");
if (form) {
form.addEventListener("reset", this.formResetHandler);
}
}
componentDidLoad() {
if (this.focused) {
this.input.focus();
}
}
disconnectedCallback() {
const form = this.el.closest("form");
if (form) {
form.removeEventListener("reset", this.formResetHandler);
}
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderLabel() {
if (this.el.textContent) {
return (h("calcite-label", { dir: getElementDir(this.el), "disable-spacing": true, disabled: this.disabled, for: `${this.guid}-input`, layout: "inline", scale: this.scale }, h("slot", null)));
}
return h("slot", null);
}
render() {
const inputStyle = { opacity: "0", position: "fixed", zIndex: "-1" };
return (h(Host, { labeled: !!this.el.textContent }, h("input", { "aria-label": this.value || this.guid, checked: this.checked, disabled: this.disabled, hidden: this.hidden, id: `${this.guid}-input`, name: this.name, onBlur: this.onInputBlur, onFocus: this.onInputFocus, ref: (el) => (this.input = el), required: this.required, style: inputStyle, type: "radio", value: this.value }), h("calcite-radio", { checked: this.checked, disabled: this.disabled, focused: this.focused, hidden: this.hidden, hovered: this.hovered, ref: (el) => (this.radio = el), scale: this.scale, theme: this.theme }), this.renderLabel()));
}
get el() { return getElement(this); }
static get watchers() { return {
"checked": ["checkedChanged"],
"disabled": ["disabledChanged"],
"focused": ["focusedChanged"],
"hidden": ["hiddenChanged"],
"name": ["nameChanged"],
"required": ["requiredChanged"]
}; }
};
CalciteRadioButton.style = calciteRadioButtonCss;
export { CalciteRadioButton as calcite_radio_button };