UNPKG

@postnord/web-components

Version:

PostNord Web Components

251 lines (250 loc) 9.61 kB
/*! * Built with Stencil * By PostNord. */ import { forceUpdate, Host, h } from "@stencil/core"; import { uuidv4 } from "../../../globals/helpers"; import { alert_exclamation_circle, alert_info_circle } from "pn-design-assets/pn-assets/icons.js"; /** * The fielset is a wrapper to give context and information about inputs. Most commonly, a set of radio/checkboxes. * However, you may include any type of input such as textareas, toggles and selects. */ export class PnFieldset { mo; id = `pn-fieldset-${uuidv4()}`; idHelpertext = `${this.id}-helpertext`; hostElement; /** * The legend provides a caption for the fieldset group. * This should be a question or heading that describes the inputs/fields. */ legend; /** Text message underneath the legend for more instruction or hints on how to fill in controls correctly. */ helpertext; /** Give the fieldset a custom HTML id. */ fieldsetId = this.id; /** * The id of the form you want the fieldset to be part of. * If you want the inputs inside to be associated with a specific form, you need to set on those elements directly. */ form; /** Give the fieldset a HTML name. */ name; /** Disable all of the inputs inside of the fieldset. */ disabled = false; /** Set the fieldset as `invalid`, will add red error styles. */ invalid = false; /** Error message, applies warning styles and icon. */ error; handleId() { this.idHelpertext = `${this.fieldsetId}-helpertext`; } componentWillLoad() { this.handleId(); } componentDidLoad() { if (this.mo) this.mo.disconnect(); this.mo = new MutationObserver(() => forceUpdate(this.hostElement)); this.mo.observe(this.hostElement.querySelector('.pn-fieldset'), { childList: true, subtree: true }); } componentDidRender() { /** * Stencil has a known problem about rendering the `form` attribute. * Setting it via setAttribute solves it for now. * https://github.com/ionic-team/stencil/issues/2703 **/ if (this.form) { this.hostElement.querySelector('fieldset')?.setAttribute('form', this.form); } } hasError() { return this.invalid || !!this.error; } getSymbol() { if (this.disabled) return alert_info_circle; return alert_exclamation_circle; } render() { return (h(Host, { key: '2f9bd47d141fe715a0550ec9bdea9139a4e30cb8' }, h("fieldset", { key: 'c72ea4f3ca1336bd0ed0ba3cdf20771d3394aad1', id: this.fieldsetId, class: "pn-fieldset", disabled: this.disabled, "aria-describedby": this.helpertext && this.idHelpertext, "data-error": this.hasError() }, h("legend", { key: 'b1cb227f4638fe50f2a062d8b2e2b26f0bbddfe7', class: "pn-fieldset-legend" }, this.legend), this.helpertext && (h("p", { key: '7b67268a4fd7c135808d3b64236ff3e287d06aa2', id: this.idHelpertext, class: "pn-fieldset-helpertext" }, this.helpertext)), h("div", { key: '6bd3c274636fd1394b1695371905127d9b79112f', class: "pn-fieldset-items" }, h("slot", { key: 'd9bc4fd16c21bc2871c0374d3feffe025ff83b7f' })), h("div", { key: 'a42a9e9bda1e906a90724fe9ad97044f5563e80c', class: "pn-fieldset-message-wrapper", role: "alert", hidden: !this.error }, h("pn-icon", { key: '5e48f24beece808f5f24ca04152fdd4f6bceb082', icon: this.getSymbol() }), h("p", { key: '711b7ce505b5e11e62d397952cd62c49c6d53c3c', class: "pn-fieldset-message" }, this.error))))); } static get is() { return "pn-fieldset"; } static get originalStyleUrls() { return { "$": ["pn-fieldset.scss"] }; } static get styleUrls() { return { "$": ["pn-fieldset.css"] }; } static get properties() { return { "legend": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The legend provides a caption for the fieldset group.\nThis should be a question or heading that describes the inputs/fields." }, "getter": false, "setter": false, "attribute": "legend", "reflect": false }, "helpertext": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Text message underneath the legend for more instruction or hints on how to fill in controls correctly." }, "getter": false, "setter": false, "attribute": "helpertext", "reflect": false }, "fieldsetId": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Give the fieldset a custom HTML id." }, "getter": false, "setter": false, "attribute": "fieldset-id", "reflect": false, "defaultValue": "this.id" }, "form": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The id of the form you want the fieldset to be part of.\nIf you want the inputs inside to be associated with a specific form, you need to set on those elements directly." }, "getter": false, "setter": false, "attribute": "form", "reflect": false }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Give the fieldset a HTML name." }, "getter": false, "setter": false, "attribute": "name", "reflect": false }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Disable all of the inputs inside of the fieldset." }, "getter": false, "setter": false, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "invalid": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Set the fieldset as `invalid`, will add red error styles." }, "getter": false, "setter": false, "attribute": "invalid", "reflect": false, "defaultValue": "false" }, "error": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Error message, applies warning styles and icon." }, "getter": false, "setter": false, "attribute": "error", "reflect": false } }; } static get elementRef() { return "hostElement"; } static get watchers() { return [{ "propName": "fieldsetId", "methodName": "handleId" }]; } } //# sourceMappingURL=pn-fieldset.js.map