UNPKG

@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

391 lines (390 loc) 14.6 kB
import { h, Host, } from "@stencil/core"; import { convertPropsToClasses } from "./modus-wc-select.tailwind"; import { generateElementId, inheritAriaAttributes } from "../utils"; /** * A customizable select component used to pick a value from a list of options */ export class ModusWcSelect { constructor() { this.inheritedAttributes = {}; this.generatedId = generateElementId(); /** Indicates that the input should have a border. */ this.bordered = true; /** Custom CSS class to apply to the inner div. */ this.customClass = ''; /** Whether the form control is disabled. */ this.disabled = false; /** The options to display in the select dropdown. */ this.options = []; /** A value is required for the form to be submittable. */ this.required = false; /** The size of the input. */ this.size = 'md'; /** The value of the control. */ this.value = ''; this.handleBlur = (event) => { this.inputBlur.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 = 'Select'; } this.inheritedAttributes = inheritAriaAttributes(this.el); } getClasses() { const classList = ['modus-wc-select', 'modus-wc-w-full']; const propClasses = convertPropsToClasses({ bordered: this.bordered, feedback: this.feedback, 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(' '); } render() { const effectiveId = this.inputId || this.generatedId; return (h(Host, { key: '39e94da2f1c0f27c14d97a0594de1ed62ee57302' }, this.label && (h("modus-wc-input-label", { key: 'b9f1cd78d1de0de76caa93a450239a2851ccba7c', forId: effectiveId, labelText: this.label, required: this.required, size: this.size })), h("select", Object.assign({ key: '2b3af54e4bb1a1c7d408618537c49aa0c1d9bf29', class: this.getClasses(), disabled: this.disabled, id: effectiveId, name: this.name, onBlur: this.handleBlur, onFocus: this.handleFocus, onInput: this.handleInput, required: this.required, tabindex: this.inputTabIndex }, this.inheritedAttributes), this.options.map((option) => (h("option", { disabled: option.disabled, selected: option.value === this.value, value: option.value }, option.label)))), this.feedback && (h("modus-wc-input-feedback", { key: 'a4001e4c07e511c83dc676f26cfd0dbdc31fde28', level: this.feedback.level, message: this.feedback.message, size: this.size })))); } static get is() { return "modus-wc-select"; } static get originalStyleUrls() { return { "$": ["modus-wc-select.scss"] }; } static get styleUrls() { return { "$": ["modus-wc-select.css"] }; } static get properties() { return { "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" }, "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 inner div." }, "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" }, "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 }, "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 }, "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 }, "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 }, "options": { "type": "unknown", "attribute": "options", "mutable": true, "complexType": { "original": "ISelectOption[]", "resolved": "ISelectOption[]", "references": { "ISelectOption": { "location": "local", "path": "/home/runner/work/modus-wc-2.0/modus-wc-2.0/src/components/modus-wc-select/modus-wc-select.tsx", "id": "src/components/modus-wc-select/modus-wc-select.tsx::ISelectOption" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "The options to display in the select dropdown." }, "getter": false, "setter": false, "defaultValue": "[]" }, "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'" }, "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"; } }