UNPKG

@postnord/web-components

Version:
90 lines (86 loc) 6.72 kB
/*! * Built with Stencil * By PostNord. */ import { t as transformTag, r as registerInstance, g as getElement, f as forceUpdate, h, a as Host } from './index-XKg-ydzH.js'; import { uuidv4 } from './index.js'; import { a as alert_exclamation_circle } from './alert_exclamation_circle-B88w-Zxn.js'; import { a as alert_info_circle } from './alert_info_circle-BTqkgfeH.js'; const pnFieldsetCss = () => `${transformTag("pn-fieldset")}{display:inline-block}${transformTag("pn-fieldset")} .pn-fieldset{border:0;padding:0;margin:0;color:#2d2013;display:flex;flex-direction:column;font-size:1em}${transformTag("pn-fieldset")} .pn-fieldset-legend{float:left;font-weight:500;line-height:1.4;padding:0}${transformTag("pn-fieldset")} .pn-fieldset-message{color:#5e554a;font-size:0.875em;font-weight:400;margin:0.25em 0 0 0;display:flex;flex-direction:column;gap:0.25em}${transformTag("pn-fieldset")} .pn-fieldset-message[hidden]{display:none}${transformTag("pn-fieldset")} .pn-fieldset-error{display:flex;flex-direction:row;flex-wrap:nowrap;gap:0.25em}${transformTag("pn-fieldset")} .pn-fieldset-error[hidden]{display:none}${transformTag("pn-fieldset")} .pn-fieldset-error>.pn-fieldset-message{color:#a70707}${transformTag("pn-fieldset")} .pn-fieldset-items{padding:0.75em 0;display:flex;flex-direction:column;gap:0.75em}${transformTag("pn-fieldset")} .pn-fieldset[aria-invalid=true]>.pn-fieldset-legend,${transformTag("pn-fieldset")} .pn-fieldset[aria-invalid=true]>.pn-fieldset-message-wrapper{color:#a70707}${transformTag("pn-fieldset")} .pn-fieldset[aria-invalid=true]>.pn-fieldset-legend>${transformTag("pn-icon")} .pn-icon-svg>path,${transformTag("pn-fieldset")} .pn-fieldset[aria-invalid=true]>.pn-fieldset-message-wrapper>${transformTag("pn-icon")} .pn-icon-svg>path{fill:#a70707}${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-legend,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-helpertext,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-message,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-message-wrapper{color:#2d2013}${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-legend>${transformTag("pn-icon")} .pn-icon-svg>path,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-helpertext>${transformTag("pn-icon")} .pn-icon-svg>path,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-message>${transformTag("pn-icon")} .pn-icon-svg>path,${transformTag("pn-fieldset")} .pn-fieldset:disabled>.pn-fieldset-message-wrapper>${transformTag("pn-icon")} .pn-icon-svg>path{fill:#2d2013}`; const PnFieldset = class { constructor(hostRef) { registerInstance(this, hostRef); } mo; id = `pn-fieldset-${uuidv4()}`; idHelpertext = `${this.id}-helpertext`; get hostElement() { return getElement(this); } /** * 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 HTML name. @category Native attributes */ name; /** * 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. * @category Native attributes */ form; /** Disable all of the inputs inside of the fieldset. @category Native attributes */ disabled = false; /** Set the fieldset as `invalid`, will add red error styles. @category Features */ invalid = false; /** Error message, applies warning styles and icon. @category Features */ error; /** Set a custom ID for the fieldset. @since v7.25.0 @category HTML attributes */ pnId = this.id; /** Set a custom ID for the fieldset. @deprecated Use `pn-id` instead. @category HTML attributes */ fieldsetId = this.id; handleId() { this.idHelpertext = `${this.getId()}-helpertext`; } connectedCallback() { this.mo = new MutationObserver(() => forceUpdate(this.hostElement)); this.mo.observe(this.hostElement, { childList: true, subtree: true }); } disconnectedCallback() { if (this.mo) this.mo.disconnect(); } getId() { return this.pnId || this.fieldsetId || this.id; } hasHelpertext() { const helpertextSlot = this.hostElement.querySelector('[slot="helpertext"]'); return !!this.helpertext || helpertextSlot?.textContent?.trim().length > 0; } hasError() { const errorSlot = this.hostElement.querySelector('[slot="error"]'); return !!this.error || errorSlot?.textContent?.trim().length > 0; } isInvalid() { return this.invalid || this.hasError(); } getIcon() { if (this.disabled) return alert_info_circle; return alert_exclamation_circle; } render() { return (h(Host, { key: '908a403f3fb8b4779393f4ca0a4f17f518ba53a7' }, h("fieldset", { key: '97b321f776944a0d27f1b1096a967b2c9d9c9fcd', id: this.getId(), name: this.name, form: this.form, class: "pn-fieldset", disabled: this.disabled, "aria-describedby": this.hasHelpertext() ? this.idHelpertext : undefined, "aria-invalid": this.isInvalid().toString() }, h("legend", { key: 'a230416f33f787624f90977054c6492f1516dba6', class: "pn-fieldset-legend" }, this.legend), h("p", { key: '6911c6e63588c36bc0e314d7b3c8b81df2282579', id: this.idHelpertext, class: "pn-fieldset-message", hidden: !this.hasHelpertext() }, h("span", { key: '3370e13a7cf93741a4aa76880533b11c320aea03' }, this.helpertext), h("slot", { key: '0365489d121f3fcfa1211e3c2dcf2881f849940d', name: "helpertext" })), h("div", { key: '23d806e61d39d509ad07f0f68c002a1962332bf4', class: "pn-fieldset-items" }, h("slot", { key: 'a2ead121001e5f751f779957fbbb44716e64274c' })), h("div", { key: '075c709c13a8363be1b1a0b661e3621419a69333', class: "pn-fieldset-error", role: "alert", hidden: !this.hasError() }, h("pn-icon", { key: '0c4e969436033de98814135853e51388862e0812', icon: this.getIcon(), color: "warning" }), h("p", { key: 'd94095bee0a7e4fdc3962db9666dd4ffba7dfa50', class: "pn-fieldset-message" }, h("span", { key: '0c3808a66242f11afdc1ea5053517e965eeb3a93' }, this.error), h("slot", { key: 'ad9604de305ce00ef164cc999562707e923bdd83', name: "error" })))))); } static get watchers() { return { "fieldsetId": [{ "handleId": 0 }], "pnId": [{ "handleId": 1 }] }; } }; PnFieldset.style = pnFieldsetCss(); export { PnFieldset as pn_fieldset };