calcite-input-number.cjs.entry.js 33 KB

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