@postnord/web-components
Version:
PostNord Web Components
485 lines (484 loc) • 17.7 kB
JavaScript
/*!
* 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: '5a142dbaa2f6ea08d868668bf3f12e43be23f935' }, h("div", { key: '2858929a5c5daedaa477dcb720a232e63e3c0c85', class: "pn-textarea", "data-valid": this.valid, "data-error": this.hasError(), "data-resize": this.resize }, h("label", { key: 'ea30152404c7dacafbac4f7bbede2a5e65245ef1', class: "pn-textarea-label", htmlFor: this.textareaid }, h("span", { key: 'd095e7a14076c808d6ae13684e5961ae74ce11e6' }, this.label), this.maxlength >= 1 && h("span", { key: 'd689bbda3d391099285f7d1609668647935e5f13' }, `${this.value?.length || 0}/${this.maxlength}`)), h("textarea", { key: 'e38bea77a771e20d6318afa5481fb3fe78063952', 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: '5cf140710137d0ff3a843e0435b1c6551ee12a13', id: this.idHelper, class: "pn-textarea-text", role: this.error?.length ? 'alert' : null }, h("span", { key: 'ea36ceb356fd1a985c9ef9cad9673b11a38a598f' }, 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