UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

172 lines (171 loc) 4.67 kB
import { Component, Host, h, Prop, Element, Event, Watch } from "@stencil/core"; export class CalciteOption { constructor() { //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- /** * When true, it prevents the option from being selected. */ this.disabled = false; this.mutationObserver = new MutationObserver(() => { this.ensureTextContentDependentProps(); this.calciteOptionChange.emit(); }); } handlePropChange(_newValue, _oldValue, propName) { if (propName === "label" || propName === "value") { this.ensureTextContentDependentProps(); } this.calciteOptionChange.emit(); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- ensureTextContentDependentProps() { const { el: { textContent } } = this; if (!this.label || this.label === this.internallySetLabel) { this.label = textContent; this.internallySetLabel = textContent; } if (!this.value || this.value === this.internallySetValue) { this.value = textContent; this.internallySetValue = textContent; } } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { this.ensureTextContentDependentProps(); this.mutationObserver.observe(this.el, { characterData: true, subtree: true, attributeFilter: ["label", "value"] }); } disconnectedCallback() { this.mutationObserver.disconnect(); } //-------------------------------------------------------------------------- // // Render Methods // //-------------------------------------------------------------------------- render() { return (h(Host, null, h("slot", null, this.label))); } static get is() { return "calcite-option"; } static get encapsulation() { return "shadow"; } static get properties() { return { "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When true, it prevents the option from being selected." }, "attribute": "disabled", "reflect": true, "defaultValue": "false" }, "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The option label." }, "attribute": "label", "reflect": false }, "selected": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When true, this option is selected. Otherwise, false." }, "attribute": "selected", "reflect": true }, "value": { "type": "any", "mutable": false, "complexType": { "original": "any", "resolved": "any", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The value associated with this option." }, "attribute": "value", "reflect": false } }; } static get events() { return [{ "method": "calciteOptionChange", "name": "calciteOptionChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "text": undefined, "name": "internal" }], "text": "" }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "disabled", "methodName": "handlePropChange" }, { "propName": "label", "methodName": "handlePropChange" }, { "propName": "selected", "methodName": "handlePropChange" }, { "propName": "value", "methodName": "handlePropChange" }]; } }