@postnord/web-components
Version:
PostNord Web Components
223 lines (222 loc) • 8.51 kB
JavaScript
/*!
* 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 {
constructor() {
this.legend = undefined;
this.helpertext = undefined;
this.fieldsetId = this.id;
this.form = undefined;
this.name = undefined;
this.disabled = false;
this.invalid = false;
this.error = undefined;
}
mo;
id = `pn-fieldset-${uuidv4()}`;
idHelpertext = `${this.id}-helpertext`;
hostElement;
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: 'dd2f2178cd03c01266cedab11efff9816a167a33', id: this.fieldsetId, class: "pn-fieldset", disabled: this.disabled, "aria-describedby": this.helpertext && this.idHelpertext, "aria-invalid": this.hasError().toString() }, h("legend", { key: '106f0a8fe2fedd06d196df94820179aacaeaa391', class: "pn-fieldset-legend" }, this.legend), this.helpertext && (h("p", { key: '177d95cccd6991035d5b3f99b447f547dfd8beda', id: this.idHelpertext, class: "pn-fieldset-helpertext" }, this.helpertext)), h("div", { key: '14f4316e932cc34cc191bcba9ad0cc3d0acbfca3', class: "pn-fieldset-items" }, h("slot", { key: '29b725ae5b831261b0935ae32acfd089d646f337' })), h("div", { key: '67f555a9c88d29d6291000147ce7d34cbbc05811', class: "pn-fieldset-message-wrapper", role: "alert", hidden: !this.error }, h("pn-icon", { key: '20b8f4d989aed460c16cf8b4b785563736082a9c', icon: this.getSymbol() }), h("p", { key: '18a46179b5f093d04f22e688aa9a0bcd1f8bda50', 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."
},
"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."
},
"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."
},
"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."
},
"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."
},
"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."
},
"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."
},
"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."
},
"attribute": "error",
"reflect": false
}
};
}
static get elementRef() { return "hostElement"; }
static get watchers() {
return [{
"propName": "fieldsetId",
"methodName": "handleId"
}];
}
}
//# sourceMappingURL=pn-fieldset.js.map