123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- /*!
- * 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 { Build, h, Host } from "@stencil/core";
- import { getElementDir } from "../../utils/dom";
- import { connectLabel, disconnectLabel } from "../../utils/label";
- import { afterConnectDefaultValueSet, connectForm, disconnectForm, HiddenFormInputSlot } from "../../utils/form";
- import { updateHostInteraction } from "../../utils/interactive";
- /**
- * @slot - A slot for adding `calcite-radio-group-item`s.
- */
- export class RadioGroup {
- constructor() {
- //--------------------------------------------------------------------------
- //
- // 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();
- }
- }
- static get is() { return "calcite-radio-group"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() {
- return {
- "$": ["radio-group.scss"]
- };
- }
- static get styleUrls() {
- return {
- "$": ["radio-group.css"]
- };
- }
- static get properties() {
- return {
- "appearance": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "RadioAppearance",
- "resolved": "\"outline\" | \"solid\"",
- "references": {
- "RadioAppearance": {
- "location": "import",
- "path": "./interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the appearance style of the component."
- },
- "attribute": "appearance",
- "reflect": true,
- "defaultValue": "\"solid\""
- },
- "disabled": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
- },
- "attribute": "disabled",
- "reflect": true,
- "defaultValue": "false"
- },
- "required": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "When `true`, the component must have a value in order for the form to submit."
- },
- "attribute": "required",
- "reflect": true,
- "defaultValue": "false"
- },
- "layout": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "Layout",
- "resolved": "\"grid\" | \"horizontal\" | \"vertical\"",
- "references": {
- "Layout": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Defines the layout of the component."
- },
- "attribute": "layout",
- "reflect": true,
- "defaultValue": "\"horizontal\""
- },
- "name": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the name of the component on form submission."
- },
- "attribute": "name",
- "reflect": true
- },
- "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": "Specifies the size of the component."
- },
- "attribute": "scale",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "value": {
- "type": "string",
- "mutable": true,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The component's `selectedItem` value."
- },
- "attribute": "value",
- "reflect": false,
- "defaultValue": "null"
- },
- "selectedItem": {
- "type": "unknown",
- "mutable": true,
- "complexType": {
- "original": "HTMLCalciteRadioGroupItemElement",
- "resolved": "HTMLCalciteRadioGroupItemElement",
- "references": {
- "HTMLCalciteRadioGroupItemElement": {
- "location": "global"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "readonly",
- "text": undefined
- }],
- "text": "The component's selected item `HTMLElement`."
- }
- },
- "width": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "Extract<\"auto\" | \"full\", Width>",
- "resolved": "\"auto\" | \"full\"",
- "references": {
- "Extract": {
- "location": "global"
- },
- "Width": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the width of the component."
- },
- "attribute": "width",
- "reflect": true,
- "defaultValue": "\"auto\""
- }
- };
- }
- static get events() {
- return [{
- "method": "calciteRadioGroupChange",
- "name": "calciteRadioGroupChange",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the selected option changes, where the event detail is the new value."
- },
- "complexType": {
- "original": "string",
- "resolved": "string",
- "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": []
- }
- }
- };
- }
- static get elementRef() { return "el"; }
- static get watchers() {
- return [{
- "propName": "value",
- "methodName": "valueHandler"
- }, {
- "propName": "selectedItem",
- "methodName": "handleSelectedItemChange"
- }];
- }
- static get listeners() {
- return [{
- "name": "calciteInternalRadioGroupItemChange",
- "method": "handleSelected",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "keydown",
- "method": "handleKeyDown",
- "target": undefined,
- "capture": false,
- "passive": false
- }];
- }
- }
|