UNPKG

@postnord/web-components

Version:

PostNord Web Components

485 lines (484 loc) 17.7 kB
/*! * Built with Stencil * By PostNord. */ import { Host, h } from "@stencil/core"; import { uuidv4 } from "../../../globals/helpers"; /** * The `pn-textarea` is built with a native `textarea`. * This means you can use native events to listen to the input. * * @nativeInput Use the `input` event to listen to content being modified by the user. */ export class PnTextarea { constructor() { this.content = undefined; this.label = undefined; this.value = ''; this.helpertext = undefined; this.textareaid = this.id; this.name = undefined; this.form = undefined; this.rows = 2; this.cols = 20; this.wrap = 'soft'; this.maxlength = undefined; this.autocomplete = 'off'; this.spellcheck = false; this.placeholder = undefined; this.resize = false; this.required = false; this.valid = false; this.invalid = false; this.error = undefined; this.disabled = false; this.readonly = false; } id = `pn-textarea-${uuidv4()}`; idHelper = `${this.id}-text`; hostElement; componentWillLoad() { if (this.textareaid !== this.id) { this.idHelper = `${this.textareaid}-helper`; } } setVal(event) { const value = event.target.value; this.value = value; } hasError() { return this.invalid || !!this.error; } hasMessage() { return !!(this.helpertext?.length || this.error?.length); } render() { return (h(Host, { key: 'c8cd0f74604646f7ac3ed6e8d4e7846b6d818b02' }, h("div", { key: '48fb0a2c6c30968079633a36b5d6a85ee8a07366', class: "pn-textarea", "data-valid": this.valid, "data-error": this.hasError(), "data-resize": this.resize }, h("label", { key: 'fbe7eac959225c7ee7d2a13fc3bdc75aca42363a', class: "pn-textarea-label", htmlFor: this.textareaid }, h("span", { key: '6d433af86539e7bb9b0bdd673f33f3297f79aa69' }, this.label), this.maxlength >= 1 && h("span", { key: '12e7c0a22af8e92136d7c893685d80e741d0c9f1' }, `${this.value?.length || 0}/${this.maxlength}`)), h("textarea", { key: 'c7dca374bf36de54afc9e2183ddb95c0c78c9f61', class: "pn-textarea-element", id: this.textareaid, name: this.name, rows: this.rows, cols: this.cols, wrap: this.wrap, autocomplete: this.autocomplete, spellcheck: this.spellcheck, placeholder: this.placeholder, maxlength: this.maxlength, required: this.required, disabled: this.disabled, readonly: this.readonly, "aria-describedby": this.hasMessage() ? this.idHelper : null, "aria-invalid": this.hasError().toString(), onInput: e => this.setVal(e), value: this.value }), this.hasMessage() && (h("p", { key: '9a5c29b6db59b4b881fe4b76f3cfd6dd69d79249', id: this.idHelper, class: "pn-textarea-text", role: this.error?.length ? 'alert' : null }, h("span", { key: '25a43d7f4ba47550623e4937ae4b8bf830fda6c0' }, this.error || this.helpertext)))))); } static get is() { return "pn-textarea"; } static get originalStyleUrls() { return { "$": ["pn-textarea.scss"] }; } static get styleUrls() { return { "$": ["pn-textarea.css"] }; } static get properties() { return { "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The label describing the textarea." }, "attribute": "label", "reflect": false }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The textarea content, do not provide slotted content." }, "attribute": "value", "reflect": false, "defaultValue": "''" }, "helpertext": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Helper text that will appear underneath the textarea. Will be overwritten if you set an `error` string" }, "attribute": "helpertext", "reflect": false }, "textareaid": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "A unique ID connecting the element to the label." }, "attribute": "textareaid", "reflect": false, "defaultValue": "this.id" }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "HTML name." }, "attribute": "name", "reflect": false }, "form": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Set the associated form." }, "attribute": "form", "reflect": false }, "rows": { "type": "number", "mutable": false, "complexType": { "original": "number", "resolved": "number", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Set the row count for the textarea." }, "attribute": "rows", "reflect": false, "defaultValue": "2" }, "cols": { "type": "number", "mutable": false, "complexType": { "original": "number", "resolved": "number", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Set the col count for the textarea." }, "attribute": "cols", "reflect": false, "defaultValue": "20" }, "wrap": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Set the wrap control." }, "attribute": "wrap", "reflect": false, "defaultValue": "'soft'" }, "maxlength": { "type": "number", "mutable": false, "complexType": { "original": "number", "resolved": "number", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "The maximum number of characters the user should be able to add, also adds a visible counter." }, "attribute": "maxlength", "reflect": false }, "autocomplete": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Allow the browser to autocomplete the textarea." }, "attribute": "autocomplete", "reflect": false, "defaultValue": "'off'" }, "spellcheck": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Allow the browser to spellcheck the textarea." }, "attribute": "spellcheck", "reflect": false, "defaultValue": "false" }, "placeholder": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Placeholder for the textarea." }, "attribute": "placeholder", "reflect": false }, "resize": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Allow the user to resize the textarea vertically. Handle width in your own layout." }, "attribute": "resize", "reflect": false, "defaultValue": "false" }, "required": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "HTML Textarea" }], "text": "Make the textarea required." }, "attribute": "required", "reflect": false, "defaultValue": "false" }, "valid": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Set the textarea as `valid`, will make the Label and focus rings green." }, "attribute": "valid", "reflect": false, "defaultValue": "false" }, "invalid": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Set the textarea as `invalid`, will make the Label and focus rings red." }, "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": "State" }], "text": "Same as `invalid`, but will display the string as an error message under the textarea." }, "attribute": "error", "reflect": false }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Disable the textarea." }, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "readonly": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "State" }], "text": "Make the textarea readonly." }, "attribute": "readonly", "reflect": false, "defaultValue": "false" } }; } static get states() { return { "content": {} }; } static get elementRef() { return "hostElement"; } } //# sourceMappingURL=pn-textarea.js.map