@trimble-oss/moduswebcomponents
Version:
Modus Web Components is a modern, accessible UI library built with Stencil JS that provides reusable web components following Trimble's Modus design system. This updated version focuses on improved flexibility, enhanced theming options, comprehensive cust
683 lines (682 loc) • 26.8 kB
JavaScript
import { h, Host, } from "@stencil/core";
import { convertPropsToClasses } from "./modus-wc-text-input.tailwind";
import { CloseSolidIcon } from "../../icons/close-solid.icon";
import { SearchSolidIcon } from "../../icons/search-solid.icon";
import { inheritAriaAttributes, inheritAttributes } from "../utils";
/**
* A customizable input component used to create text inputs with types.
*/
export class ModusWcTextInput {
constructor() {
this.inheritedAttributes = {};
/** Indicates that the input should have a border. */
this.bordered = true;
/** Aria label for the clear icon button. */
this.clearAriaLabel = 'Clear text';
/** Custom CSS class to apply to the input. */
this.customClass = '';
/** Whether the form control is disabled. */
this.disabled = false;
/** Show the clear button within the input field. */
this.includeClear = false;
/** Show the search icon within the input field. */
this.includeSearch = false;
/**
* Hints at the type of data that might be entered by the user while editing the element or its contents.
* This allows a browser to display an appropriate virtual keyboard.
*/
this.inputMode = 'text';
/** Text that appears in the form control when it has no value set. */
this.placeholder = '';
/** Whether the value is editable. */
this.readOnly = false;
/** A value is required for the form to be submittable. */
this.required = false;
/** The size of the input. */
this.size = 'md';
/** Type of form control. */
this.type = 'text';
/** The value of the control. */
this.value = '';
this.handleBlur = (event) => {
this.inputBlur.emit(event);
};
this.handleClearText = (event) => {
this.value = '';
this.inputChange.emit(event);
};
this.handleFocus = (event) => {
this.inputFocus.emit(event);
};
this.handleInput = (event) => {
this.value = event.target.value;
this.inputChange.emit(event);
};
}
componentWillLoad() {
if (!this.el.ariaLabel) {
this.el.ariaLabel = this.placeholder || 'Text input';
}
this.inheritedAttributes = Object.assign(Object.assign({}, inheritAriaAttributes(this.el)), inheritAttributes(this.el, ['spellcheck']));
}
getClasses() {
const classList = [
'modus-wc-text-input',
'modus-wc-input',
'modus-wc-w-full',
'modus-wc-flex',
'modus-wc-items-center',
'modus-wc-gap-1',
];
const propClasses = convertPropsToClasses({
bordered: this.bordered,
disabled: this.disabled,
feedback: this.feedback,
readOnly: this.readOnly,
size: this.size,
});
// The order CSS classes are added matters to CSS specificity
if (propClasses)
classList.push(propClasses);
if (this.customClass)
classList.push(this.customClass);
return classList.join(' ');
}
shouldIncludeClear() {
return (!!this.includeClear && !this.disabled && !this.readOnly && !!this.value);
}
render() {
const showClear = this.shouldIncludeClear();
return (h(Host, { key: '67083501fce3d5d0ffc136c8ef9d5ecbc84bc89b' }, this.label && (h("modus-wc-input-label", { key: 'fc96bc443d4246620f5f1c85d046e6f4a7f50917', forId: this.inputId, labelText: this.label, required: this.required, size: this.size })), h("label", { key: '4f83b07d3847e0ae0a34c1c645d7c163bde2da92', class: this.getClasses() }, this.includeSearch && (h(SearchSolidIcon, { key: '3151ec9863d28fa62043ecbcb8cb40ecf69083bb', className: "modus-wc-text-input-icon modus-wc-text-input-icon-search" })), h("input", Object.assign({ key: '6a0b561925bbe100eec8d85bf94b66fc47183260', "aria-placeholder": this.placeholder, "aria-required": this.required, autocapitalize: this.autoCapitalize, autocomplete: this.autoComplete, autocorrect: this.autoCorrect, class: "modus-wc-grow", disabled: this.disabled, enterkeyhint: this.enterkeyhint, id: this.inputId, inputmode: this.inputMode, maxlength: this.maxLength, minlength: this.minLength, name: this.name, onBlur: this.handleBlur, onFocus: this.handleFocus, onInput: this.handleInput, pattern: this.pattern, placeholder: this.placeholder, readonly: this.readOnly, required: this.required, tabIndex: this.inputTabIndex, type: this.type, value: this.value }, this.inheritedAttributes)), this.includeClear && (h("div", { key: '340410881c6ff48765eb25000815c962da835550', class: `modus-wc-clear-icon-container ${showClear ? 'modus-wc-clear-icon-visible' : 'modus-wc-clear-icon-hidden'}` }, h(CloseSolidIcon, { key: 'e944f8a2dd0444ca6944bc57aec3f89497ae67fe', ariaLabel: this.clearAriaLabel, className: "modus-wc-text-input-icon modus-wc-text-input-icon-clear", decorative: false, onClear: this.handleClearText })))), this.feedback && (h("modus-wc-input-feedback", { key: '8825e88f6cbac14ad072c0988a05604426e1827b', level: this.feedback.level, message: this.feedback.message, size: this.size }))));
}
static get is() { return "modus-wc-text-input"; }
static get originalStyleUrls() {
return {
"$": ["modus-wc-text-input.scss"]
};
}
static get styleUrls() {
return {
"$": ["modus-wc-text-input.css"]
};
}
static get properties() {
return {
"autoCapitalize": {
"type": "string",
"attribute": "auto-capitalize",
"mutable": false,
"complexType": {
"original": "| 'off'\n | 'none'\n | 'on'\n | 'sentences'\n | 'words'\n | 'characters'",
"resolved": "\"characters\" | \"none\" | \"off\" | \"on\" | \"sentences\" | \"words\" | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Controls automatic capitalization in inputted text."
},
"getter": false,
"setter": false,
"reflect": false
},
"autoComplete": {
"type": "string",
"attribute": "auto-complete",
"mutable": false,
"complexType": {
"original": "AutocompleteTypes",
"resolved": "AutocompleteTypes | undefined",
"references": {
"AutocompleteTypes": {
"location": "import",
"path": "../types",
"id": "src/components/types.ts::AutocompleteTypes"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Hint for form autofill feature."
},
"getter": false,
"setter": false,
"reflect": false
},
"autoCorrect": {
"type": "string",
"attribute": "auto-correct",
"mutable": false,
"complexType": {
"original": "'on' | 'off'",
"resolved": "\"off\" | \"on\" | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Controls automatic correction in inputted text. Support by browser varies."
},
"getter": false,
"setter": false,
"reflect": false
},
"bordered": {
"type": "boolean",
"attribute": "bordered",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Indicates that the input should have a border."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "true"
},
"clearAriaLabel": {
"type": "string",
"attribute": "clear-aria-label",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Aria label for the clear icon button."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'Clear text'"
},
"customClass": {
"type": "string",
"attribute": "custom-class",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Custom CSS class to apply to the input."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "''"
},
"disabled": {
"type": "boolean",
"attribute": "disabled",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Whether the form control is disabled."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "false"
},
"enterkeyhint": {
"type": "string",
"attribute": "enterkeyhint",
"mutable": false,
"complexType": {
"original": "| 'enter'\n | 'done'\n | 'go'\n | 'next'\n | 'previous'\n | 'search'\n | 'send'",
"resolved": "\"done\" | \"enter\" | \"go\" | \"next\" | \"previous\" | \"search\" | \"send\" | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "A hint to the browser for which enter key to display."
},
"getter": false,
"setter": false,
"reflect": false
},
"feedback": {
"type": "unknown",
"attribute": "feedback",
"mutable": false,
"complexType": {
"original": "IInputFeedbackProp",
"resolved": "IInputFeedbackProp | undefined",
"references": {
"IInputFeedbackProp": {
"location": "import",
"path": "../types",
"id": "src/components/types.ts::IInputFeedbackProp"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Feedback to render below the input."
},
"getter": false,
"setter": false
},
"includeClear": {
"type": "boolean",
"attribute": "include-clear",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Show the clear button within the input field."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "false"
},
"includeSearch": {
"type": "boolean",
"attribute": "include-search",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Show the search icon within the input field."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "false"
},
"inputId": {
"type": "string",
"attribute": "input-id",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The ID of the input element."
},
"getter": false,
"setter": false,
"reflect": false
},
"inputMode": {
"type": "string",
"attribute": "input-mode",
"mutable": false,
"complexType": {
"original": "| 'decimal'\n | 'email'\n | 'none'\n | 'numeric'\n | 'search'\n | 'tel'\n | 'text'\n | 'url'",
"resolved": "\"decimal\" | \"email\" | \"none\" | \"numeric\" | \"search\" | \"tel\" | \"text\" | \"url\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Hints at the type of data that might be entered by the user while editing the element or its contents.\nThis allows a browser to display an appropriate virtual keyboard."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'text'"
},
"inputTabIndex": {
"type": "number",
"attribute": "input-tab-index",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Determine the control's relative ordering for sequential focus navigation (typically with the Tab key)."
},
"getter": false,
"setter": false,
"reflect": false
},
"label": {
"type": "string",
"attribute": "label",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The text to display within the label."
},
"getter": false,
"setter": false,
"reflect": false
},
"maxLength": {
"type": "number",
"attribute": "max-length",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Maximum length (number of characters) of value."
},
"getter": false,
"setter": false,
"reflect": false
},
"minLength": {
"type": "number",
"attribute": "min-length",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Minimum length (number of characters) of value."
},
"getter": false,
"setter": false,
"reflect": false
},
"name": {
"type": "string",
"attribute": "name",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Name of the form control. Submitted with the form as part of a name/value pair."
},
"getter": false,
"setter": false,
"reflect": false
},
"pattern": {
"type": "string",
"attribute": "pattern",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Pattern the value must match to be valid"
},
"getter": false,
"setter": false,
"reflect": false
},
"placeholder": {
"type": "string",
"attribute": "placeholder",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Text that appears in the form control when it has no value set."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "''"
},
"readOnly": {
"type": "boolean",
"attribute": "read-only",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Whether the value is editable."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "false"
},
"required": {
"type": "boolean",
"attribute": "required",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean | undefined",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "A value is required for the form to be submittable."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "false"
},
"size": {
"type": "string",
"attribute": "size",
"mutable": false,
"complexType": {
"original": "ModusSize",
"resolved": "\"lg\" | \"md\" | \"sm\" | undefined",
"references": {
"ModusSize": {
"location": "import",
"path": "../types",
"id": "src/components/types.ts::ModusSize"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The size of the input."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'md'"
},
"type": {
"type": "string",
"attribute": "type",
"mutable": false,
"complexType": {
"original": "TextFieldTypes",
"resolved": "\"date\" | \"datetime-local\" | \"email\" | \"month\" | \"number\" | \"password\" | \"search\" | \"tel\" | \"text\" | \"time\" | \"url\" | \"week\" | undefined",
"references": {
"TextFieldTypes": {
"location": "import",
"path": "../types",
"id": "src/components/types.ts::TextFieldTypes"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Type of form control."
},
"getter": false,
"setter": false,
"reflect": false,
"defaultValue": "'text'"
},
"value": {
"type": "string",
"attribute": "value",
"mutable": true,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The value of the control."
},
"getter": false,
"setter": false,
"reflect": true,
"defaultValue": "''"
}
};
}
static get events() {
return [{
"method": "inputBlur",
"name": "inputBlur",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Event emitted when the input loses focus."
},
"complexType": {
"original": "FocusEvent",
"resolved": "FocusEvent",
"references": {
"FocusEvent": {
"location": "global",
"id": "global::FocusEvent"
}
}
}
}, {
"method": "inputChange",
"name": "inputChange",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Event emitted when the input value changes."
},
"complexType": {
"original": "InputEvent",
"resolved": "InputEvent",
"references": {
"InputEvent": {
"location": "global",
"id": "global::InputEvent"
}
}
}
}, {
"method": "inputFocus",
"name": "inputFocus",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Event emitted when the input gains focus."
},
"complexType": {
"original": "FocusEvent",
"resolved": "FocusEvent",
"references": {
"FocusEvent": {
"location": "global",
"id": "global::FocusEvent"
}
}
}
}];
}
static get elementRef() { return "el"; }
}