UNPKG

@scania/tegel

Version:
267 lines (266 loc) 9.59 kB
import { h } from "@stencil/core"; import generateUniqueId from "../../utils/generateUniqueId"; /** * @slot label - Slot for the label text. */ export class TdsToggle { constructor() { this.handleToggle = () => { this.checked = !this.checked; this.tdsToggle.emit({ toggleId: this.toggleId, checked: this.checked, }); }; this.checked = false; this.required = false; this.size = 'lg'; this.name = undefined; this.headline = undefined; this.disabled = false; this.toggleId = generateUniqueId(); this.tdsAriaLabel = undefined; } /** Toggles the Toggle. */ async toggle() { this.checked = !this.checked; return { toggleId: this.toggleId, checked: this.checked, }; } /** Method to programmatically focus the toggle element */ async focusElement() { if (this.inputElement) { this.inputElement.focus(); } } componentWillLoad() { this.labelSlot = this.host.querySelector("[slot='label']"); } connectedCallback() { if (!this.tdsAriaLabel) { console.warn('Tegel Toggle component: tdsAriaLabel prop is missing'); } } render() { return (h("div", { key: '212a541d06f0cc534855ac872cfb386dc1b2d4ff', class: "tds-toggle" }, this.headline && (h("div", { key: '076c51461d4898d2bfa3c86fadd94ac2ad63c7a8', class: { 'toggle-headline': true, 'disabled': this.disabled, } }, this.headline)), h("input", { key: 'e0e849d7b05b77612fc949894cac8befa5b89370', ref: (inputEl) => (this.inputElement = inputEl), "aria-label": this.tdsAriaLabel, "aria-describedby": this.host.getAttribute('aria-describedby'), "aria-labelledby": this.host.getAttribute('aria-labelledby'), "aria-checked": this.checked, "aria-required": this.required, onChange: () => this.handleToggle(), class: `${this.size}`, checked: this.checked, disabled: this.disabled, required: this.required, type: "checkbox", name: this.name, id: this.toggleId, role: "switch" }), this.labelSlot && (h("label", { key: '4d1dcc70f896695657ca7ca68fd832ee4d13417a', class: { disabled: this.disabled }, htmlFor: this.toggleId }, h("slot", { key: '800ea4ee2760efee8cdfe1ac3aee4b3a6f95f199', name: "label" }))))); } static get is() { return "tds-toggle"; } static get encapsulation() { return "scoped"; } static get originalStyleUrls() { return { "$": ["toggle.scss"] }; } static get styleUrls() { return { "$": ["toggle.css"] }; } static get properties() { return { "checked": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Sets the Toggle as checked" }, "attribute": "checked", "reflect": true, "defaultValue": "false" }, "required": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Make the Toggle required" }, "attribute": "required", "reflect": false, "defaultValue": "false" }, "size": { "type": "string", "mutable": false, "complexType": { "original": "'sm' | 'lg'", "resolved": "\"lg\" | \"sm\"", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Size of the Toggle" }, "attribute": "size", "reflect": false, "defaultValue": "'lg'" }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Name of the toggle's input element" }, "attribute": "name", "reflect": false }, "headline": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Headline for the Toggle" }, "attribute": "headline", "reflect": false }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Sets the Toggle in a disabled state" }, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "toggleId": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "ID of the Toggle's input element, if not specified, it's randomly generated" }, "attribute": "toggle-id", "reflect": false, "defaultValue": "generateUniqueId()" }, "tdsAriaLabel": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Defines aria-label attribute for input" }, "attribute": "tds-aria-label", "reflect": false } }; } static get events() { return [{ "method": "tdsToggle", "name": "tdsToggle", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Sends unique Toggle identifier and status when it is toggled." }, "complexType": { "original": "{\n toggleId: string;\n checked: boolean;\n }", "resolved": "{ toggleId: string; checked: boolean; }", "references": {} } }]; } static get methods() { return { "toggle": { "complexType": { "signature": "() => Promise<{ toggleId: string; checked: boolean; }>", "parameters": [], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<{ toggleId: string; checked: boolean; }>" }, "docs": { "text": "Toggles the Toggle.", "tags": [] } }, "focusElement": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "Method to programmatically focus the toggle element", "tags": [] } } }; } static get elementRef() { return "host"; } }