calcite-input-number.entry.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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, s as sanitizeNumberString, a as defaultNumberingSystem, 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 SLOTS = {
  34. action: "action"
  35. };
  36. const TEXT = {
  37. clear: "Clear value"
  38. };
  39. const inputNumberCss = "@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]) .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]) .clear-button{min-block-size:1.5rem;min-inline-size:1.5rem}: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]) .number-button-wrapper,:host([scale=m]) .action-wrapper calcite-button,:host([scale=m]) .action-wrapper calcite-button button{block-size:2rem}:host([scale=m]) .clear-button{min-block-size:2rem;min-inline-size:2rem}: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]) .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]) .clear-button{min-block-size:2.75rem;min-inline-size:2.75rem}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}: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);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{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input:focus{border-color:var(--calcite-ui-brand);color:var(--calcite-ui-text-1)}:host input[readonly]{background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium)}:host input[readonly]:focus{color:var(--calcite-ui-text-1)}:host calcite-icon{color:var(--calcite-ui-text-3)}:host input{outline-color:transparent}:host input:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}:host([status=invalid]) input{border-color:var(--calcite-ui-danger)}:host([status=invalid]) input: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;z-index:1;display:block;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.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]) input{text-align:start}:host([alignment=end]) input{text-align:end}.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{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(.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}";
  40. const InputNumber = class {
  41. constructor(hostRef) {
  42. registerInstance(this, hostRef);
  43. this.calciteInternalInputNumberFocus = createEvent(this, "calciteInternalInputNumberFocus", 6);
  44. this.calciteInternalInputNumberBlur = createEvent(this, "calciteInternalInputNumberBlur", 6);
  45. this.calciteInputNumberInput = createEvent(this, "calciteInputNumberInput", 7);
  46. this.calciteInputNumberChange = createEvent(this, "calciteInputNumberChange", 6);
  47. //--------------------------------------------------------------------------
  48. //
  49. // Properties
  50. //
  51. //--------------------------------------------------------------------------
  52. /** Specifies the text alignment of the component's value. */
  53. this.alignment = "start";
  54. /**
  55. * When `true`, the component is focused on page load.
  56. *
  57. * @mdn [autofocus](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus)
  58. */
  59. this.autofocus = false;
  60. /**
  61. * When `true`, a clear button is displayed when the component has a value.
  62. */
  63. this.clearable = false;
  64. /**
  65. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  66. *
  67. * @mdn [disabled](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled)
  68. */
  69. this.disabled = false;
  70. /**
  71. * When `true`, number values are displayed with a group separator corresponding to the language and country format.
  72. */
  73. this.groupSeparator = false;
  74. /**
  75. * When `true`, the component will not be visible.
  76. *
  77. * @mdn [hidden](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden)
  78. */
  79. this.hidden = false;
  80. /**
  81. * Accessible name that will appear while loading.
  82. *
  83. * @default "Loading"
  84. */
  85. this.intlLoading = TEXT$1.loading;
  86. /** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
  87. this.iconFlipRtl = false;
  88. /** When `true`, the component is in the loading state and `calcite-progress` is displayed. */
  89. this.loading = false;
  90. /**
  91. * Toggles locale formatting for numbers.
  92. *
  93. * @internal
  94. */
  95. this.localeFormat = false;
  96. /** Specifies the placement of the buttons. */
  97. this.numberButtonType = "vertical";
  98. /**
  99. * When `true`, the component's value can be read, but cannot be modified.
  100. *
  101. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  102. */
  103. this.readOnly = false;
  104. /** When `true`, the component must have a value in order for the form to submit. */
  105. this.required = false;
  106. /** Specifies the size of the component. */
  107. this.scale = "m";
  108. /** Specifies the status of the input field, which determines message and icons. */
  109. this.status = "idle";
  110. /**
  111. * @internal
  112. */
  113. this.editingEnabled = false;
  114. /** The component's value. */
  115. this.value = "";
  116. this.previousValueOrigin = "initial";
  117. this.mutationObserver = createObserver("mutation", () => this.setDisabledAction());
  118. this.userChangedValue = false;
  119. //--------------------------------------------------------------------------
  120. //
  121. // State
  122. //
  123. //--------------------------------------------------------------------------
  124. this.effectiveLocale = "";
  125. //--------------------------------------------------------------------------
  126. //
  127. // Private Methods
  128. //
  129. //--------------------------------------------------------------------------
  130. this.keyDownHandler = (event) => {
  131. if (this.readOnly || this.disabled) {
  132. return;
  133. }
  134. if (this.isClearable && event.key === "Escape") {
  135. this.clearInputValue(event);
  136. event.preventDefault();
  137. }
  138. if (event.key === "Enter" && !event.defaultPrevented) {
  139. if (submitForm(this)) {
  140. event.preventDefault();
  141. }
  142. }
  143. };
  144. this.clearInputValue = (nativeEvent) => {
  145. this.setNumberValue({
  146. committing: true,
  147. nativeEvent,
  148. origin: "user",
  149. value: ""
  150. });
  151. };
  152. this.emitChangeIfUserModified = () => {
  153. if (this.previousValueOrigin === "user" && this.value !== this.previousEmittedValue) {
  154. this.calciteInputNumberChange.emit();
  155. }
  156. this.previousEmittedValue = this.value;
  157. };
  158. this.inputNumberBlurHandler = () => {
  159. this.calciteInternalInputNumberBlur.emit();
  160. this.emitChangeIfUserModified();
  161. };
  162. this.inputNumberFocusHandler = (event) => {
  163. const slottedActionEl = getSlotted(this.el, "action");
  164. if (event.target !== slottedActionEl) {
  165. this.setFocus();
  166. }
  167. this.calciteInternalInputNumberFocus.emit();
  168. };
  169. this.inputNumberInputHandler = (nativeEvent) => {
  170. if (this.disabled || this.readOnly) {
  171. return;
  172. }
  173. const value = nativeEvent.target.value;
  174. numberStringFormatter.numberFormatOptions = {
  175. locale: this.effectiveLocale,
  176. numberingSystem: this.numberingSystem,
  177. useGrouping: this.groupSeparator
  178. };
  179. const delocalizedValue = numberStringFormatter.delocalize(value);
  180. if (nativeEvent.inputType === "insertFromPaste") {
  181. if (!isValidNumber(delocalizedValue)) {
  182. nativeEvent.preventDefault();
  183. }
  184. this.setNumberValue({
  185. nativeEvent,
  186. origin: "user",
  187. value: parseNumberString(delocalizedValue)
  188. });
  189. this.childNumberEl.value = this.localizedValue;
  190. }
  191. else {
  192. this.setNumberValue({
  193. nativeEvent,
  194. origin: "user",
  195. value: delocalizedValue
  196. });
  197. }
  198. };
  199. this.inputNumberKeyDownHandler = (event) => {
  200. if (this.disabled || this.readOnly) {
  201. return;
  202. }
  203. if (event.key === "ArrowUp") {
  204. /* prevent default behavior of moving cursor to the beginning of the input when holding down ArrowUp */
  205. event.preventDefault();
  206. this.nudgeNumberValue("up", event);
  207. return;
  208. }
  209. if (event.key === "ArrowDown") {
  210. this.nudgeNumberValue("down", event);
  211. return;
  212. }
  213. const supportedKeys = [
  214. ...numberKeys,
  215. "ArrowLeft",
  216. "ArrowRight",
  217. "Backspace",
  218. "Delete",
  219. "Enter",
  220. "Escape",
  221. "Tab"
  222. ];
  223. if (event.altKey || event.ctrlKey || event.metaKey) {
  224. return;
  225. }
  226. const isShiftTabEvent = event.shiftKey && event.key === "Tab";
  227. if (supportedKeys.includes(event.key) && (!event.shiftKey || isShiftTabEvent)) {
  228. if (event.key === "Enter") {
  229. this.emitChangeIfUserModified();
  230. }
  231. return;
  232. }
  233. numberStringFormatter.numberFormatOptions = {
  234. locale: this.effectiveLocale,
  235. numberingSystem: this.numberingSystem,
  236. useGrouping: this.groupSeparator
  237. };
  238. if (event.key === numberStringFormatter.decimal) {
  239. if (!this.value && !this.childNumberEl.value) {
  240. return;
  241. }
  242. if (this.value && this.childNumberEl.value.indexOf(numberStringFormatter.decimal) === -1) {
  243. return;
  244. }
  245. }
  246. if (/[eE]/.test(event.key)) {
  247. if (!this.value && !this.childNumberEl.value) {
  248. return;
  249. }
  250. if (this.value && !/[eE]/.test(this.childNumberEl.value)) {
  251. return;
  252. }
  253. }
  254. if (event.key === "-") {
  255. if (!this.value && !this.childNumberEl.value) {
  256. return;
  257. }
  258. if (this.value && this.childNumberEl.value.split("-").length <= 2) {
  259. return;
  260. }
  261. }
  262. event.preventDefault();
  263. };
  264. this.nudgeNumberValue = (direction, nativeEvent) => {
  265. if (nativeEvent instanceof KeyboardEvent && nativeEvent.repeat) {
  266. return;
  267. }
  268. const inputMax = this.maxString ? parseFloat(this.maxString) : null;
  269. const inputMin = this.minString ? parseFloat(this.minString) : null;
  270. const valueNudgeDelayInMs = 100;
  271. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  272. if (this.nudgeNumberValueIntervalId) {
  273. window.clearInterval(this.nudgeNumberValueIntervalId);
  274. }
  275. let firstValueNudge = true;
  276. this.nudgeNumberValueIntervalId = window.setInterval(() => {
  277. if (firstValueNudge) {
  278. firstValueNudge = false;
  279. return;
  280. }
  281. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  282. }, valueNudgeDelayInMs);
  283. };
  284. this.nudgeButtonPointerUpAndOutHandler = (event) => {
  285. if (!isPrimaryPointerButton(event)) {
  286. return;
  287. }
  288. window.clearInterval(this.nudgeNumberValueIntervalId);
  289. };
  290. this.nudgeButtonPointerDownHandler = (event) => {
  291. if (!isPrimaryPointerButton(event)) {
  292. return;
  293. }
  294. event.preventDefault();
  295. const direction = event.target.dataset.adjustment;
  296. if (!this.disabled) {
  297. this.nudgeNumberValue(direction, event);
  298. }
  299. };
  300. this.hiddenInputChangeHandler = (event) => {
  301. if (event.target.name === this.name) {
  302. this.setNumberValue({
  303. value: event.target.value,
  304. origin: "direct"
  305. });
  306. }
  307. event.stopPropagation();
  308. };
  309. this.setChildNumberElRef = (el) => {
  310. this.childNumberEl = el;
  311. };
  312. this.setInputNumberValue = (newInputValue) => {
  313. if (!this.childNumberEl) {
  314. return;
  315. }
  316. this.childNumberEl.value = newInputValue;
  317. };
  318. this.setPreviousEmittedNumberValue = (newPreviousEmittedValue) => {
  319. this.previousEmittedValue = isValidNumber(newPreviousEmittedValue)
  320. ? newPreviousEmittedValue
  321. : "";
  322. };
  323. this.setPreviousNumberValue = (newPreviousValue) => {
  324. this.previousValue = isValidNumber(newPreviousValue) ? newPreviousValue : "";
  325. };
  326. this.setNumberValue = ({ committing = false, nativeEvent, origin, previousValue, value }) => {
  327. numberStringFormatter.numberFormatOptions = {
  328. locale: this.effectiveLocale,
  329. numberingSystem: this.numberingSystem,
  330. useGrouping: this.groupSeparator
  331. };
  332. const sanitizedValue = sanitizeNumberString((this.numberingSystem && this.numberingSystem !== "latn") || defaultNumberingSystem !== "latn"
  333. ? numberStringFormatter.delocalize(value)
  334. : value);
  335. const newValue = value && !sanitizedValue
  336. ? isValidNumber(this.previousValue)
  337. ? this.previousValue
  338. : ""
  339. : sanitizedValue;
  340. const newLocalizedValue = numberStringFormatter.localize(newValue);
  341. this.setPreviousNumberValue(previousValue || this.value);
  342. this.previousValueOrigin = origin;
  343. this.userChangedValue = origin === "user" && this.value !== newValue;
  344. this.value = newValue;
  345. this.localizedValue = newLocalizedValue;
  346. if (origin === "direct") {
  347. this.setInputNumberValue(newLocalizedValue);
  348. }
  349. if (nativeEvent) {
  350. const calciteInputNumberInputEvent = this.calciteInputNumberInput.emit({
  351. element: this.childNumberEl,
  352. nativeEvent,
  353. value: this.value
  354. });
  355. if (calciteInputNumberInputEvent.defaultPrevented) {
  356. const previousLocalizedValue = numberStringFormatter.localize(this.previousValue);
  357. this.value = this.previousValue;
  358. this.localizedValue = previousLocalizedValue;
  359. }
  360. else if (committing) {
  361. this.emitChangeIfUserModified();
  362. }
  363. }
  364. };
  365. this.inputNumberKeyUpHandler = () => {
  366. window.clearInterval(this.nudgeNumberValueIntervalId);
  367. };
  368. }
  369. disabledWatcher() {
  370. this.setDisabledAction();
  371. }
  372. localeChanged() {
  373. updateEffectiveLocale(this);
  374. }
  375. /** watcher to update number-to-string for max */
  376. maxWatcher() {
  377. var _a;
  378. this.maxString = ((_a = this.max) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  379. }
  380. /** watcher to update number-to-string for min */
  381. minWatcher() {
  382. var _a;
  383. this.minString = ((_a = this.min) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  384. }
  385. valueWatcher(newValue, previousValue) {
  386. if (!this.userChangedValue) {
  387. this.setNumberValue({
  388. origin: "direct",
  389. previousValue,
  390. value: newValue == null || newValue == ""
  391. ? ""
  392. : isValidNumber(newValue)
  393. ? newValue
  394. : this.previousValue || ""
  395. });
  396. this.warnAboutInvalidNumberValue(newValue);
  397. }
  398. this.userChangedValue = false;
  399. }
  400. updateRequestedIcon() {
  401. this.requestedIcon = setRequestedIcon({}, this.icon, "number");
  402. }
  403. get isClearable() {
  404. return this.clearable && this.value.length > 0;
  405. }
  406. //--------------------------------------------------------------------------
  407. //
  408. // Lifecycle
  409. //
  410. //--------------------------------------------------------------------------
  411. connectedCallback() {
  412. var _a;
  413. connectLocalized(this);
  414. this.scale = getElementProp(this.el, "scale", this.scale);
  415. this.status = getElementProp(this.el, "status", this.status);
  416. this.inlineEditableEl = this.el.closest("calcite-inline-editable");
  417. if (this.inlineEditableEl) {
  418. this.editingEnabled = this.inlineEditableEl.editingEnabled || false;
  419. }
  420. connectLabel(this);
  421. connectForm(this);
  422. this.setPreviousEmittedNumberValue(this.value);
  423. this.setPreviousNumberValue(this.value);
  424. this.warnAboutInvalidNumberValue(this.value);
  425. this.setNumberValue({
  426. origin: "connected",
  427. value: isValidNumber(this.value) ? this.value : ""
  428. });
  429. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  430. this.setDisabledAction();
  431. this.el.addEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  432. }
  433. disconnectedCallback() {
  434. var _a;
  435. disconnectLabel(this);
  436. disconnectForm(this);
  437. disconnectLocalized(this);
  438. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  439. this.el.removeEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  440. }
  441. componentWillLoad() {
  442. var _a, _b;
  443. this.maxString = (_a = this.max) === null || _a === void 0 ? void 0 : _a.toString();
  444. this.minString = (_b = this.min) === null || _b === void 0 ? void 0 : _b.toString();
  445. this.requestedIcon = setRequestedIcon({}, this.icon, "number");
  446. }
  447. componentShouldUpdate(newValue, oldValue, property) {
  448. if (property === "value" && newValue && !isValidNumber(newValue)) {
  449. this.setNumberValue({
  450. origin: "reset",
  451. value: oldValue
  452. });
  453. return false;
  454. }
  455. return true;
  456. }
  457. componentDidRender() {
  458. updateHostInteraction(this);
  459. }
  460. //--------------------------------------------------------------------------
  461. //
  462. // Public Methods
  463. //
  464. //--------------------------------------------------------------------------
  465. /** Sets focus on the component. */
  466. async setFocus() {
  467. var _a;
  468. (_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.focus();
  469. }
  470. /** Selects all text of the component's `value`. */
  471. async selectText() {
  472. var _a;
  473. (_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.select();
  474. }
  475. onLabelClick() {
  476. this.setFocus();
  477. }
  478. incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent) {
  479. const { value } = this;
  480. const inputStep = this.step === "any" ? 1 : Math.abs(this.step || 1);
  481. const inputVal = value && value !== "" ? parseFloat(value) : 0;
  482. const adjustment = direction === "up" ? 1 : -1;
  483. const nudgedValue = inputVal + inputStep * adjustment;
  484. const finalValue = (typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
  485. (typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
  486. ? inputVal
  487. : nudgedValue;
  488. const inputValPlaces = decimalPlaces(inputVal);
  489. const inputStepPlaces = decimalPlaces(inputStep);
  490. this.setNumberValue({
  491. committing: true,
  492. nativeEvent,
  493. origin: "user",
  494. value: finalValue.toFixed(Math.max(inputValPlaces, inputStepPlaces))
  495. });
  496. }
  497. onFormReset() {
  498. this.setNumberValue({
  499. origin: "reset",
  500. value: this.defaultValue
  501. });
  502. }
  503. syncHiddenFormInput(input) {
  504. var _a, _b, _c, _d;
  505. input.type = "number";
  506. input.min = (_b = (_a = this.min) === null || _a === void 0 ? void 0 : _a.toString(10)) !== null && _b !== void 0 ? _b : "";
  507. input.max = (_d = (_c = this.max) === null || _c === void 0 ? void 0 : _c.toString(10)) !== null && _d !== void 0 ? _d : "";
  508. }
  509. setDisabledAction() {
  510. const slottedActionEl = getSlotted(this.el, "action");
  511. if (!slottedActionEl) {
  512. return;
  513. }
  514. this.disabled
  515. ? slottedActionEl.setAttribute("disabled", "")
  516. : slottedActionEl.removeAttribute("disabled");
  517. }
  518. warnAboutInvalidNumberValue(value) {
  519. if (value && !isValidNumber(value)) {
  520. console.warn(`The specified value "${value}" cannot be parsed, or is out of range.`);
  521. }
  522. }
  523. // --------------------------------------------------------------------------
  524. //
  525. // Render Methods
  526. //
  527. // --------------------------------------------------------------------------
  528. render() {
  529. const dir = getElementDir(this.el);
  530. const loader = (h("div", { class: CSS.loader }, h("calcite-progress", { label: this.intlLoading, type: "indeterminate" })));
  531. 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" })));
  532. const iconEl = (h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: "s" }));
  533. const isHorizontalNumberButton = this.numberButtonType === "horizontal";
  534. const numberButtonsHorizontalUp = (h("button", { "aria-hidden": "true", class: {
  535. [CSS.numberButtonItem]: true,
  536. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  537. }, "data-adjustment": "up", disabled: this.disabled || this.readOnly, onPointerDown: this.nudgeButtonPointerDownHandler, onPointerOut: this.nudgeButtonPointerUpAndOutHandler, onPointerUp: this.nudgeButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-up", scale: "s" })));
  538. const numberButtonsHorizontalDown = (h("button", { "aria-hidden": "true", class: {
  539. [CSS.numberButtonItem]: true,
  540. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  541. }, "data-adjustment": "down", disabled: this.disabled || this.readOnly, onPointerDown: this.nudgeButtonPointerDownHandler, onPointerOut: this.nudgeButtonPointerUpAndOutHandler, onPointerUp: this.nudgeButtonPointerUpAndOutHandler, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "chevron-down", scale: "s" })));
  542. const numberButtonsVertical = (h("div", { class: CSS.numberButtonWrapper }, numberButtonsHorizontalUp, numberButtonsHorizontalDown));
  543. const prefixText = h("div", { class: CSS.prefix }, this.prefixText);
  544. const suffixText = h("div", { class: CSS.suffix }, this.suffixText);
  545. const childEl = (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.inputNumberBlurHandler, onFocus: this.inputNumberFocusHandler, onInput: this.inputNumberInputHandler, onKeyDown: this.inputNumberKeyDownHandler, onKeyUp: this.inputNumberKeyUpHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildNumberElRef, type: "text", value: this.localizedValue }));
  546. return (h(Host, { onClick: this.inputNumberFocusHandler, onKeyDown: this.keyDownHandler }, h("div", { class: { [CSS.inputWrapper]: true, [CSS_UTILITY.rtl]: dir === "rtl" } }, this.numberButtonType === "horizontal" && !this.readOnly
  547. ? numberButtonsHorizontalDown
  548. : null, this.prefixText ? prefixText : null, h("div", { class: CSS.wrapper }, childEl, this.isClearable ? inputClearButton : null, this.requestedIcon ? iconEl : null, this.loading ? loader : null), h("div", { class: CSS.actionWrapper }, h("slot", { name: SLOTS.action })), this.numberButtonType === "vertical" && !this.readOnly ? numberButtonsVertical : null, this.suffixText ? suffixText : null, this.numberButtonType === "horizontal" && !this.readOnly
  549. ? numberButtonsHorizontalUp
  550. : null, h(HiddenFormInputSlot, { component: this }))));
  551. }
  552. get el() { return getElement(this); }
  553. static get watchers() { return {
  554. "disabled": ["disabledWatcher"],
  555. "locale": ["localeChanged"],
  556. "max": ["maxWatcher"],
  557. "min": ["minWatcher"],
  558. "value": ["valueWatcher"],
  559. "icon": ["updateRequestedIcon"]
  560. }; }
  561. };
  562. InputNumber.style = inputNumberCss;
  563. export { InputNumber as calcite_input_number };