UNPKG

@postnord/web-components

Version:

PostNord Web Components

282 lines (281 loc) 10.9 kB
/*! * Built with Stencil * By PostNord. */ import { h, Host, forceUpdate } from "@stencil/core"; import { alert_exclamation_circle, angle_down } from "pn-design-assets/pn-assets/icons.js"; import { uuidv4 } from "../../../index"; /** * The `pn-select` uses a native `select` element under the hood. * Use the slot to include `option` elements. * * Just like a regular `select`, you can use the `selected` prop on the nested option elements to preselect an option. * * @slot - This is the default slot, where the `option` elements go. * * @nativeChange Use the `change` event to listen to changes on the select element. */ export class PnSelect { constructor() { this.label = undefined; this.helpertext = undefined; this.selectId = this.id; this.icon = undefined; this.name = undefined; this.form = undefined; this.autocomplete = undefined; this.required = false; this.disabled = false; this.invalid = false; this.error = undefined; } id = `pn-select-${uuidv4()}`; idHelpertext = `${this.id}-text`; select; mo; hostElement; componentDidLoad() { if (this.mo) this.mo.disconnect(); this.mo = new MutationObserver(() => forceUpdate(this.hostElement)); this.mo.observe(this.hostElement, { childList: true, subtree: true }); } hasError() { return this.invalid || !!this.error; } hasMessage() { return !!this.helpertext || !!this.error; } render() { return (h(Host, { key: '6547ef0d2fc3b0dbfbcb5e183d52eb50699d8d2f' }, h("div", { key: 'ee058c03644eff4b5cbd577e76489aa70c8f1a4a', class: "pn-select", "data-error": this.hasError() }, this.label && (h("label", { key: 'e6924c61358ab86b7117b1b6e047bbb4dc0b1f1f', htmlFor: this.selectId, class: "pn-select-label" }, h("span", { key: '7bd2dd6a4ca5a2b70bd6b78ce7b0b94cc621af30' }, this.label))), h("div", { key: '8803be773c6c1fd7d863c7446afd325615a4ecd1', class: "pn-select-input" }, this.icon && h("pn-icon", { key: '9be2e1646f0264d83ad089e3ad2443566b6d77b7', class: "pn-select-icon", icon: this.icon, "data-custom": true, "aria-hidden": "true" }), this.hasError() && (h("pn-icon", { key: '09b9c13b5cb2bdcd06e4d8de3b29147591a2cabc', class: "pn-select-icon", icon: alert_exclamation_circle, color: "warning", "data-error": true, "aria-hidden": "true" })), h("select", { key: 'b62ab5d51792a0fdae9c471cca6e783305da742d', id: this.selectId, class: "pn-select-element", name: this.name, form: this.form, autocomplete: this.autocomplete, "aria-describedby": this.hasMessage() && this.idHelpertext, "aria-invalid": this.hasError().toString(), disabled: this.disabled, "data-placeholder": this.select?.value === '', ref: el => (this.select = el) }, h("slot", { key: 'e56bf23e804b82a5b221e01c2977188bec004cc8' })), h("pn-icon", { key: '2f8cc0b7c23ab34574796fa87cd2ccbb5cd98930', class: "pn-select-icon", icon: angle_down, color: "blue700", "data-default": true, "aria-hidden": "true" })), this.hasMessage() && (h("p", { key: '5188e3df8a8105bf4ff86a139586171185cabaa8', id: this.idHelpertext, class: "pn-select-helper", role: !!this.error ? 'alert' : null }, h("span", { key: '5ded8e4687577e6d44d10caf6c6c770ed366e5e6' }, this.error || this.helpertext)))))); } static get is() { return "pn-select"; } static get originalStyleUrls() { return { "$": ["pn-select.scss"] }; } static get styleUrls() { return { "$": ["pn-select.css"] }; } static get properties() { return { "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "Label placed above the select." }, "attribute": "label", "reflect": false }, "helpertext": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Display a helper text underneath the select." }, "attribute": "helpertext", "reflect": false }, "selectId": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Select HTML id" }, "attribute": "select-id", "reflect": false, "defaultValue": "this.id" }, "icon": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Display an icon to the left of the select input." }, "attribute": "icon", "reflect": false }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "HTML" }], "text": "HTML select name." }, "attribute": "name", "reflect": false }, "form": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "HTML" }], "text": "HTML form name." }, "attribute": "form", "reflect": false }, "autocomplete": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "HTML" }], "text": "HTML autocomplete." }, "attribute": "autocomplete", "reflect": false }, "required": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Validation" }], "text": "Set the select as required." }, "attribute": "required", "reflect": false, "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Validation" }], "text": "Disable the select." }, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "invalid": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Validation" }], "text": "Trigger the invalid state." }, "attribute": "invalid", "reflect": false, "defaultValue": "false" }, "error": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Validation" }], "text": "Display an error message and trigger the invalid state." }, "attribute": "error", "reflect": false } }; } static get elementRef() { return "hostElement"; } } //# sourceMappingURL=pn-select.js.map