UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

665 lines (658 loc) • 40.6 kB
/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.5.0-next.4 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-55f8a3b7.js'); const dom = require('./dom-18ca68ff.js'); const form = require('./form-f9d34433.js'); const interactive = require('./interactive-26294f2c.js'); const key = require('./key-2ce02f02.js'); const label = require('./label-fb3080ea.js'); const loadable = require('./loadable-53f729bb.js'); const locale = require('./locale-fc347462.js'); const observers = require('./observers-83b3999d.js'); const resources = require('./resources-45d84c94.js'); const t9n = require('./t9n-14d528c4.js'); require('./guid-db20443e.js'); require('./browser-28ea2ce1.js'); const CSS = { loader: "loader", clearButton: "clear-button", editingEnabled: "editing-enabled", inlineChild: "inline-child", inputIcon: "icon", prefix: "prefix", suffix: "suffix", numberButtonWrapper: "number-button-wrapper", buttonItemHorizontal: "number-button-item--horizontal", wrapper: "element-wrapper", inputWrapper: "wrapper", actionWrapper: "action-wrapper", resizeIconWrapper: "resize-icon-wrapper", numberButtonItem: "number-button-item" }; const INPUT_TYPE_ICONS = { tel: "phone", password: "lock", email: "email-address", date: "calendar", time: "clock", search: "search" }; const SLOTS = { action: "action" }; const inputCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:block}:host([scale=s]) input,:host([scale=s]) .prefix,:host([scale=s]) .suffix{block-size:1.5rem;padding-inline:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) textarea{block-size:1.5rem;min-block-size:1.5rem}:host([scale=s]) .number-button-wrapper,:host([scale=s]) .action-wrapper calcite-button,:host([scale=s]) .action-wrapper calcite-button button{block-size:1.5rem}:host([scale=s]) input[type=file]{block-size:1.5rem}:host([scale=s]) .clear-button{min-block-size:1.5rem;min-inline-size:1.5rem}:host([scale=s]) textarea{block-size:auto;padding-block:0.25rem;padding-inline:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) input,:host([scale=m]) .prefix,:host([scale=m]) .suffix{block-size:2rem;padding-inline:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) textarea{min-block-size:2rem}:host([scale=m]) .number-button-wrapper,:host([scale=m]) .action-wrapper calcite-button,:host([scale=m]) .action-wrapper calcite-button button{block-size:2rem}:host([scale=m]) input[type=file]{block-size:2rem}:host([scale=m]) .clear-button{min-block-size:2rem;min-inline-size:2rem}:host([scale=m]) textarea{block-size:auto;padding-block:0.5rem;padding-inline:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]) input,:host([scale=l]) .prefix,:host([scale=l]) .suffix{block-size:2.75rem;padding-inline:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) textarea{min-block-size:2.75rem}:host([scale=l]) .number-button-wrapper,:host([scale=l]) .action-wrapper calcite-button,:host([scale=l]) .action-wrapper calcite-button button{block-size:2.75rem}:host([scale=l]) input[type=file]{block-size:2.75rem}:host([scale=l]) .clear-button{min-block-size:2.75rem;min-inline-size:2.75rem}:host([scale=l]) textarea{block-size:auto;padding-block:0.75rem;padding-inline:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([disabled]) textarea{resize:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host textarea,:host input{transition:var(--calcite-animation-timing), block-size 0, outline-offset 0s;-webkit-appearance:none;position:relative;margin:0px;box-sizing:border-box;display:flex;max-block-size:100%;inline-size:100%;max-inline-size:100%;flex:1 1 0%;border-radius:0px;background-color:var(--calcite-ui-foreground-1);font-family:inherit;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-1)}:host input[type=search]::-webkit-search-decoration{-webkit-appearance:none}:host input,:host textarea{border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);color:var(--calcite-ui-text-1)}:host input::placeholder,:host input:-ms-input-placeholder,:host input::-ms-input-placeholder,:host textarea::placeholder,:host textarea:-ms-input-placeholder,:host textarea::-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input:focus,:host textarea:focus{border-color:var(--calcite-ui-brand);color:var(--calcite-ui-text-1)}:host input[readonly],:host textarea[readonly]{background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium)}:host input[readonly]:focus,:host textarea[readonly]:focus{color:var(--calcite-ui-text-1)}:host calcite-icon{color:var(--calcite-ui-text-3)}:host textarea,:host input{outline-color:transparent}:host textarea:focus,:host input:focus{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}:host([status=invalid]) input,:host([status=invalid]) textarea{border-color:var(--calcite-ui-danger)}:host([status=invalid]) input:focus,:host([status=invalid]) textarea:focus{outline:2px solid var(--calcite-ui-danger);outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}:host([scale=s]) .icon{inset-inline-start:0.5rem}:host([scale=m]) .icon{inset-inline-start:0.75rem}:host([scale=l]) .icon{inset-inline-start:1rem}:host([icon][scale=s]) input{padding-inline-start:2rem}:host([icon][scale=m]) input{padding-inline-start:2.5rem}:host([icon][scale=l]) input{padding-inline-start:3.5rem}.element-wrapper{position:relative;order:3;display:inline-flex;flex:1 1 0%;align-items:center}.icon{pointer-events:none;position:absolute;display:block;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.icon,.resize-icon-wrapper{z-index:var(--calcite-app-z-index)}input[type=text]::-ms-clear,input[type=text]::-ms-reveal{display:none;block-size:0px;inline-size:0px}input[type=search]::-webkit-search-decoration,input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-results-button,input[type=search]::-webkit-search-results-decoration,input[type=date]::-webkit-clear-button,input[type=time]::-webkit-clear-button{display:none}.clear-button{pointer-events:initial;order:4;margin:0px;box-sizing:border-box;display:flex;min-block-size:100%;cursor:pointer;align-items:center;justify-content:center;align-self:stretch;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);outline-color:transparent;border-inline-start-width:0px}.clear-button:hover{background-color:var(--calcite-ui-foreground-2);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.clear-button:hover calcite-icon{color:var(--calcite-ui-text-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.clear-button:active{background-color:var(--calcite-ui-foreground-3)}.clear-button:active calcite-icon{color:var(--calcite-ui-text-1)}.clear-button:focus{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}.clear-button:disabled{opacity:var(--calcite-ui-opacity-disabled)}.loader{inset-block-start:1px;inset-inline:1px;pointer-events:none;position:absolute;display:block}.action-wrapper{order:7;display:flex}.prefix,.suffix{box-sizing:border-box;display:flex;block-size:auto;min-block-size:100%;-webkit-user-select:none;user-select:none;align-content:center;align-items:center;overflow-wrap:break-word;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium);line-height:1;color:var(--calcite-ui-text-2)}.prefix{order:2;border-inline-end-width:0px}.suffix{order:5;border-inline-start-width:0px}:host([alignment=start]) textarea,:host([alignment=start]) input{text-align:start}:host([alignment=end]) textarea,:host([alignment=end]) input{text-align:end}:host input[type=number]{-moz-appearance:textfield}:host input[type=number]::-webkit-inner-spin-button,:host input[type=number]::-webkit-outer-spin-button{-webkit-appearance:none;-moz-appearance:textfield;margin:0px}.number-button-wrapper{pointer-events:none;order:6;box-sizing:border-box;display:flex;flex-direction:column;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}:host([number-button-type=vertical]) .wrapper{flex-direction:row;display:flex}:host([number-button-type=vertical]) input,:host([number-button-type=vertical]) textarea{order:2}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=down] calcite-icon{transform:rotate(-90deg)}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=up] calcite-icon{transform:rotate(-90deg)}.number-button-item.number-button-item--horizontal[data-adjustment=down],.number-button-item.number-button-item--horizontal[data-adjustment=up]{order:1;max-block-size:100%;min-block-size:100%;align-self:stretch}.number-button-item.number-button-item--horizontal[data-adjustment=down] calcite-icon,.number-button-item.number-button-item--horizontal[data-adjustment=up] calcite-icon{transform:rotate(90deg)}.number-button-item.number-button-item--horizontal[data-adjustment=down]{border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);border-inline-end-width:0px}.number-button-item.number-button-item--horizontal[data-adjustment=down]:hover{background-color:var(--calcite-ui-foreground-2)}.number-button-item.number-button-item--horizontal[data-adjustment=down]:hover calcite-icon{color:var(--calcite-ui-text-1)}.number-button-item.number-button-item--horizontal[data-adjustment=up]{order:5}.number-button-item.number-button-item--horizontal[data-adjustment=up]:hover{background-color:var(--calcite-ui-foreground-2)}.number-button-item.number-button-item--horizontal[data-adjustment=up]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]:hover{background-color:var(--calcite-ui-foreground-2)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=up]:hover{background-color:var(--calcite-ui-foreground-2)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=up]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]{border-block-start-width:0px}.number-button-item{max-block-size:50%;min-block-size:50%;pointer-events:initial;margin:0px;box-sizing:border-box;display:flex;cursor:pointer;align-items:center;align-self:center;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);padding-block:0px;padding-inline:0.5rem;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;border-inline-start-width:0px}.number-button-item calcite-icon{pointer-events:none;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.number-button-item:focus{background-color:var(--calcite-ui-foreground-2)}.number-button-item:focus calcite-icon{color:var(--calcite-ui-text-1)}.number-button-item:active{background-color:var(--calcite-ui-foreground-3)}.number-button-item:disabled{pointer-events:none}.wrapper{position:relative;display:flex;flex-direction:row;align-items:center}:host input::-webkit-calendar-picker-indicator{display:none}:host input[type=date]::-webkit-input-placeholder{visibility:hidden !important}:host textarea::-webkit-resizer{position:absolute;inset-block-end:0px;box-sizing:border-box;padding-block:0px;padding-inline:0.25rem;inset-inline-end:0}.resize-icon-wrapper{inset-block-end:2px;inset-inline-end:2px;pointer-events:none;position:absolute;block-size:0.75rem;inline-size:0.75rem;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-3)}.resize-icon-wrapper calcite-icon{inset-block-end:0.25rem;inset-inline-end:0.25rem;transform:rotate(-45deg)}.calcite--rtl .resize-icon-wrapper calcite-icon{transform:rotate(45deg)}:host([type=color]) input{padding:0.25rem}:host([type=file]) input{cursor:pointer;border-width:1px;border-style:dashed;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);text-align:center}:host([type=file][scale=s]) input{padding-block:1px;padding-inline:0.5rem}:host([type=file][scale=m]) input{padding-block:0.25rem;padding-inline:0.75rem}:host([type=file][scale=l]) input{padding-block:0.5rem;padding-inline:1rem}:host(.no-bottom-border) input{border-block-end-width:0px}:host(.border-top-color-one) input{border-block-start-color:var(--calcite-ui-border-1)}:host .inline-child{background-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}:host .inline-child .editing-enabled{background-color:inherit}:host .inline-child:not(.editing-enabled){display:flex;cursor:pointer;text-overflow:ellipsis;border-color:transparent;padding-inline-start:0}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}"; const Input = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.calciteInternalInputFocus = index.createEvent(this, "calciteInternalInputFocus", 6); this.calciteInternalInputBlur = index.createEvent(this, "calciteInternalInputBlur", 6); this.calciteInputInput = index.createEvent(this, "calciteInputInput", 7); this.calciteInputChange = index.createEvent(this, "calciteInputChange", 6); /** keep track of the rendered child type */ this.childElType = "input"; this.previousValueOrigin = "initial"; this.mutationObserver = observers.createObserver("mutation", () => this.setDisabledAction()); this.userChangedValue = false; //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- this.keyDownHandler = (event) => { if (this.readOnly || this.disabled) { return; } if (this.isClearable && event.key === "Escape") { this.clearInputValue(event); event.preventDefault(); } if (event.key === "Enter" && !event.defaultPrevented) { if (form.submitForm(this)) { event.preventDefault(); } } }; this.clearInputValue = (nativeEvent) => { this.setValue({ committing: true, nativeEvent, origin: "user", value: "" }); }; this.emitChangeIfUserModified = () => { if (this.previousValueOrigin === "user" && this.value !== this.previousEmittedValue) { this.calciteInputChange.emit(); this.setPreviousEmittedValue(this.value); } }; this.inputBlurHandler = () => { this.calciteInternalInputBlur.emit(); this.emitChangeIfUserModified(); }; this.clickHandler = (event) => { if (this.disabled) { return; } const slottedActionEl = dom.getSlotted(this.el, "action"); if (event.target !== slottedActionEl) { this.setFocus(); } }; this.inputFocusHandler = () => { this.calciteInternalInputFocus.emit(); }; this.inputChangeHandler = () => { if (this.type === "file") { this.files = this.childEl.files; } }; this.inputInputHandler = (nativeEvent) => { if (this.disabled || this.readOnly) { return; } this.setValue({ nativeEvent, origin: "user", value: nativeEvent.target.value }); }; this.inputKeyDownHandler = (event) => { if (this.disabled || this.readOnly) { return; } if (event.key === "Enter") { this.emitChangeIfUserModified(); } }; this.inputNumberInputHandler = (nativeEvent) => { if (this.disabled || this.readOnly) { return; } const value = nativeEvent.target.value; locale.numberStringFormatter.numberFormatOptions = { locale: this.effectiveLocale, numberingSystem: this.numberingSystem, useGrouping: this.groupSeparator }; const delocalizedValue = locale.numberStringFormatter.delocalize(value); if (nativeEvent.inputType === "insertFromPaste") { if (!locale.isValidNumber(delocalizedValue)) { nativeEvent.preventDefault(); } this.setValue({ nativeEvent, origin: "user", value: locale.parseNumberString(delocalizedValue) }); this.childNumberEl.value = this.localizedValue; } else { this.setValue({ nativeEvent, origin: "user", value: delocalizedValue }); } }; this.inputNumberKeyDownHandler = (event) => { if (this.type !== "number" || this.disabled || this.readOnly) { return; } if (event.key === "ArrowUp") { /* prevent default behavior of moving cursor to the beginning of the input when holding down ArrowUp */ event.preventDefault(); this.nudgeNumberValue("up", event); return; } if (event.key === "ArrowDown") { this.nudgeNumberValue("down", event); return; } const supportedKeys = [ ...key.numberKeys, "ArrowLeft", "ArrowRight", "Backspace", "Delete", "Enter", "Escape", "Tab" ]; if (event.altKey || event.ctrlKey || event.metaKey) { return; } const isShiftTabEvent = event.shiftKey && event.key === "Tab"; if (supportedKeys.includes(event.key) && (!event.shiftKey || isShiftTabEvent)) { if (event.key === "Enter") { this.emitChangeIfUserModified(); } return; } locale.numberStringFormatter.numberFormatOptions = { locale: this.effectiveLocale, numberingSystem: this.numberingSystem, useGrouping: this.groupSeparator }; if (event.key === locale.numberStringFormatter.decimal) { if (!this.value && !this.childNumberEl.value) { return; } if (this.value && this.childNumberEl.value.indexOf(locale.numberStringFormatter.decimal) === -1) { return; } } if (/[eE]/.test(event.key)) { if (!this.value && !this.childNumberEl.value) { return; } if (this.value && !/[eE]/.test(this.childNumberEl.value)) { return; } } if (event.key === "-") { if (!this.value && !this.childNumberEl.value) { return; } if (this.value && this.childNumberEl.value.split("-").length <= 2) { return; } } event.preventDefault(); }; this.nudgeNumberValue = (direction, nativeEvent) => { if ((nativeEvent instanceof KeyboardEvent && nativeEvent.repeat) || this.type !== "number") { return; } const inputMax = this.maxString ? parseFloat(this.maxString) : null; const inputMin = this.minString ? parseFloat(this.minString) : null; const valueNudgeDelayInMs = 150; this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent); if (this.nudgeNumberValueIntervalId) { window.clearInterval(this.nudgeNumberValueIntervalId); } let firstValueNudge = true; this.nudgeNumberValueIntervalId = window.setInterval(() => { if (firstValueNudge) { firstValueNudge = false; return; } this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent); }, valueNudgeDelayInMs); }; this.numberButtonPointerUpAndOutHandler = () => { window.clearInterval(this.nudgeNumberValueIntervalId); }; this.numberButtonPointerDownHandler = (event) => { if (!dom.isPrimaryPointerButton(event)) { return; } event.preventDefault(); const direction = event.target.dataset.adjustment; if (!this.disabled) { this.nudgeNumberValue(direction, event); } }; this.hiddenInputChangeHandler = (event) => { if (event.target.name === this.name) { this.setValue({ value: event.target.value, origin: "direct" }); } event.stopPropagation(); }; this.setChildElRef = (el) => { this.childEl = el; }; this.setChildNumberElRef = (el) => { this.childNumberEl = el; }; this.setInputValue = (newInputValue) => { if (this.type === "text" && !this.childEl) { return; } if (this.type === "number" && !this.childNumberEl) { return; } this[`child${this.type === "number" ? "Number" : ""}El`].value = newInputValue; }; this.setPreviousEmittedValue = (value) => { this.previousEmittedValue = this.normalizeValue(value); }; this.setPreviousValue = (value) => { this.previousValue = this.normalizeValue(value); }; this.setValue = ({ committing = false, nativeEvent, origin, previousValue, value }) => { this.setPreviousValue(previousValue ?? this.value); this.previousValueOrigin = origin; if (this.type === "number") { locale.numberStringFormatter.numberFormatOptions = { locale: this.effectiveLocale, numberingSystem: this.numberingSystem, useGrouping: this.groupSeparator, signDisplay: "never" }; const sanitizedValue = locale.sanitizeNumberString( // no need to delocalize a string that ia already in latn numerals (this.numberingSystem && this.numberingSystem !== "latn") || locale.defaultNumberingSystem !== "latn" ? locale.numberStringFormatter.delocalize(value) : value); const newValue = value && !sanitizedValue ? locale.isValidNumber(this.previousValue) ? this.previousValue : "" : sanitizedValue; const newLocalizedValue = locale.numberStringFormatter.localize(newValue); this.localizedValue = newLocalizedValue; this.userChangedValue = origin === "user" && this.value !== newValue; // don't sanitize the start of negative/decimal numbers, but // don't set value to an invalid number this.value = ["-", "."].includes(newValue) ? "" : newValue; } else { this.userChangedValue = origin === "user" && this.value !== value; this.value = value; } if (origin === "direct") { this.setInputValue(value); this.previousEmittedValue = value; } if (nativeEvent) { const calciteInputInputEvent = this.calciteInputInput.emit(); if (calciteInputInputEvent.defaultPrevented) { this.value = this.previousValue; this.localizedValue = this.type === "number" ? locale.numberStringFormatter.localize(this.previousValue) : this.previousValue; } else if (committing) { this.emitChangeIfUserModified(); } } }; this.inputKeyUpHandler = () => { window.clearInterval(this.nudgeNumberValueIntervalId); }; this.alignment = "start"; this.autofocus = false; this.clearable = false; this.disabled = false; this.form = undefined; this.groupSeparator = false; this.hidden = false; this.icon = undefined; this.iconFlipRtl = false; this.label = undefined; this.loading = false; this.numberingSystem = undefined; this.localeFormat = false; this.max = undefined; this.min = undefined; this.maxLength = undefined; this.minLength = undefined; this.name = undefined; this.numberButtonType = "vertical"; this.placeholder = undefined; this.prefixText = undefined; this.readOnly = false; this.required = false; this.scale = "m"; this.status = "idle"; this.step = undefined; this.autocomplete = undefined; this.pattern = undefined; this.accept = undefined; this.multiple = false; this.inputMode = "text"; this.enterKeyHint = undefined; this.suffixText = undefined; this.editingEnabled = false; this.type = "text"; this.value = ""; this.files = undefined; this.messages = undefined; this.messageOverrides = undefined; this.defaultMessages = undefined; this.effectiveLocale = ""; this.localizedValue = undefined; this.slottedActionElDisabledInternally = false; } disabledWatcher() { this.setDisabledAction(); } /** watcher to update number-to-string for max */ maxWatcher() { this.maxString = this.max?.toString() || null; } /** watcher to update number-to-string for min */ minWatcher() { this.minString = this.min?.toString() || null; } onMessagesChange() { /* wired up by t9n util */ } valueWatcher(newValue, previousValue) { if (!this.userChangedValue) { this.setValue({ origin: "direct", previousValue, value: newValue == null || newValue == "" ? "" : this.type === "number" ? locale.isValidNumber(newValue) ? newValue : this.previousValue || "" : newValue }); this.warnAboutInvalidNumberValue(newValue); } this.userChangedValue = false; } updateRequestedIcon() { this.requestedIcon = dom.setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type); } get isClearable() { return !this.isTextarea && (this.clearable || this.type === "search") && this.value.length > 0; } get isTextarea() { return this.childElType === "textarea"; } effectiveLocaleChange() { t9n.updateMessages(this, this.effectiveLocale); } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { interactive.connectInteractive(this); locale.connectLocalized(this); t9n.connectMessages(this); this.inlineEditableEl = this.el.closest("calcite-inline-editable"); if (this.inlineEditableEl) { this.editingEnabled = this.inlineEditableEl.editingEnabled || false; } label.connectLabel(this); form.connectForm(this); this.setPreviousEmittedValue(this.value); this.setPreviousValue(this.value); if (this.type === "number") { this.warnAboutInvalidNumberValue(this.value); this.setValue({ origin: "connected", value: locale.isValidNumber(this.value) ? this.value : "" }); } this.mutationObserver?.observe(this.el, { childList: true }); this.setDisabledAction(); this.el.addEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler); } disconnectedCallback() { interactive.disconnectInteractive(this); label.disconnectLabel(this); form.disconnectForm(this); locale.disconnectLocalized(this); t9n.disconnectMessages(this); this.mutationObserver?.disconnect(); this.el.removeEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler); } async componentWillLoad() { loadable.setUpLoadableComponent(this); this.childElType = this.type === "textarea" ? "textarea" : "input"; this.maxString = this.max?.toString(); this.minString = this.min?.toString(); this.requestedIcon = dom.setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type); await t9n.setUpMessages(this); } componentDidLoad() { loadable.setComponentLoaded(this); } componentShouldUpdate(newValue, oldValue, property) { if (this.type === "number" && property === "value" && newValue && !locale.isValidNumber(newValue)) { this.setValue({ origin: "reset", value: oldValue }); return false; } return true; } componentDidRender() { interactive.updateHostInteraction(this); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** Sets focus on the component. */ async setFocus() { await loadable.componentLoaded(this); if (this.type === "number") { this.childNumberEl?.focus(); } else { this.childEl?.focus(); } } /** Selects the text of the component's `value`. */ async selectText() { if (this.type === "number") { this.childNumberEl?.select(); } else { this.childEl?.select(); } } // TODO: refactor so we don't need to sync the internals in color-picker // https://github.com/Esri/calcite-components/issues/6100 /** @internal */ async internalSyncChildElValue() { if (this.type === "number") { this.childNumberEl.value = this.value; } else { this.childEl.value = this.value; } } onLabelClick() { this.setFocus(); } incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent) { const { value } = this; const adjustment = direction === "up" ? 1 : -1; const inputStep = this.step === "any" ? 1 : Math.abs(this.step || 1); const inputVal = new locale.BigDecimal(value !== "" ? value : "0"); const nudgedValue = inputVal.add(`${inputStep * adjustment}`); const nudgedValueBelowInputMin = () => typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue.subtract(`${inputMin}`).isNegative; const nudgedValueAboveInputMax = () => typeof inputMax === "number" && !isNaN(inputMax) && !nudgedValue.subtract(`${inputMax}`).isNegative; const finalValue = nudgedValueBelowInputMin() ? `${inputMin}` : nudgedValueAboveInputMax() ? `${inputMax}` : nudgedValue.toString(); this.setValue({ committing: true, nativeEvent, origin: "user", value: finalValue }); } onFormReset() { this.setValue({ origin: "reset", value: this.defaultValue }); } syncHiddenFormInput(input) { const { type } = this; input.type = type; if (type === "number") { input.min = this.min?.toString(10) ?? ""; input.max = this.max?.toString(10) ?? ""; } else if (type === "text") { if (this.minLength != null) { input.minLength = this.minLength; } if (this.maxLength != null) { input.maxLength = this.maxLength; } } } setDisabledAction() { const slottedActionEl = dom.getSlotted(this.el, "action"); if (!slottedActionEl) { return; } if (this.disabled) { if (slottedActionEl.getAttribute("disabled") == null) { this.slottedActionElDisabledInternally = true; } slottedActionEl.setAttribute("disabled", ""); } else if (this.slottedActionElDisabledInternally) { slottedActionEl.removeAttribute("disabled"); this.slottedActionElDisabledInternally = false; } } normalizeValue(value) { return this.type === "number" ? (locale.isValidNumber(value) ? value : "") : value; } warnAboutInvalidNumberValue(value) { if (this.type === "number" && value && !locale.isValidNumber(value)) { console.warn(`The specified value "${value}" cannot be parsed, or is out of range.`); } } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- render() { const dir = dom.getElementDir(this.el); const loader = (index.h("div", { class: CSS.loader }, index.h("calcite-progress", { label: this.messages.loading, type: "indeterminate" }))); const inputClearButton = (index.h("button", { "aria-label": this.messages.clear, class: CSS.clearButton, disabled: this.disabled || this.readOnly, onClick: this.clearInputValue, tabIndex: -1, type: "button" }, index.h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : "s" }))); const iconEl = (index.h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: this.scale === "l" ? "m" : "s" })); const isHorizontalNumberButton = this.numberButtonType === "horizontal"; const numberButtonsHorizontalUp = (index.h("button", { "aria-hidden": "true", class: { [CSS.numberButtonItem]: true, [CSS.buttonItemHorizontal]: isHorizontalNumberButton }, "data-adjustment": "up", disabled: this.disabled || this.readOnly, onPointerDown: this.numberButtonPointerDownHandler, onPointerOut: this.numberButtonPointerUpAndOutHandler, onPointerUp: this.numberButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, index.h("calcite-icon", { icon: "chevron-up", scale: this.scale === "l" ? "m" : "s" }))); const numberButtonsHorizontalDown = (index.h("button", { "aria-hidden": "true", class: { [CSS.numberButtonItem]: true, [CSS.buttonItemHorizontal]: isHorizontalNumberButton }, "data-adjustment": "down", disabled: this.disabled || this.readOnly, onPointerDown: this.numberButtonPointerDownHandler, onPointerOut: this.numberButtonPointerUpAndOutHandler, onPointerUp: this.numberButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, index.h("calcite-icon", { icon: "chevron-down", scale: this.scale === "l" ? "m" : "s" }))); const numberButtonsVertical = (index.h("div", { class: CSS.numberButtonWrapper }, numberButtonsHorizontalUp, numberButtonsHorizontalDown)); const prefixText = index.h("div", { class: CSS.prefix }, this.prefixText); const suffixText = index.h("div", { class: CSS.suffix }, this.suffixText); const localeNumberInput = this.type === "number" ? (index.h("input", { accept: this.accept, "aria-label": label.getLabelText(this), autocomplete: this.autocomplete, autofocus: this.autofocus ? true : null, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.enterKeyHint, inputMode: this.inputMode, key: "localized-input", maxLength: this.maxLength, minLength: this.minLength, multiple: this.multiple, name: undefined, onBlur: this.inputBlurHandler, onFocus: this.inputFocusHandler, onInput: this.inputNumberInputHandler, onKeyDown: this.inputNumberKeyDownHandler, onKeyUp: this.inputKeyUpHandler, pattern: this.pattern, placeholder: this.placeholder || "", readOnly: this.readOnly, type: "text", value: this.localizedValue, // eslint-disable-next-line react/jsx-sort-props ref: this.setChildNumberElRef })) : null; const childEl = this.type !== "number" ? [ index.h(this.childElType, { accept: this.accept, "aria-label": label.getLabelText(this), autocomplete: this.autocomplete, autofocus: this.autofocus ? true : null, class: { [CSS.editingEnabled]: this.editingEnabled, [CSS.inlineChild]: !!this.inlineEditableEl }, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.enterKeyHint, inputMode: this.inputMode, max: this.maxString, maxLength: this.maxLength, min: this.minString, minLength: this.minLength, multiple: this.multiple, name: this.name, onBlur: this.inputBlurHandler, onChange: this.inputChangeHandler, onFocus: this.inputFocusHandler, onInput: this.inputInputHandler, onKeyDown: this.inputKeyDownHandler, onKeyUp: this.inputKeyUpHandler, pattern: this.pattern, placeholder: this.placeholder || "", readOnly: this.readOnly, required: this.required ? true : null, step: this.step, tabIndex: this.disabled || (this.inlineEditableEl && !this.editingEnabled) ? -1 : null, type: this.type, value: this.value, // eslint-disable-next-line react/jsx-sort-props ref: this.setChildElRef }), this.isTextarea ? (index.h("div", { class: CSS.resizeIconWrapper }, index.h("calcite-icon", { icon: "chevron-down", scale: this.scale === "l" ? "m" : "s" }))) : null ] : null; return (index.h(index.Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, index.h("div", { class: { [CSS.inputWrapper]: true, [resources.CSS_UTILITY.rtl]: dir === "rtl" } }, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly ? numberButtonsHorizontalDown : null, this.prefixText ? prefixText : null, index.h("div", { class: CSS.wrapper }, localeNumberInput, childEl, this.isClearable ? inputClearButton : null, this.requestedIcon ? iconEl : null, this.loading ? loader : null), index.h("div", { class: CSS.actionWrapper }, index.h("slot", { name: SLOTS.action })), this.type === "number" && this.numberButtonType === "vertical" && !this.readOnly ? numberButtonsVertical : null, this.suffixText ? suffixText : null, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly ? numberButtonsHorizontalUp : null, index.h(form.HiddenFormInputSlot, { component: this })))); } static get assetsDirs() { return ["assets"]; } get el() { return index.getElement(this); } static get watchers() { return { "disabled": ["disabledWatcher"], "max": ["maxWatcher"], "min": ["minWatcher"], "messageOverrides": ["onMessagesChange"], "value": ["valueWatcher"], "icon": ["updateRequestedIcon"], "type": ["updateRequestedIcon"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; Input.style = inputCss; exports.calcite_input = Input;