@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
189 lines (188 loc) • 5.12 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.
* v1.5.0-next.4
*/
import { h } from "@stencil/core";
import { createObserver } from "../../utils/observers";
export class Option {
constructor() {
this.mutationObserver = createObserver("mutation", () => {
this.ensureTextContentDependentProps();
this.calciteInternalOptionChange.emit();
});
this.disabled = false;
this.label = undefined;
this.selected = undefined;
this.value = undefined;
}
handlePropChange(_newValue, _oldValue, propName) {
if (propName === "label" || propName === "value") {
this.ensureTextContentDependentProps();
}
this.calciteInternalOptionChange.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, {
attributeFilter: ["label", "value"],
characterData: true,
childList: true,
subtree: true
});
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
}
//--------------------------------------------------------------------------
//
// Render Methods
//
//--------------------------------------------------------------------------
render() {
return h("slot", null, this.label);
}
static get is() { return "calcite-option"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["option.scss"]
};
}
static get styleUrls() {
return {
"$": ["option.css"]
};
}
static get properties() {
return {
"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"
},
"label": {
"type": "string",
"mutable": true,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Accessible name for the component."
},
"attribute": "label",
"reflect": false
},
"selected": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the component is selected."
},
"attribute": "selected",
"reflect": true
},
"value": {
"type": "any",
"mutable": true,
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The component's value."
},
"attribute": "value",
"reflect": false
}
};
}
static get events() {
return [{
"method": "calciteInternalOptionChange",
"name": "calciteInternalOptionChange",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "void",
"resolved": "void",
"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"
}];
}
}