calcite-input.entry.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { g as getSlotted, i as isPrimaryPointerButton, s as setRequestedIcon, e as getElementProp, a as getElementDir } from './dom-8f0a9ff2.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-333c4a4c.js';
  9. import { s as submitForm, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-80dbd90e.js';
  10. import { n as numberStringFormatter, i as isValidNumber, p as parseNumberString, a as defaultNumberingSystem, s as sanitizeNumberString, u as updateEffectiveLocale, c as connectLocalized, d as disconnectLocalized } from './locale-35f81208.js';
  11. import { n as numberKeys } from './key-acc755b7.js';
  12. import { T as TEXT$1, C as CSS_UTILITY } from './resources-9c476cb6.js';
  13. import { d as decimalPlaces } from './math-dd4df0b5.js';
  14. import { c as createObserver } from './observers-9f44e9b3.js';
  15. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  16. import './guid-9f15e57a.js';
  17. const CSS = {
  18. loader: "loader",
  19. clearButton: "clear-button",
  20. editingEnabled: "editing-enabled",
  21. inlineChild: "inline-child",
  22. inputIcon: "icon",
  23. prefix: "prefix",
  24. suffix: "suffix",
  25. numberButtonWrapper: "number-button-wrapper",
  26. buttonItemHorizontal: "number-button-item--horizontal",
  27. wrapper: "element-wrapper",
  28. inputWrapper: "wrapper",
  29. actionWrapper: "action-wrapper",
  30. resizeIconWrapper: "resize-icon-wrapper",
  31. numberButtonItem: "number-button-item"
  32. };
  33. const INPUT_TYPE_ICONS = {
  34. tel: "phone",
  35. password: "lock",
  36. email: "email-address",
  37. date: "calendar",
  38. time: "clock",
  39. search: "search"
  40. };
  41. const SLOTS = {
  42. action: "action"
  43. };
  44. const TEXT = {
  45. clear: "Clear value"
  46. };
  47. 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-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-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)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}: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-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{padding-inline-start:2rem}:host([icon][scale=m]) input{padding-inline-start:2.5rem}:host([icon][scale=l]) input{padding-inline-start:3rem}.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:1}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-brand);outline-offset:-2px}.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;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}";
  48. const Input = class {
  49. constructor(hostRef) {
  50. registerInstance(this, hostRef);
  51. this.calciteInternalInputFocus = createEvent(this, "calciteInternalInputFocus", 6);
  52. this.calciteInternalInputBlur = createEvent(this, "calciteInternalInputBlur", 6);
  53. this.calciteInputInput = createEvent(this, "calciteInputInput", 7);
  54. this.calciteInputChange = createEvent(this, "calciteInputChange", 6);
  55. //--------------------------------------------------------------------------
  56. //
  57. // Properties
  58. //
  59. //--------------------------------------------------------------------------
  60. /** Specifies the text alignment of the component's value. */
  61. this.alignment = "start";
  62. /**
  63. * When `true`, the component is focused on page load.
  64. *
  65. * @mdn [autofocus](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus)
  66. */
  67. this.autofocus = false;
  68. /**
  69. * When `true`, a clear button is displayed when the component has a value. The clear button shows by default for `"search"`, `"time"`, and `"date"` types, and will not display for the `"textarea"` type.
  70. */
  71. this.clearable = false;
  72. /**
  73. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  74. *
  75. * @mdn [disabled](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled)
  76. */
  77. this.disabled = false;
  78. /**
  79. * When `true`, number values are displayed with a group separator corresponding to the language and country format.
  80. */
  81. this.groupSeparator = false;
  82. /**
  83. * When `true`, the component will not be visible.
  84. *
  85. * @mdn [hidden](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden)
  86. */
  87. this.hidden = false;
  88. /**
  89. * Accessible name when the component is loading.
  90. *
  91. * @default "Loading"
  92. */
  93. this.intlLoading = TEXT$1.loading;
  94. /** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
  95. this.iconFlipRtl = false;
  96. /** When `true`, a busy indicator is displayed. */
  97. this.loading = false;
  98. /**
  99. * When `true`, uses locale formatting for numbers.
  100. *
  101. * @internal
  102. */
  103. this.localeFormat = false;
  104. /** Specifies the placement of the buttons for `type="number"`. */
  105. this.numberButtonType = "vertical";
  106. /**
  107. * When `true`, the component's value can be read, but cannot be modified.
  108. *
  109. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  110. */
  111. this.readOnly = false;
  112. /** When `true`, the component must have a value in order for the form to submit. */
  113. this.required = false;
  114. /** Specifies the size of the component. */
  115. this.scale = "m";
  116. /** Specifies the status of the input field, which determines message and icons. */
  117. this.status = "idle";
  118. /**
  119. * @internal
  120. */
  121. this.editingEnabled = false;
  122. /**
  123. * Specifies the component type.
  124. *
  125. * Note that the following `type`s add type-specific icons by default: `"date"`, `"email"`, `"password"`, `"search"`, `"tel"`, `"time"`.
  126. */
  127. this.type = "text";
  128. /** The component's value. */
  129. this.value = "";
  130. /** keep track of the rendered child type */
  131. this.childElType = "input";
  132. this.previousValueOrigin = "initial";
  133. this.mutationObserver = createObserver("mutation", () => this.setDisabledAction());
  134. this.userChangedValue = false;
  135. //--------------------------------------------------------------------------
  136. //
  137. // State
  138. //
  139. //--------------------------------------------------------------------------
  140. this.effectiveLocale = "";
  141. //--------------------------------------------------------------------------
  142. //
  143. // Private Methods
  144. //
  145. //--------------------------------------------------------------------------
  146. this.keyDownHandler = (event) => {
  147. if (this.readOnly || this.disabled) {
  148. return;
  149. }
  150. if (this.isClearable && event.key === "Escape") {
  151. this.clearInputValue(event);
  152. event.preventDefault();
  153. }
  154. if (event.key === "Enter" && !event.defaultPrevented) {
  155. if (submitForm(this)) {
  156. event.preventDefault();
  157. }
  158. }
  159. };
  160. this.clearInputValue = (nativeEvent) => {
  161. this.setValue({
  162. committing: true,
  163. nativeEvent,
  164. origin: "user",
  165. value: ""
  166. });
  167. };
  168. this.emitChangeIfUserModified = () => {
  169. if (this.previousValueOrigin === "user" && this.value !== this.previousEmittedValue) {
  170. this.calciteInputChange.emit();
  171. }
  172. this.previousEmittedValue = this.value;
  173. };
  174. this.inputBlurHandler = () => {
  175. this.calciteInternalInputBlur.emit();
  176. this.emitChangeIfUserModified();
  177. };
  178. this.inputFocusHandler = (event) => {
  179. const slottedActionEl = getSlotted(this.el, "action");
  180. if (event.target !== slottedActionEl) {
  181. this.setFocus();
  182. }
  183. this.calciteInternalInputFocus.emit();
  184. };
  185. this.inputInputHandler = (nativeEvent) => {
  186. if (this.disabled || this.readOnly) {
  187. return;
  188. }
  189. this.setValue({
  190. nativeEvent,
  191. origin: "user",
  192. value: nativeEvent.target.value
  193. });
  194. };
  195. this.inputKeyDownHandler = (event) => {
  196. if (this.disabled || this.readOnly) {
  197. return;
  198. }
  199. if (event.key === "Enter") {
  200. this.emitChangeIfUserModified();
  201. }
  202. };
  203. this.inputNumberInputHandler = (nativeEvent) => {
  204. if (this.disabled || this.readOnly) {
  205. return;
  206. }
  207. const value = nativeEvent.target.value;
  208. numberStringFormatter.numberFormatOptions = {
  209. locale: this.effectiveLocale,
  210. numberingSystem: this.numberingSystem,
  211. useGrouping: this.groupSeparator
  212. };
  213. const delocalizedValue = numberStringFormatter.delocalize(value);
  214. if (nativeEvent.inputType === "insertFromPaste") {
  215. if (!isValidNumber(delocalizedValue)) {
  216. nativeEvent.preventDefault();
  217. }
  218. this.setValue({
  219. nativeEvent,
  220. origin: "user",
  221. value: parseNumberString(delocalizedValue)
  222. });
  223. this.childNumberEl.value = this.localizedValue;
  224. }
  225. else {
  226. this.setValue({
  227. nativeEvent,
  228. origin: "user",
  229. value: delocalizedValue
  230. });
  231. }
  232. };
  233. this.inputNumberKeyDownHandler = (event) => {
  234. if (this.type !== "number" || this.disabled || this.readOnly) {
  235. return;
  236. }
  237. if (event.key === "ArrowUp") {
  238. /* prevent default behavior of moving cursor to the beginning of the input when holding down ArrowUp */
  239. event.preventDefault();
  240. this.nudgeNumberValue("up", event);
  241. return;
  242. }
  243. if (event.key === "ArrowDown") {
  244. this.nudgeNumberValue("down", event);
  245. return;
  246. }
  247. const supportedKeys = [
  248. ...numberKeys,
  249. "ArrowLeft",
  250. "ArrowRight",
  251. "Backspace",
  252. "Delete",
  253. "Enter",
  254. "Escape",
  255. "Tab"
  256. ];
  257. if (event.altKey || event.ctrlKey || event.metaKey) {
  258. return;
  259. }
  260. const isShiftTabEvent = event.shiftKey && event.key === "Tab";
  261. if (supportedKeys.includes(event.key) && (!event.shiftKey || isShiftTabEvent)) {
  262. if (event.key === "Enter") {
  263. this.emitChangeIfUserModified();
  264. }
  265. return;
  266. }
  267. numberStringFormatter.numberFormatOptions = {
  268. locale: this.effectiveLocale,
  269. numberingSystem: this.numberingSystem,
  270. useGrouping: this.groupSeparator
  271. };
  272. if (event.key === numberStringFormatter.decimal) {
  273. if (!this.value && !this.childNumberEl.value) {
  274. return;
  275. }
  276. if (this.value && this.childNumberEl.value.indexOf(numberStringFormatter.decimal) === -1) {
  277. return;
  278. }
  279. }
  280. if (/[eE]/.test(event.key)) {
  281. if (!this.value && !this.childNumberEl.value) {
  282. return;
  283. }
  284. if (this.value && !/[eE]/.test(this.childNumberEl.value)) {
  285. return;
  286. }
  287. }
  288. if (event.key === "-") {
  289. if (!this.value && !this.childNumberEl.value) {
  290. return;
  291. }
  292. if (this.value && this.childNumberEl.value.split("-").length <= 2) {
  293. return;
  294. }
  295. }
  296. event.preventDefault();
  297. };
  298. this.nudgeNumberValue = (direction, nativeEvent) => {
  299. if ((nativeEvent instanceof KeyboardEvent && nativeEvent.repeat) || this.type !== "number") {
  300. return;
  301. }
  302. const inputMax = this.maxString ? parseFloat(this.maxString) : null;
  303. const inputMin = this.minString ? parseFloat(this.minString) : null;
  304. const valueNudgeDelayInMs = 100;
  305. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  306. if (this.nudgeNumberValueIntervalId) {
  307. window.clearInterval(this.nudgeNumberValueIntervalId);
  308. }
  309. let firstValueNudge = true;
  310. this.nudgeNumberValueIntervalId = window.setInterval(() => {
  311. if (firstValueNudge) {
  312. firstValueNudge = false;
  313. return;
  314. }
  315. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  316. }, valueNudgeDelayInMs);
  317. };
  318. this.numberButtonPointerUpAndOutHandler = () => {
  319. window.clearInterval(this.nudgeNumberValueIntervalId);
  320. };
  321. this.numberButtonPointerDownHandler = (event) => {
  322. if (!isPrimaryPointerButton(event)) {
  323. return;
  324. }
  325. event.preventDefault();
  326. const direction = event.target.dataset.adjustment;
  327. if (!this.disabled) {
  328. this.nudgeNumberValue(direction, event);
  329. }
  330. };
  331. this.hiddenInputChangeHandler = (event) => {
  332. if (event.target.name === this.name) {
  333. const hiddenInputValue = event.target.value;
  334. const value = this.type === "number"
  335. ? isValidNumber(hiddenInputValue)
  336. ? hiddenInputValue
  337. : ""
  338. : hiddenInputValue;
  339. this.setValue({
  340. value,
  341. origin: "direct"
  342. });
  343. }
  344. event.stopPropagation();
  345. };
  346. this.setChildElRef = (el) => {
  347. this.childEl = el;
  348. };
  349. this.setChildNumberElRef = (el) => {
  350. this.childNumberEl = el;
  351. };
  352. this.setInputValue = (newInputValue) => {
  353. if (this.type === "text" && !this.childEl) {
  354. return;
  355. }
  356. if (this.type === "number" && !this.childNumberEl) {
  357. return;
  358. }
  359. this[`child${this.type === "number" ? "Number" : ""}El`].value = newInputValue;
  360. };
  361. this.setPreviousEmittedValue = (newPreviousEmittedValue) => {
  362. this.previousEmittedValue =
  363. this.type === "number"
  364. ? isValidNumber(newPreviousEmittedValue)
  365. ? newPreviousEmittedValue
  366. : ""
  367. : newPreviousEmittedValue;
  368. };
  369. this.setPreviousValue = (newPreviousValue) => {
  370. this.previousValue =
  371. this.type === "number"
  372. ? isValidNumber(newPreviousValue)
  373. ? newPreviousValue
  374. : ""
  375. : newPreviousValue;
  376. };
  377. this.setValue = ({ committing = false, nativeEvent, origin, previousValue, value }) => {
  378. numberStringFormatter.numberFormatOptions = {
  379. locale: this.effectiveLocale,
  380. numberingSystem: this.numberingSystem,
  381. useGrouping: this.groupSeparator
  382. };
  383. if (this.type === "number") {
  384. const delocalizedValue = (this.numberingSystem && this.numberingSystem !== "latn") ||
  385. defaultNumberingSystem !== "latn"
  386. ? numberStringFormatter.delocalize(value)
  387. : value;
  388. const sanitizedValue = sanitizeNumberString(delocalizedValue);
  389. const newValue = (value && !sanitizedValue) || [".", "-"].includes(sanitizedValue)
  390. ? isValidNumber(this.previousValue)
  391. ? this.previousValue
  392. : ""
  393. : sanitizedValue;
  394. const newLocalizedValue = numberStringFormatter.localize(newValue);
  395. this.localizedValue = newLocalizedValue;
  396. this.userChangedValue = origin === "user" && this.value !== newValue;
  397. this.value = newValue;
  398. origin === "direct" && this.setInputValue(newLocalizedValue);
  399. }
  400. else {
  401. this.userChangedValue = origin === "user" && this.value !== value;
  402. this.value = value;
  403. origin === "direct" && this.setInputValue(value);
  404. }
  405. this.setPreviousValue(previousValue || this.value);
  406. this.previousValueOrigin = origin;
  407. if (nativeEvent) {
  408. const calciteInputInputEvent = this.calciteInputInput.emit({
  409. element: this.childEl,
  410. nativeEvent,
  411. value: this.value
  412. });
  413. if (calciteInputInputEvent.defaultPrevented) {
  414. this.value = this.previousValue;
  415. this.localizedValue =
  416. this.type === "number"
  417. ? numberStringFormatter.localize(this.previousValue)
  418. : this.previousValue;
  419. }
  420. else if (committing) {
  421. this.emitChangeIfUserModified();
  422. }
  423. }
  424. };
  425. this.inputKeyUpHandler = () => {
  426. window.clearInterval(this.nudgeNumberValueIntervalId);
  427. };
  428. }
  429. disabledWatcher() {
  430. this.setDisabledAction();
  431. }
  432. localeChanged() {
  433. updateEffectiveLocale(this);
  434. }
  435. /** watcher to update number-to-string for max */
  436. maxWatcher() {
  437. var _a;
  438. this.maxString = ((_a = this.max) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  439. }
  440. /** watcher to update number-to-string for min */
  441. minWatcher() {
  442. var _a;
  443. this.minString = ((_a = this.min) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  444. }
  445. valueWatcher(newValue, previousValue) {
  446. if (!this.userChangedValue) {
  447. this.setValue({
  448. origin: "direct",
  449. previousValue,
  450. value: newValue == null || newValue == ""
  451. ? ""
  452. : this.type === "number"
  453. ? isValidNumber(newValue)
  454. ? newValue
  455. : this.previousValue || ""
  456. : newValue
  457. });
  458. this.warnAboutInvalidNumberValue(newValue);
  459. }
  460. this.userChangedValue = false;
  461. }
  462. updateRequestedIcon() {
  463. this.requestedIcon = setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type);
  464. }
  465. get isClearable() {
  466. return !this.isTextarea && (this.clearable || this.type === "search") && this.value.length > 0;
  467. }
  468. get isTextarea() {
  469. return this.childElType === "textarea";
  470. }
  471. //--------------------------------------------------------------------------
  472. //
  473. // Lifecycle
  474. //
  475. //--------------------------------------------------------------------------
  476. connectedCallback() {
  477. var _a;
  478. connectLocalized(this);
  479. this.scale = getElementProp(this.el, "scale", this.scale);
  480. this.status = getElementProp(this.el, "status", this.status);
  481. this.inlineEditableEl = this.el.closest("calcite-inline-editable");
  482. if (this.inlineEditableEl) {
  483. this.editingEnabled = this.inlineEditableEl.editingEnabled || false;
  484. }
  485. connectLabel(this);
  486. connectForm(this);
  487. this.setPreviousEmittedValue(this.value);
  488. this.setPreviousValue(this.value);
  489. if (this.type === "number") {
  490. this.warnAboutInvalidNumberValue(this.value);
  491. this.setValue({
  492. origin: "connected",
  493. value: isValidNumber(this.value) ? this.value : ""
  494. });
  495. }
  496. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  497. this.setDisabledAction();
  498. this.el.addEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  499. }
  500. disconnectedCallback() {
  501. var _a;
  502. disconnectLabel(this);
  503. disconnectForm(this);
  504. disconnectLocalized(this);
  505. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  506. this.el.removeEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  507. }
  508. componentWillLoad() {
  509. var _a, _b;
  510. this.childElType = this.type === "textarea" ? "textarea" : "input";
  511. this.maxString = (_a = this.max) === null || _a === void 0 ? void 0 : _a.toString();
  512. this.minString = (_b = this.min) === null || _b === void 0 ? void 0 : _b.toString();
  513. this.requestedIcon = setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type);
  514. }
  515. componentShouldUpdate(newValue, oldValue, property) {
  516. if (this.type === "number" && property === "value" && newValue && !isValidNumber(newValue)) {
  517. this.setValue({
  518. origin: "reset",
  519. value: oldValue
  520. });
  521. return false;
  522. }
  523. return true;
  524. }
  525. componentDidRender() {
  526. updateHostInteraction(this);
  527. }
  528. //--------------------------------------------------------------------------
  529. //
  530. // Public Methods
  531. //
  532. //--------------------------------------------------------------------------
  533. /** Sets focus on the component. */
  534. async setFocus() {
  535. var _a, _b;
  536. if (this.type === "number") {
  537. (_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.focus();
  538. }
  539. else {
  540. (_b = this.childEl) === null || _b === void 0 ? void 0 : _b.focus();
  541. }
  542. }
  543. /** Selects all text of the component's `value`. */
  544. async selectText() {
  545. var _a, _b;
  546. if (this.type === "number") {
  547. (_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.select();
  548. }
  549. else {
  550. (_b = this.childEl) === null || _b === void 0 ? void 0 : _b.select();
  551. }
  552. }
  553. onLabelClick() {
  554. this.setFocus();
  555. }
  556. incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent) {
  557. const { value } = this;
  558. const inputStep = this.step === "any" ? 1 : Math.abs(this.step || 1);
  559. const inputVal = value && value !== "" ? parseFloat(value) : 0;
  560. const adjustment = direction === "up" ? 1 : -1;
  561. const nudgedValue = inputVal + inputStep * adjustment;
  562. const finalValue = (typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
  563. (typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
  564. ? inputVal
  565. : nudgedValue;
  566. const inputValPlaces = decimalPlaces(inputVal);
  567. const inputStepPlaces = decimalPlaces(inputStep);
  568. this.setValue({
  569. committing: true,
  570. nativeEvent,
  571. origin: "user",
  572. value: finalValue.toFixed(Math.max(inputValPlaces, inputStepPlaces))
  573. });
  574. }
  575. onFormReset() {
  576. this.setValue({
  577. origin: "reset",
  578. value: this.defaultValue
  579. });
  580. }
  581. syncHiddenFormInput(input) {
  582. var _a, _b, _c, _d;
  583. if (this.type === "number") {
  584. input.type = "number";
  585. input.min = (_b = (_a = this.min) === null || _a === void 0 ? void 0 : _a.toString(10)) !== null && _b !== void 0 ? _b : "";
  586. input.max = (_d = (_c = this.max) === null || _c === void 0 ? void 0 : _c.toString(10)) !== null && _d !== void 0 ? _d : "";
  587. }
  588. else if (this.type === "text") {
  589. input.type = "text";
  590. if (this.minLength != null) {
  591. input.minLength = this.minLength;
  592. }
  593. if (this.maxLength != null) {
  594. input.maxLength = this.maxLength;
  595. }
  596. }
  597. else if (this.type === "password") {
  598. input.type = "password";
  599. }
  600. }
  601. setDisabledAction() {
  602. const slottedActionEl = getSlotted(this.el, "action");
  603. if (!slottedActionEl) {
  604. return;
  605. }
  606. this.disabled
  607. ? slottedActionEl.setAttribute("disabled", "")
  608. : slottedActionEl.removeAttribute("disabled");
  609. }
  610. warnAboutInvalidNumberValue(value) {
  611. if (this.type === "number" && value && !isValidNumber(value)) {
  612. console.warn(`The specified value "${value}" cannot be parsed, or is out of range.`);
  613. }
  614. }
  615. // --------------------------------------------------------------------------
  616. //
  617. // Render Methods
  618. //
  619. // --------------------------------------------------------------------------
  620. render() {
  621. const dir = getElementDir(this.el);
  622. const loader = (h("div", { class: CSS.loader }, h("calcite-progress", { label: this.intlLoading, type: "indeterminate" })));
  623. const inputClearButton = (h("button", { "aria-label": this.intlClear || TEXT.clear, class: CSS.clearButton, disabled: this.disabled || this.readOnly, onClick: this.clearInputValue, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "x", scale: "s" })));
  624. const iconEl = (h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: "s" }));
  625. const isHorizontalNumberButton = this.numberButtonType === "horizontal";
  626. const numberButtonsHorizontalUp = (h("button", { "aria-hidden": "true", class: {
  627. [CSS.numberButtonItem]: true,
  628. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  629. }, "data-adjustment": "up", disabled: this.disabled || this.readOnly, onPointerDown: this.numberButtonPointerDownHandler, onPointerOut: this.numberButtonPointerUpAndOutHandler, onPointerUp: this.numberButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-up", scale: "s" })));
  630. const numberButtonsHorizontalDown = (h("button", { "aria-hidden": "true", class: {
  631. [CSS.numberButtonItem]: true,
  632. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  633. }, "data-adjustment": "down", disabled: this.disabled || this.readOnly, onPointerDown: this.numberButtonPointerDownHandler, onPointerOut: this.numberButtonPointerUpAndOutHandler, onPointerUp: this.numberButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-down", scale: "s" })));
  634. const numberButtonsVertical = (h("div", { class: CSS.numberButtonWrapper }, numberButtonsHorizontalUp, numberButtonsHorizontalDown));
  635. const prefixText = h("div", { class: CSS.prefix }, this.prefixText);
  636. const suffixText = h("div", { class: CSS.suffix }, this.suffixText);
  637. 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;
  638. const childEl = this.type !== "number"
  639. ? [
  640. h(this.childElType, { "aria-label": getLabelText(this), autofocus: this.autofocus ? true : null, class: {
  641. [CSS.editingEnabled]: this.editingEnabled,
  642. [CSS.inlineChild]: !!this.inlineEditableEl
  643. }, 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 }),
  644. this.isTextarea ? (h("div", { class: CSS.resizeIconWrapper }, h("calcite-icon", { icon: "chevron-down", scale: "s" }))) : null
  645. ]
  646. : null;
  647. 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
  648. ? numberButtonsHorizontalDown
  649. : 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
  650. ? numberButtonsVertical
  651. : null, this.suffixText ? suffixText : null, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly
  652. ? numberButtonsHorizontalUp
  653. : null, h(HiddenFormInputSlot, { component: this }))));
  654. }
  655. get el() { return getElement(this); }
  656. static get watchers() { return {
  657. "disabled": ["disabledWatcher"],
  658. "locale": ["localeChanged"],
  659. "max": ["maxWatcher"],
  660. "min": ["minWatcher"],
  661. "value": ["valueWatcher"],
  662. "icon": ["updateRequestedIcon"],
  663. "type": ["updateRequestedIcon"]
  664. }; }
  665. };
  666. Input.style = inputCss;
  667. export { Input as calcite_input };