123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- /*!
- * 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.82
- */
- import { Component, Element, Event, h, Host, Listen, Method, Prop, Watch } from "@stencil/core";
- import { guid } from "../../utils/guid";
- import { focusElement, getElementDir, toAriaBoolean } from "../../utils/dom";
- import { connectLabel, disconnectLabel, getLabelText } from "../../utils/label";
- import { HiddenFormInputSlot, connectForm, disconnectForm } from "../../utils/form";
- import { CSS } from "./resources";
- import { getRoundRobinIndex } from "../../utils/array";
- import { updateHostInteraction } from "../../utils/interactive";
- export class RadioButton {
- constructor() {
- //--------------------------------------------------------------------------
- //
- // 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.
- * @internal
- */
- 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.
- * @internal
- */
- 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. `scale` is passed as a property automatically from `calcite-radio-button-group`. */
- this.scale = "m";
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- this.selectItem = (items, selectedIndex) => {
- items[selectedIndex].click();
- };
- this.queryButtons = () => {
- return Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter((radioButton) => radioButton.name === this.name);
- };
- this.isDefaultSelectable = () => {
- const radioButtons = this.queryButtons();
- return !radioButtons.some((radioButton) => radioButton.checked) && radioButtons[0] === this.el;
- };
- this.check = () => {
- if (this.disabled) {
- return;
- }
- this.uncheckAllRadioButtonsInGroup();
- this.checked = true;
- this.calciteRadioButtonChange.emit();
- this.setFocus();
- };
- this.clickHandler = () => {
- this.check();
- };
- this.setContainerEl = (el) => {
- this.containerEl = el;
- };
- this.handleKeyDown = (event) => {
- const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
- const key = event.key;
- const { el } = this;
- if (keys.indexOf(key) === -1) {
- return;
- }
- if (key === " ") {
- this.check();
- return;
- }
- let adjustedKey = key;
- if (getElementDir(el) === "rtl") {
- if (key === "ArrowRight") {
- adjustedKey = "ArrowLeft";
- }
- if (key === "ArrowLeft") {
- adjustedKey = "ArrowRight";
- }
- }
- const radioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden]")).filter((radioButton) => radioButton.name === this.name);
- let currentIndex = 0;
- const radioButtonsLength = radioButtons.length;
- radioButtons.some((item, index) => {
- if (item.checked) {
- currentIndex = index;
- return true;
- }
- });
- switch (adjustedKey) {
- case "ArrowLeft":
- case "ArrowUp":
- event.preventDefault();
- this.selectItem(radioButtons, getRoundRobinIndex(Math.max(currentIndex - 1, -1), radioButtonsLength));
- return;
- case "ArrowRight":
- case "ArrowDown":
- event.preventDefault();
- this.selectItem(radioButtons, getRoundRobinIndex(currentIndex + 1, radioButtonsLength));
- return;
- default:
- return;
- }
- };
- this.onContainerBlur = () => {
- this.focused = false;
- this.calciteInternalRadioButtonBlur.emit();
- };
- this.onContainerFocus = () => {
- if (!this.disabled) {
- this.focused = true;
- this.calciteInternalRadioButtonFocus.emit();
- }
- };
- }
- checkedChanged(newChecked) {
- if (newChecked) {
- this.uncheckOtherRadioButtonsInGroup();
- }
- this.calciteInternalRadioButtonCheckedChange.emit(newChecked);
- }
- nameChanged() {
- this.checkLastRadioButton();
- }
- //--------------------------------------------------------------------------
- //
- // Public Methods
- //
- //--------------------------------------------------------------------------
- /** Sets focus on the component. */
- async setFocus() {
- if (!this.disabled) {
- focusElement(this.containerEl);
- }
- }
- onLabelClick(event) {
- if (!this.disabled && !this.hidden) {
- this.uncheckOtherRadioButtonsInGroup();
- const label = event.currentTarget;
- const radioButton = label.for
- ? this.rootNode.querySelector(`calcite-radio-button[id="${label.for}"]`)
- : label.querySelector(`calcite-radio-button[name="${this.name}"]`);
- if (radioButton) {
- radioButton.checked = true;
- radioButton.focused = true;
- }
- this.calciteRadioButtonChange.emit();
- this.setFocus();
- }
- }
- checkLastRadioButton() {
- const radioButtons = this.queryButtons();
- 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.calciteInternalRadioButtonCheckedChange.emit();
- }
- uncheckAllRadioButtonsInGroup() {
- const radioButtons = this.queryButtons();
- radioButtons.forEach((radioButton) => {
- if (radioButton.checked) {
- radioButton.checked = false;
- radioButton.focused = false;
- }
- });
- }
- uncheckOtherRadioButtonsInGroup() {
- const radioButtons = this.queryButtons();
- const otherRadioButtons = radioButtons.filter((radioButton) => radioButton.guid !== this.guid);
- otherRadioButtons.forEach((otherRadioButton) => {
- if (otherRadioButton.checked) {
- otherRadioButton.checked = false;
- otherRadioButton.focused = false;
- }
- });
- }
- getTabIndex() {
- if (this.disabled) {
- return undefined;
- }
- return this.checked || this.isDefaultSelectable() ? 0 : -1;
- }
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- mouseenter() {
- this.hovered = true;
- }
- mouseleave() {
- this.hovered = false;
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- connectedCallback() {
- this.rootNode = this.el.getRootNode();
- this.guid = this.el.id || `calcite-radio-button-${guid()}`;
- if (this.name) {
- this.checkLastRadioButton();
- }
- connectLabel(this);
- connectForm(this);
- }
- componentDidLoad() {
- if (this.focused && !this.disabled) {
- this.setFocus();
- }
- }
- disconnectedCallback() {
- disconnectLabel(this);
- disconnectForm(this);
- }
- componentDidRender() {
- updateHostInteraction(this);
- }
- // --------------------------------------------------------------------------
- //
- // Render Methods
- //
- // --------------------------------------------------------------------------
- render() {
- const tabIndex = this.getTabIndex();
- return (h(Host, { onClick: this.clickHandler, onKeyDown: this.handleKeyDown },
- h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: CSS.container, onBlur: this.onContainerBlur, onFocus: this.onContainerFocus, ref: this.setContainerEl, role: "radio", tabIndex: tabIndex },
- h("div", { class: "radio" })),
- h(HiddenFormInputSlot, { component: this })));
- }
- static get is() { return "calcite-radio-button"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() { return {
- "$": ["radio-button.scss"]
- }; }
- static get styleUrls() { return {
- "$": ["radio-button.css"]
- }; }
- static get properties() { return {
- "checked": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The checked state of the radio button."
- },
- "attribute": "checked",
- "reflect": true,
- "defaultValue": "false"
- },
- "disabled": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The disabled state of the radio button."
- },
- "attribute": "disabled",
- "reflect": true,
- "defaultValue": "false"
- },
- "focused": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "The focused state of the radio button."
- },
- "attribute": "focused",
- "reflect": true,
- "defaultValue": "false"
- },
- "guid": {
- "type": "string",
- "mutable": true,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The id attribute of the radio button. When omitted, a globally unique identifier is used."
- },
- "attribute": "guid",
- "reflect": true
- },
- "hidden": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The radio button's hidden status. When a radio button is hidden it is not focusable or checkable."
- },
- "attribute": "hidden",
- "reflect": true,
- "defaultValue": "false"
- },
- "hovered": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "The hovered state of the radio button."
- },
- "attribute": "hovered",
- "reflect": true,
- "defaultValue": "false"
- },
- "label": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "The label of the radio input"
- },
- "attribute": "label",
- "reflect": false
- },
- "name": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The name of the radio button. `name` is passed as a property automatically from `calcite-radio-button-group`."
- },
- "attribute": "name",
- "reflect": true
- },
- "required": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Requires that a value is selected for the radio button group before the parent form will submit."
- },
- "attribute": "required",
- "reflect": true,
- "defaultValue": "false"
- },
- "scale": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "Scale",
- "resolved": "\"l\" | \"m\" | \"s\"",
- "references": {
- "Scale": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The scale (size) of the radio button. `scale` is passed as a property automatically from `calcite-radio-button-group`."
- },
- "attribute": "scale",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "value": {
- "type": "any",
- "mutable": true,
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- },
- "required": true,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The value of the radio button."
- },
- "attribute": "value",
- "reflect": false
- }
- }; }
- static get events() { return [{
- "method": "calciteInternalRadioButtonBlur",
- "name": "calciteInternalRadioButtonBlur",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "Fires when the radio button is blurred."
- },
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- }
- }, {
- "method": "calciteRadioButtonChange",
- "name": "calciteRadioButtonChange",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires only when the radio button is checked. This behavior is identical to the native HTML input element.\nSince this event does not fire when the radio button is unchecked, it's not recommended to attach a listener for this event\ndirectly on the element, but instead either attach it to a node that contains all of the radio buttons in the group\nor use the calciteRadioButtonGroupChange event if using this with calcite-radio-button-group."
- },
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- }
- }, {
- "method": "calciteInternalRadioButtonCheckedChange",
- "name": "calciteInternalRadioButtonCheckedChange",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "Fires when the checked property changes. This is an internal event used for styling purposes only.\nUse calciteRadioButtonChange or calciteRadioButtonGroupChange for responding to changes in the checked value for forms."
- },
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- }
- }, {
- "method": "calciteInternalRadioButtonFocus",
- "name": "calciteInternalRadioButtonFocus",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "Fires when the radio button is focused."
- },
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- }
- }]; }
- static get methods() { return {
- "setFocus": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Sets focus on the component.",
- "tags": []
- }
- },
- "emitCheckedChange": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "",
- "tags": [{
- "name": "internal",
- "text": undefined
- }]
- }
- }
- }; }
- static get elementRef() { return "el"; }
- static get watchers() { return [{
- "propName": "checked",
- "methodName": "checkedChanged"
- }, {
- "propName": "name",
- "methodName": "nameChanged"
- }]; }
- static get listeners() { return [{
- "name": "mouseenter",
- "method": "mouseenter",
- "target": undefined,
- "capture": false,
- "passive": true
- }, {
- "name": "mouseleave",
- "method": "mouseleave",
- "target": undefined,
- "capture": false,
- "passive": true
- }]; }
- }
|