UNPKG

@scania/tegel

Version:
287 lines (286 loc) 10.9 kB
import { Host, h, } from "@stencil/core"; import { convertToString } from "../../../utils/convertToString"; /** * @slot <default> - <b>Unnamed slot.</b> For the option label text. */ export class TdsDropdownOption { constructor() { /** Internal value storage that's always a string */ this.internalValue = ''; /** Sets the option as disabled. */ this.disabled = false; this.selected = false; this.multiselect = false; this.size = 'lg'; this.parentDropdown = null; // @ts-expect-error - label property is used internally for text content tracking // eslint-disable-next-line no-unused-vars, this.label = ''; this.componentWillRender = () => { var _a, _b, _c; if (!this.host.parentElement) { return; } this.parentDropdown = ((_a = this.host.parentElement) === null || _a === void 0 ? void 0 : _a.tagName) === 'TDS-DROPDOWN' ? this.host.parentElement : this.host.getRootNode().host; if (this.parentDropdown) { this.multiselect = (_b = this.parentDropdown.multiselect) !== null && _b !== void 0 ? _b : false; this.size = this.parentDropdown.size || 'lg'; } this.label = ((_c = this.host.textContent) === null || _c === void 0 ? void 0 : _c.trim()) || ''; }; this.handleSingleSelect = () => { var _a, _b; if (!this.disabled) { this.selected = true; (_a = this.parentDropdown) === null || _a === void 0 ? void 0 : _a.appendValue(this.internalValue); (_b = this.parentDropdown) === null || _b === void 0 ? void 0 : _b.close(); this.tdsSelect.emit({ value: this.internalValue, selected: this.selected, }); } }; this.handleMultiselect = (event) => { var _a, _b; if (!this.disabled) { if (event.detail.checked) { (_a = this.parentDropdown) === null || _a === void 0 ? void 0 : _a.appendValue(this.internalValue); this.selected = true; this.tdsSelect.emit({ value: this.internalValue, selected: this.selected, }); } else { (_b = this.parentDropdown) === null || _b === void 0 ? void 0 : _b.removeValue(this.internalValue); this.selected = false; this.tdsSelect.emit({ value: this.internalValue, selected: this.selected, }); } event.stopPropagation(); } }; this.handleFocus = (event) => { // Focus events are now handled by the parent dropdown component // Only emit if this is a standalone option (not within a dropdown) if (!this.parentDropdown) { this.tdsFocus.emit(event); } }; this.handleBlur = (event) => { // Blur events are now handled by the parent dropdown component // Only emit if this is a standalone option (not within a dropdown) if (!this.parentDropdown) { this.tdsBlur.emit(event); } }; } /** Method to select/deselect an option. */ async setSelected(selected) { this.selected = selected; } valueWatcher(newValue) { this.internalValue = convertToString(newValue); } componentWillLoad() { this.internalValue = convertToString(this.value); } render() { return (h(Host, { key: '35542ea54d2ba455982e2470190711f75f88a999' }, h("div", { key: '4236d5281cf4675586a7d8b16e80141b9689dad3', class: `dropdown-option ${this.size} ${this.selected ? 'selected' : ''} ${this.disabled ? 'disabled' : ''} ` }, this.multiselect ? (h("div", { class: "multiselect", onKeyDown: (event) => { var _a; if (event.key === 'Escape') { (_a = this.parentDropdown) === null || _a === void 0 ? void 0 : _a.close(); } } }, h("tds-checkbox", { onTdsChange: (event) => { this.handleMultiselect(event); }, onTdsBlur: (event) => { event.stopPropagation(); }, disabled: this.disabled, checked: this.selected, tdsAriaLabel: this.tdsAriaLabel, class: { [this.size]: true, } }, h("div", { slot: "label" }, h("slot", null))))) : (h("button", { role: "option", "aria-disabled": this.disabled, "aria-selected": this.selected, "aria-label": this.tdsAriaLabel, onClick: () => { this.handleSingleSelect(); }, onFocus: (event) => this.handleFocus(event), onBlur: (event) => this.handleBlur(event), disabled: this.disabled, class: this.size }, h("div", { class: "single-select" }, h("slot", null), this.selected && h("tds-icon", { name: "tick", size: "16px" }))))))); } static get is() { return "tds-dropdown-option"; } static get encapsulation() { return "shadow"; } static get delegatesFocus() { return true; } static get originalStyleUrls() { return { "$": ["dropdown-option.scss"] }; } static get styleUrls() { return { "$": ["dropdown-option.css"] }; } static get properties() { return { "value": { "type": "any", "mutable": false, "complexType": { "original": "string | number", "resolved": "number | string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Value of the dropdown option" }, "getter": false, "setter": false, "reflect": false, "attribute": "value" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Sets the option as disabled." }, "getter": false, "setter": false, "reflect": false, "attribute": "disabled", "defaultValue": "false" }, "tdsAriaLabel": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines aria-label attribute for the option" }, "getter": false, "setter": false, "reflect": false, "attribute": "tds-aria-label" } }; } static get states() { return { "internalValue": {}, "selected": {}, "multiselect": {}, "size": {} }; } static get events() { return [{ "method": "tdsSelect", "name": "tdsSelect", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Click event for the Dropdown option." }, "complexType": { "original": "{\n selected: boolean;\n value: string;\n }", "resolved": "{ selected: boolean; value: string; }", "references": {} } }, { "method": "tdsFocus", "name": "tdsFocus", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Focus event for the Dropdown option." }, "complexType": { "original": "FocusEvent", "resolved": "FocusEvent", "references": { "FocusEvent": { "location": "global", "id": "global::FocusEvent" } } } }, { "method": "tdsBlur", "name": "tdsBlur", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Blur event for the Dropdown option." }, "complexType": { "original": "FocusEvent", "resolved": "FocusEvent", "references": { "FocusEvent": { "location": "global", "id": "global::FocusEvent" } } } }]; } static get methods() { return { "setSelected": { "complexType": { "signature": "(selected: boolean) => Promise<void>", "parameters": [{ "name": "selected", "type": "boolean", "docs": "" }], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "Method to select/deselect an option.", "tags": [] } } }; } static get elementRef() { return "host"; } static get watchers() { return [{ "propName": "value", "methodName": "valueWatcher" }]; } }