@postnord/web-components
Version:
PostNord Web Components
282 lines (281 loc) • 10.9 kB
JavaScript
/*!
* 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: '1e9048eda53ecaa901689f0164cba2a389f543df' }, h("div", { key: 'e24db0f74c4d3657bb1ee6903160fcdaf2910da4', class: "pn-select", "data-error": this.hasError() }, this.label && (h("label", { key: 'c4e6c6c613df6392b0a67123d46f9474ae0e305f', htmlFor: this.selectId, class: "pn-select-label" }, h("span", { key: '58965840227667a57d624538702990125c5f7dca' }, this.label))), h("div", { key: 'd48cef7cd8ceb7cc7fafce1da20574d933d9413f', class: "pn-select-input" }, this.icon && h("pn-icon", { key: 'f4021fd781fb67e2df267606b165607369fd0371', class: "pn-select-icon", icon: this.icon, "data-custom": true, "aria-hidden": "true" }), this.hasError() && (h("pn-icon", { key: 'e7d35d9f8191aec7c0ced7442b2419c9dc88fb44', class: "pn-select-icon", icon: alert_exclamation_circle, color: "warning", "data-error": true, "aria-hidden": "true" })), h("select", { key: '1b2bd070a7d12e7504bfddfb389d1cb3e9a9053f', 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: 'b9d68ad6844d3c0444f4a4c0f53e7f4c98ce41f7' })), h("pn-icon", { key: '66bc82e862821f70b7f62559c00a9937301b3715', class: "pn-select-icon", icon: angle_down, color: "blue700", "data-default": true, "aria-hidden": "true" })), this.hasMessage() && (h("p", { key: '100c25db7cb319a1ae7389bc862ffbcdb6b8f6d3', id: this.idHelpertext, class: "pn-select-helper", role: !!this.error ? 'alert' : null }, h("span", { key: '73a0373c059ee9d0ec9abd4bcbf99a48409d8112' }, 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