@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
630 lines (625 loc) • 42.9 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { T as TEXT, b as getSlotted, s as setRequestedIcon, g as getElementProp, a as getElementDir, C as CSS_UTILITY } from './dom.js';
import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label.js';
import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
import { d as delocalizeNumberString, g as getDecimalSeparator, l as localizeNumberString } from './locale.js';
import { i as isValidNumber, p as parseNumberString, s as sanitizeNumberString, n as numberKeys } from './number.js';
import { d as decimalPlaces } from './math.js';
import { c as createObserver } from './observers.js';
import { u as updateHostInteraction } from './interactive.js';
import { d as defineCustomElement$2 } from './icon.js';
import { d as defineCustomElement$1 } from './progress.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 = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:block}:host([scale=s]) input,:host([scale=s]) .prefix,:host([scale=s]) .suffix{height:1.5rem;padding:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) textarea{height:1.5rem;min-height:1.5rem}:host([scale=s]) .number-button-wrapper,:host([scale=s]) .action-wrapper calcite-button,:host([scale=s]) .action-wrapper calcite-button button{height:1.5rem}:host([scale=s]) input[type=file]{height:1.5rem}:host([scale=s]) .clear-button{min-height:1.5rem;min-width:1.5rem}:host([scale=s]) textarea{height:auto;padding-top:0.25rem;padding-bottom:0.25rem;padding-left:0.5rem;padding-right:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) input,:host([scale=m]) .prefix,:host([scale=m]) .suffix{height:2rem;padding:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) textarea{min-height:2rem}:host([scale=m]) .number-button-wrapper,:host([scale=m]) .action-wrapper calcite-button,:host([scale=m]) .action-wrapper calcite-button button{height:2rem}:host([scale=m]) input[type=file]{height:2rem}:host([scale=m]) .clear-button{min-height:2rem;min-width:2rem}:host([scale=m]) textarea{height:auto;padding-top:0.5rem;padding-bottom:0.5rem;padding-left:0.75rem;padding-right:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]) input,:host([scale=l]) .prefix,:host([scale=l]) .suffix{height:2.75rem;padding:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) textarea{min-height:2.75rem}:host([scale=l]) .number-button-wrapper,:host([scale=l]) .action-wrapper calcite-button,:host([scale=l]) .action-wrapper calcite-button button{height:2.75rem}:host([scale=l]) input[type=file]{height:2.75rem}:host([scale=l]) .clear-button{min-height:2.75rem;min-width:2.75rem}:host([scale=l]) textarea{height:auto;padding-top:0.75rem;padding-bottom:0.75rem;padding-left:1rem;padding-right:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) textarea{resize:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host textarea,:host input{-webkit-transition:var(--calcite-animation-timing), height 0s;transition:var(--calcite-animation-timing), height 0s;-webkit-appearance:none;position:relative;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;max-height:100%;width:100%;max-width:100%;-ms-flex:1 1 0%;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:-ms-input-placeholder,:host textarea:-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input::-ms-input-placeholder,:host textarea::-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}: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-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}:host textarea:focus,:host input:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}: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:-2px}: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{-webkit-padding-start:2rem;padding-inline-start:2rem}:host([icon][scale=m]) input{-webkit-padding-start:2.5rem;padding-inline-start:2.5rem}:host([icon][scale=l]) input{-webkit-padding-start:3rem;padding-inline-start:3rem}.element-wrapper{position:relative;-ms-flex-order:3;order:3;display:-ms-inline-flexbox;display:inline-flex;-ms-flex:1 1 0%;flex:1 1 0%;-ms-flex-align:center;align-items:center}.icon{pointer-events:none;position:absolute;z-index:10;display:block;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}input[type=text]::-ms-clear,input[type=text]::-ms-reveal{display:none;height:0px;width: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;-ms-flex-order:4;order:4;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;min-height:100%;cursor:pointer;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;-ms-flex-item-align:stretch;align-self:stretch;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;border-inline-start-width:0px}.clear-button:hover{background-color:var(--calcite-ui-foreground-2);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.clear-button:hover calcite-icon{color:var(--calcite-ui-text-1);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay: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-brand);outline-offset:-2px}.clear-button:disabled{opacity:var(--calcite-ui-opacity-disabled)}.loader{top:1px;left:1px;right:1px;pointer-events:none;position:absolute;display:block}.action-wrapper{-ms-flex-order:7;order:7;display:-ms-flexbox;display:flex}.prefix,.suffix{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;height:auto;min-height:100%;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-ms-flex-line-pack:center;align-content:center;-ms-flex-align: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{-ms-flex-order:2;order:2;border-inline-end-width:0px}.suffix{-ms-flex-order:5;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;-ms-flex-order:6;order:6;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}:host([number-button-type=vertical]) .wrapper{-ms-flex-direction:row;flex-direction:row;display:-ms-flexbox;display:flex}:host([number-button-type=vertical]) input,:host([number-button-type=vertical]) textarea{-ms-flex-order:2;order:2}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=down] calcite-icon{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=up] calcite-icon{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}.number-button-item.number-button-item--horizontal[data-adjustment=down],.number-button-item.number-button-item--horizontal[data-adjustment=up]{-ms-flex-order:1;order:1;max-height:100%;min-height:100%;-ms-flex-item-align:stretch;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{-webkit-transform:rotate(90deg);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]{-ms-flex-order:5;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-top-width:0px}.number-button-item{max-height:50%;min-height:50%;pointer-events:initial;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-align:center;align-items:center;-ms-flex-item-align: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-top:0px;padding-bottom:0px;padding-left:0.5rem;padding-right:0.5rem;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s;border-inline-start-width:0px}.number-button-item calcite-icon{pointer-events:none;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay: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)}.wrapper{position:relative;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;-ms-flex-align:center;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;bottom:0px;-webkit-box-sizing:border-box;box-sizing:border-box;padding-top:0px;padding-bottom:0px;padding-left:0.25rem;padding-right:0.25rem;inset-inline-end:0}.resize-icon-wrapper{bottom:2px;inset-inline-end:2px;pointer-events:none;position:absolute;z-index:10;height:0.75rem;width:0.75rem;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-3)}.resize-icon-wrapper calcite-icon{bottom:0.25rem;inset-inline-end:0.25rem;-webkit-transform:rotate(-45deg);transform:rotate(-45deg)}.calcite--rtl .resize-icon-wrapper calcite-icon{-webkit-transform:rotate(45deg);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-top:1px;padding-bottom:1px;padding-left:0.5rem;padding-right:0.5rem}:host([type=file][scale=m]) input{padding-top:0.25rem;padding-bottom:0.25rem;padding-left:0.75rem;padding-right:0.75rem}:host([type=file][scale=l]) input{padding-top:0.5rem;padding-bottom:0.5rem;padding-left:1rem;padding-right:1rem}:host(.no-bottom-border) input{border-bottom-width:0px}:host(.border-top-color-one) input{border-top-color:var(--calcite-ui-border-1)}:host .inline-child{background-color:transparent;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}:host .inline-child .editing-enabled{background-color:inherit}:host .inline-child:not(.editing-enabled){display:-ms-flexbox;display:flex;cursor:pointer;border-color:transparent;-webkit-padding-start:0;padding-inline-start:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
const Input = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteInputFocus = createEvent(this, "calciteInputFocus", 7);
this.calciteInputBlur = createEvent(this, "calciteInputBlur", 7);
this.calciteInputInput = createEvent(this, "calciteInputInput", 7);
this.calciteInputChange = createEvent(this, "calciteInputChange", 7);
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/** specify the alignment of the value of the input */
this.alignment = "start";
/** should the input autofocus */
this.autofocus = false;
/** optionally display a clear button that displays when field has a value
* shows by default for search, time, date
* will not display for type="textarea" */
this.clearable = false;
/** is the input disabled */
this.disabled = false;
/** for number values, displays the locale's group separator */
this.groupSeparator = false;
/** when true, the component will not be visible */
this.hidden = false;
/**
* string to override English loading text
* @default "Loading"
*/
this.intlLoading = TEXT.loading;
/** flip the icon in rtl */
this.iconFlipRtl = false;
/** specify if the input is in loading state */
this.loading = false;
/** BCP 47 language tag for desired language and country format */
this.locale = document.documentElement.lang || "en";
/**
* Toggles locale formatting for numbers.
* @internal
*/
this.localeFormat = false;
/** specify the placement of the number buttons */
this.numberButtonType = "vertical";
/** When true, a field cannot be modified. */
this.readOnly = false;
/** is the input required */
this.required = false;
/** specify the scale of the input, defaults to m */
this.scale = "m";
/** specify the status of the input field, determines message and icons */
this.status = "idle";
/** @internal adds inline styles for text input when slotted in calcite-inline-editable */
this.editingEnabled = false;
/**
* specify the input type
*
* Note that the following types add type-specific icons by default: `date`, `email`, `password`, `search`, `tel`, `time`
*/
this.type = "text";
/** input value */
this.value = "";
/** keep track of the rendered child type */
this.childElType = "input";
/** whether the value of the input was changed as a result of user typing or not */
this.internalValueChange = false;
this.mutationObserver = createObserver("mutation", () => this.setDisabledAction());
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.keyDownHandler = (event) => {
/* prevent default behavior for input to move the cursor to the beginning of the input with every ArrowUp press */
if (event.key === "ArrowUp") {
event.preventDefault();
}
if (this.readOnly || this.disabled) {
return;
}
if (this.isClearable && event.key === "Escape") {
this.clearInputValue(event);
event.preventDefault();
}
};
this.clearInputValue = (nativeEvent) => {
this.setValue({
committing: true,
nativeEvent,
value: ""
});
};
this.inputBlurHandler = () => {
if (this.type === "number") {
this.setValue({ value: this.value });
}
this.calciteInputBlur.emit({
element: this.childEl,
value: this.value
});
if (this.preFocusValue !== this.value) {
this.calciteInputChange.emit();
}
};
this.inputFocusHandler = (event) => {
const slottedActionEl = getSlotted(this.el, "action");
if (event.target !== slottedActionEl) {
this.setFocus();
}
this.calciteInputFocus.emit({
element: this.childEl,
value: this.value
});
this.preFocusValue = this.value;
};
this.inputInputHandler = (nativeEvent) => {
if (this.disabled || this.readOnly) {
return;
}
this.setValue({
nativeEvent,
value: nativeEvent.target.value
});
};
this.inputKeyDownHandler = (event) => {
if (this.disabled || this.readOnly) {
return;
}
if (event.key === "Enter") {
this.calciteInputChange.emit();
}
};
this.inputNumberInputHandler = (nativeEvent) => {
if (this.disabled || this.readOnly) {
return;
}
const value = nativeEvent.target.value;
const delocalizedValue = delocalizeNumberString(value, this.locale);
if (nativeEvent.inputType === "insertFromPaste") {
if (!isValidNumber(delocalizedValue)) {
nativeEvent.preventDefault();
}
this.setValue({
nativeEvent,
value: parseNumberString(delocalizedValue)
});
this.childNumberEl.value = this.localizedValue;
}
else {
this.setValue({
nativeEvent,
value: delocalizeNumberString(value, this.locale)
});
}
};
this.inputNumberKeyDownHandler = (event) => {
if (this.type !== "number" || this.disabled || this.readOnly) {
return;
}
if (event.key === "ArrowUp") {
this.nudgeNumberValue("up", event);
return;
}
if (event.key === "ArrowDown") {
this.nudgeNumberValue("down", event);
return;
}
const supportedKeys = [
...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.calciteInputChange.emit();
}
return;
}
const decimalSeparator = getDecimalSeparator(this.locale);
if (event.key === decimalSeparator) {
if (!this.value && !this.childNumberEl.value) {
return;
}
if (this.value && this.childNumberEl.value.indexOf(decimalSeparator) === -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 = 100;
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.numberButtonMouseUpAndMouseOutHandler = () => {
window.clearInterval(this.nudgeNumberValueIntervalId);
};
this.numberButtonMouseDownHandler = (event) => {
event.preventDefault();
const direction = event.target.dataset.adjustment;
this.nudgeNumberValue(direction, event);
};
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.setPreviousValue = (newPreviousValue) => {
this.previousValue =
this.type === "number"
? isValidNumber(newPreviousValue)
? newPreviousValue
: ""
: newPreviousValue;
};
this.setValue = ({ committing = false, nativeEvent, origin = "internal", value }) => {
const previousLocalizedValue = this.type === "number"
? localizeNumberString(this.previousValue, this.locale, this.groupSeparator)
: "";
const sanitizedValue = this.type === "number" ? sanitizeNumberString(value) : value;
const newValue = this.type === "number" && value && !sanitizedValue
? isValidNumber(this.previousValue)
? this.previousValue
: ""
: sanitizedValue;
const newLocalizedValue = this.type === "number"
? localizeNumberString(newValue, this.locale, this.groupSeparator)
: "";
this.internalValueChange = origin === "internal" && this.value !== newValue;
this.setPreviousValue(this.value);
this.value = newValue;
if (this.type === "number") {
this.localizedValue = newLocalizedValue;
}
if (origin === "external") {
this.setInputValue(this.type === "number" ? newLocalizedValue : newValue);
}
if (nativeEvent) {
const calciteInputInputEvent = this.calciteInputInput.emit({
element: this.childEl,
nativeEvent,
value: this.value
});
if (calciteInputInputEvent.defaultPrevented) {
this.value = this.previousValue;
this.localizedValue = previousLocalizedValue;
}
else if (committing) {
this.calciteInputChange.emit();
}
}
};
this.inputKeyUpHandler = () => {
window.clearInterval(this.nudgeNumberValueIntervalId);
};
}
disabledWatcher() {
this.setDisabledAction();
}
/** watcher to update number-to-string for max */
maxWatcher() {
var _a;
this.maxString = ((_a = this.max) === null || _a === void 0 ? void 0 : _a.toString()) || null;
}
/** watcher to update number-to-string for min */
minWatcher() {
var _a;
this.minString = ((_a = this.min) === null || _a === void 0 ? void 0 : _a.toString()) || null;
}
valueWatcher(newValue) {
if (!this.internalValueChange) {
this.setValue({
origin: "external",
value: newValue == null || newValue == ""
? ""
: this.type === "number"
? isValidNumber(newValue)
? newValue
: this.previousValue || ""
: newValue
});
this.warnAboutInvalidNumberValue(newValue);
}
this.internalValueChange = false;
}
updateRequestedIcon() {
this.requestedIcon = 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";
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
var _a;
this.scale = getElementProp(this.el, "scale", this.scale);
this.status = getElementProp(this.el, "status", this.status);
this.inlineEditableEl = this.el.closest("calcite-inline-editable");
if (this.inlineEditableEl) {
this.editingEnabled = this.inlineEditableEl.editingEnabled || false;
}
this.setPreviousValue(this.value);
if (this.type === "number") {
this.warnAboutInvalidNumberValue(this.value);
this.setValue({
origin: "loading",
value: isValidNumber(this.value) ? this.value : ""
});
}
connectLabel(this);
connectForm(this);
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
this.setDisabledAction();
}
disconnectedCallback() {
var _a;
disconnectLabel(this);
disconnectForm(this);
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
}
componentWillLoad() {
var _a, _b;
this.childElType = this.type === "textarea" ? "textarea" : "input";
this.maxString = (_a = this.max) === null || _a === void 0 ? void 0 : _a.toString();
this.minString = (_b = this.min) === null || _b === void 0 ? void 0 : _b.toString();
this.requestedIcon = setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type);
}
componentShouldUpdate(newValue, oldValue, property) {
if (this.type === "number" && property === "value" && newValue && !isValidNumber(newValue)) {
this.setValue({
value: oldValue
});
return false;
}
return true;
}
componentDidRender() {
updateHostInteraction(this);
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
var _a, _b;
if (this.type === "number") {
(_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.focus();
}
else {
(_b = this.childEl) === null || _b === void 0 ? void 0 : _b.focus();
}
}
onLabelClick() {
this.setFocus();
}
incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent) {
const { value } = this;
const inputStep = this.step === "any" ? 1 : Math.abs(this.step || 1);
const inputVal = value && value !== "" ? parseFloat(value) : 0;
const adjustment = direction === "up" ? 1 : -1;
const nudgedValue = inputVal + inputStep * adjustment;
const finalValue = (typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
(typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
? inputVal
: nudgedValue;
const inputValPlaces = decimalPlaces(inputVal);
const inputStepPlaces = decimalPlaces(inputStep);
this.setValue({
committing: true,
nativeEvent,
value: finalValue.toFixed(Math.max(inputValPlaces, inputStepPlaces))
});
}
onFormReset() {
this.setValue({
value: this.defaultValue
});
}
syncHiddenFormInput(input) {
var _a, _b, _c, _d;
if (this.type === "number") {
input.type = "number";
input.min = (_b = (_a = this.min) === null || _a === void 0 ? void 0 : _a.toString(10)) !== null && _b !== void 0 ? _b : "";
input.max = (_d = (_c = this.max) === null || _c === void 0 ? void 0 : _c.toString(10)) !== null && _d !== void 0 ? _d : "";
}
else if (this.type === "text") {
input.type = "text";
if (this.minLength != null) {
input.minLength = this.minLength;
}
if (this.maxLength != null) {
input.maxLength = this.maxLength;
}
}
else if (this.type === "password") {
input.type = "password";
}
}
setDisabledAction() {
const slottedActionEl = getSlotted(this.el, "action");
if (!slottedActionEl) {
return;
}
this.disabled
? slottedActionEl.setAttribute("disabled", "")
: slottedActionEl.removeAttribute("disabled");
}
warnAboutInvalidNumberValue(value) {
if (this.type === "number" && value && !isValidNumber(value)) {
console.warn(`The specified value "${value}" cannot be parsed, or is out of range.`);
}
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
const dir = getElementDir(this.el);
const loader = (h("div", { class: CSS.loader }, h("calcite-progress", { label: this.intlLoading, type: "indeterminate" })));
const inputClearButton = (h("button", { class: CSS.clearButton, disabled: this.disabled || this.readOnly, onClick: this.clearInputValue, tabIndex: this.disabled ? -1 : 0, type: "button" }, h("calcite-icon", { icon: "x", scale: "s" })));
const iconEl = (h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: "s" }));
const isHorizontalNumberButton = this.numberButtonType === "horizontal";
const numberButtonsHorizontalUp = (h("button", { class: {
[CSS.numberButtonItem]: true,
[CSS.buttonItemHorizontal]: isHorizontalNumberButton
}, "data-adjustment": "up", disabled: this.disabled || this.readOnly, onMouseDown: this.numberButtonMouseDownHandler, onMouseOut: this.numberButtonMouseUpAndMouseOutHandler, onMouseUp: this.numberButtonMouseUpAndMouseOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-up", scale: "s" })));
const numberButtonsHorizontalDown = (h("button", { class: {
[CSS.numberButtonItem]: true,
[CSS.buttonItemHorizontal]: isHorizontalNumberButton
}, "data-adjustment": "down", disabled: this.disabled || this.readOnly, onMouseDown: this.numberButtonMouseDownHandler, onMouseOut: this.numberButtonMouseUpAndMouseOutHandler, onMouseUp: this.numberButtonMouseUpAndMouseOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-down", scale: "s" })));
const numberButtonsVertical = (h("div", { class: CSS.numberButtonWrapper }, numberButtonsHorizontalUp, numberButtonsHorizontalDown));
const prefixText = h("div", { class: CSS.prefix }, this.prefixText);
const suffixText = h("div", { class: CSS.suffix }, this.suffixText);
const localeNumberInput = this.type === "number" ? (h("input", { "aria-label": getLabelText(this), autofocus: this.autofocus ? true : null, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.el.enterKeyHint, inputMode: this.el.inputMode, key: "localized-input", maxLength: this.maxLength, minLength: this.minLength, name: undefined, onBlur: this.inputBlurHandler, onFocus: this.inputFocusHandler, onInput: this.inputNumberInputHandler, onKeyDown: this.inputNumberKeyDownHandler, onKeyUp: this.inputKeyUpHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildNumberElRef, type: "text", value: this.localizedValue })) : null;
const childEl = this.type !== "number"
? [
h(this.childElType, { "aria-label": getLabelText(this), autofocus: this.autofocus ? true : null, class: {
[CSS.editingEnabled]: this.editingEnabled,
[CSS.inlineChild]: !!this.inlineEditableEl
}, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.el.enterKeyHint, inputMode: this.el.inputMode, max: this.maxString, maxLength: this.maxLength, min: this.minString, minLength: this.minLength, name: this.name, onBlur: this.inputBlurHandler, onFocus: this.inputFocusHandler, onInput: this.inputInputHandler, onKeyDown: this.inputKeyDownHandler, onKeyUp: this.inputKeyUpHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildElRef, required: this.required ? true : null, step: this.step, tabIndex: this.disabled || (this.inlineEditableEl && !this.editingEnabled) ? -1 : null, type: this.type, value: this.value }),
this.isTextarea ? (h("div", { class: CSS.resizeIconWrapper }, h("calcite-icon", { icon: "chevron-down", scale: "s" }))) : null
]
: null;
return (h(Host, { onClick: this.inputFocusHandler, onKeyDown: this.keyDownHandler }, h("div", { class: { [CSS.inputWrapper]: true, [CSS_UTILITY.rtl]: dir === "rtl" } }, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly
? numberButtonsHorizontalDown
: null, this.prefixText ? prefixText : null, h("div", { class: CSS.wrapper }, localeNumberInput, childEl, this.isClearable ? inputClearButton : null, this.requestedIcon ? iconEl : null, this.loading ? loader : null), h("div", { class: CSS.actionWrapper }, 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, h(HiddenFormInputSlot, { component: this }))));
}
get el() { return this; }
static get watchers() { return {
"disabled": ["disabledWatcher"],
"max": ["maxWatcher"],
"min": ["minWatcher"],
"value": ["valueWatcher"],
"icon": ["updateRequestedIcon"],
"type": ["updateRequestedIcon"]
}; }
static get style() { return inputCss; }
}, [1, "calcite-input", {
"alignment": [513],
"autofocus": [4],
"clearable": [516],
"disabled": [516],
"groupSeparator": [4, "group-separator"],
"hidden": [4],
"icon": [520],
"intlLoading": [1, "intl-loading"],
"iconFlipRtl": [516, "icon-flip-rtl"],
"label": [1],
"loading": [516],
"locale": [1],
"localeFormat": [4, "locale-format"],
"max": [514],
"min": [514],
"maxlength": [514],
"maxLength": [514, "max-length"],
"minLength": [514, "min-length"],
"name": [513],
"numberButtonType": [513, "number-button-type"],
"placeholder": [1],
"prefixText": [1, "prefix-text"],
"readOnly": [4, "read-only"],
"required": [4],
"scale": [1537],
"status": [1537],
"step": [520],
"suffixText": [1, "suffix-text"],
"editingEnabled": [1540, "editing-enabled"],
"type": [513],
"value": [1025],
"localizedValue": [32],
"setFocus": [64]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-input", "calcite-icon", "calcite-progress"];
components.forEach(tagName => { switch (tagName) {
case "calcite-input":
if (!customElements.get(tagName)) {
customElements.define(tagName, Input);
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
case "calcite-progress":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { Input as I, defineCustomElement as d };