@react-stately/numberfield 3.9.0 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.js CHANGED
@@ -1,19 +1,6 @@
1
- import {snapValueToStep as $vhjCi$snapValueToStep, clamp as $vhjCi$clamp, useControlledState as $vhjCi$useControlledState} from "@react-stately/utils";
2
- import {useFormValidationState as $vhjCi$useFormValidationState} from "@react-stately/form";
3
- import {NumberFormatter as $vhjCi$NumberFormatter, NumberParser as $vhjCi$NumberParser} from "@internationalized/number";
4
- import {useState as $vhjCi$useState, useMemo as $vhjCi$useMemo, useCallback as $vhjCi$useCallback} from "react";
1
+ import {useNumberFieldState as $de67e98908f0c6ee$export$7f629e9dc1ecf37c} from "./useNumberFieldState.module.js";
5
2
 
6
3
  /*
7
- * Copyright 2020 Adobe. All rights reserved.
8
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License. You may obtain a copy
10
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software distributed under
13
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
- * OF ANY KIND, either express or implied. See the License for the specific language
15
- * governing permissions and limitations under the License.
16
- */ /*
17
4
  * Copyright 2020 Adobe. All rights reserved.
18
5
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
19
6
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -26,185 +13,5 @@ import {useState as $vhjCi$useState, useMemo as $vhjCi$useMemo, useCallback as $
26
13
  */
27
14
 
28
15
 
29
-
30
- function $de67e98908f0c6ee$export$7f629e9dc1ecf37c(props) {
31
- let { minValue: minValue, maxValue: maxValue, step: step, formatOptions: formatOptions, value: value, defaultValue: defaultValue = NaN, onChange: onChange, locale: locale, isDisabled: isDisabled, isReadOnly: isReadOnly } = props;
32
- if (value === null) value = NaN;
33
- if (value !== undefined && !isNaN(value)) {
34
- if (step !== undefined && !isNaN(step)) value = (0, $vhjCi$snapValueToStep)(value, minValue, maxValue, step);
35
- else value = (0, $vhjCi$clamp)(value, minValue, maxValue);
36
- }
37
- if (!isNaN(defaultValue)) {
38
- if (step !== undefined && !isNaN(step)) defaultValue = (0, $vhjCi$snapValueToStep)(defaultValue, minValue, maxValue, step);
39
- else defaultValue = (0, $vhjCi$clamp)(defaultValue, minValue, maxValue);
40
- }
41
- let [numberValue, setNumberValue] = (0, $vhjCi$useControlledState)(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);
42
- let [inputValue, setInputValue] = (0, $vhjCi$useState)(()=>isNaN(numberValue) ? "" : new (0, $vhjCi$NumberFormatter)(locale, formatOptions).format(numberValue));
43
- let numberParser = (0, $vhjCi$useMemo)(()=>new (0, $vhjCi$NumberParser)(locale, formatOptions), [
44
- locale,
45
- formatOptions
46
- ]);
47
- let numberingSystem = (0, $vhjCi$useMemo)(()=>numberParser.getNumberingSystem(inputValue), [
48
- numberParser,
49
- inputValue
50
- ]);
51
- let formatter = (0, $vhjCi$useMemo)(()=>new (0, $vhjCi$NumberFormatter)(locale, {
52
- ...formatOptions,
53
- numberingSystem: numberingSystem
54
- }), [
55
- locale,
56
- formatOptions,
57
- numberingSystem
58
- ]);
59
- let intlOptions = (0, $vhjCi$useMemo)(()=>formatter.resolvedOptions(), [
60
- formatter
61
- ]);
62
- let format = (0, $vhjCi$useCallback)((value)=>isNaN(value) || value === null ? "" : formatter.format(value), [
63
- formatter
64
- ]);
65
- let validation = (0, $vhjCi$useFormValidationState)({
66
- ...props,
67
- value: numberValue
68
- });
69
- let clampStep = step !== undefined && !isNaN(step) ? step : 1;
70
- if (intlOptions.style === "percent" && (step === undefined || isNaN(step))) clampStep = 0.01;
71
- // Update the input value when the number value or format options change. This is done
72
- // in a useEffect so that the controlled behavior is correct and we only update the
73
- // textfield after prop changes.
74
- let [prevValue, setPrevValue] = (0, $vhjCi$useState)(numberValue);
75
- let [prevLocale, setPrevLocale] = (0, $vhjCi$useState)(locale);
76
- let [prevFormatOptions, setPrevFormatOptions] = (0, $vhjCi$useState)(formatOptions);
77
- if (!Object.is(numberValue, prevValue) || locale !== prevLocale || formatOptions !== prevFormatOptions) {
78
- setInputValue(format(numberValue));
79
- setPrevValue(numberValue);
80
- setPrevLocale(locale);
81
- setPrevFormatOptions(formatOptions);
82
- }
83
- let parsedValue = (0, $vhjCi$useMemo)(()=>numberParser.parse(inputValue), [
84
- numberParser,
85
- inputValue
86
- ]);
87
- let commit = ()=>{
88
- // Set to empty state if input value is empty
89
- if (!inputValue.length) {
90
- setNumberValue(NaN);
91
- setInputValue(value === undefined ? "" : format(numberValue));
92
- return;
93
- }
94
- // if it failed to parse, then reset input to formatted version of current number
95
- if (isNaN(parsedValue)) {
96
- setInputValue(format(numberValue));
97
- return;
98
- }
99
- // Clamp to min and max, round to the nearest step, and round to specified number of digits
100
- let clampedValue;
101
- if (step === undefined || isNaN(step)) clampedValue = (0, $vhjCi$clamp)(parsedValue, minValue, maxValue);
102
- else clampedValue = (0, $vhjCi$snapValueToStep)(parsedValue, minValue, maxValue, step);
103
- clampedValue = numberParser.parse(format(clampedValue));
104
- setNumberValue(clampedValue);
105
- // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
106
- setInputValue(format(value === undefined ? clampedValue : numberValue));
107
- };
108
- let safeNextStep = (operation, minMax = 0)=>{
109
- let prev = parsedValue;
110
- if (isNaN(prev)) {
111
- // if the input is empty, start from the min/max value when incrementing/decrementing,
112
- // or zero if there is no min/max value defined.
113
- let newValue = isNaN(minMax) ? 0 : minMax;
114
- return (0, $vhjCi$snapValueToStep)(newValue, minValue, maxValue, clampStep);
115
- } else {
116
- // otherwise, first snap the current value to the nearest step. if it moves in the direction
117
- // we're going, use that value, otherwise add the step and snap that value.
118
- let newValue = (0, $vhjCi$snapValueToStep)(prev, minValue, maxValue, clampStep);
119
- if (operation === "+" && newValue > prev || operation === "-" && newValue < prev) return newValue;
120
- return (0, $vhjCi$snapValueToStep)($de67e98908f0c6ee$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
121
- }
122
- };
123
- let increment = ()=>{
124
- let newValue = safeNextStep("+", minValue);
125
- // if we've arrived at the same value that was previously in the state, the
126
- // input value should be updated to match
127
- // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
128
- // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
129
- if (newValue === numberValue) setInputValue(format(newValue));
130
- setNumberValue(newValue);
131
- validation.commitValidation();
132
- };
133
- let decrement = ()=>{
134
- let newValue = safeNextStep("-", maxValue);
135
- if (newValue === numberValue) setInputValue(format(newValue));
136
- setNumberValue(newValue);
137
- validation.commitValidation();
138
- };
139
- let incrementToMax = ()=>{
140
- if (maxValue != null) {
141
- setNumberValue((0, $vhjCi$snapValueToStep)(maxValue, minValue, maxValue, clampStep));
142
- validation.commitValidation();
143
- }
144
- };
145
- let decrementToMin = ()=>{
146
- if (minValue != null) {
147
- setNumberValue(minValue);
148
- validation.commitValidation();
149
- }
150
- };
151
- let canIncrement = (0, $vhjCi$useMemo)(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || maxValue === undefined || isNaN(maxValue) || (0, $vhjCi$snapValueToStep)(parsedValue, minValue, maxValue, clampStep) > parsedValue || $de67e98908f0c6ee$var$handleDecimalOperation("+", parsedValue, clampStep) <= maxValue), [
152
- isDisabled,
153
- isReadOnly,
154
- minValue,
155
- maxValue,
156
- clampStep,
157
- parsedValue
158
- ]);
159
- let canDecrement = (0, $vhjCi$useMemo)(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || minValue === undefined || isNaN(minValue) || (0, $vhjCi$snapValueToStep)(parsedValue, minValue, maxValue, clampStep) < parsedValue || $de67e98908f0c6ee$var$handleDecimalOperation("-", parsedValue, clampStep) >= minValue), [
160
- isDisabled,
161
- isReadOnly,
162
- minValue,
163
- maxValue,
164
- clampStep,
165
- parsedValue
166
- ]);
167
- let validate = (value)=>numberParser.isValidPartialNumber(value, minValue, maxValue);
168
- return {
169
- ...validation,
170
- validate: validate,
171
- increment: increment,
172
- incrementToMax: incrementToMax,
173
- decrement: decrement,
174
- decrementToMin: decrementToMin,
175
- canIncrement: canIncrement,
176
- canDecrement: canDecrement,
177
- minValue: minValue,
178
- maxValue: maxValue,
179
- numberValue: parsedValue,
180
- setNumberValue: setNumberValue,
181
- setInputValue: setInputValue,
182
- inputValue: inputValue,
183
- commit: commit
184
- };
185
- }
186
- function $de67e98908f0c6ee$var$handleDecimalOperation(operator, value1, value2) {
187
- let result = operator === "+" ? value1 + value2 : value1 - value2;
188
- // Check if we have decimals
189
- if (value1 % 1 !== 0 || value2 % 1 !== 0) {
190
- const value1Decimal = value1.toString().split(".");
191
- const value2Decimal = value2.toString().split(".");
192
- const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
193
- const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
194
- const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));
195
- // Transform the decimals to integers based on the precision
196
- value1 = Math.round(value1 * multiplier);
197
- value2 = Math.round(value2 * multiplier);
198
- // Perform the operation on integers values to make sure we don't get a fancy decimal value
199
- result = operator === "+" ? value1 + value2 : value1 - value2;
200
- // Transform the integer result back to decimal
201
- result /= multiplier;
202
- }
203
- return result;
204
- }
205
-
206
-
207
-
208
-
209
16
  export {$de67e98908f0c6ee$export$7f629e9dc1ecf37c as useNumberFieldState};
210
17
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;;;AAkEM,SAAS,0CACd,KAA8B;IAE9B,IAAI,YACF,QAAQ,YACR,QAAQ,QACR,IAAI,iBACJ,aAAa,SACb,KAAK,gBACL,eAAe,eACf,QAAQ,UACR,MAAM,cACN,UAAU,cACV,UAAU,EACX,GAAG;IAEJ,IAAI,UAAU,MACZ,QAAQ;IAGV,IAAI,UAAU,aAAa,CAAC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KAAK,aAAa,cAAc,QAAO,GAE/D;QAAC;QAAY;QAAY;QAAU;QAAU;QAAW;KAAY;IAEvE,IAAI,WAAW,CAAC,QAAkB,aAAa,oBAAoB,CAAC,OAAO,UAAU;IAErF,OAAO;QACL,GAAG,UAAU;kBACb;mBACA;wBACA;mBACA;wBACA;sBACA;sBACA;kBACA;kBACA;QACA,aAAa;wBACb;uBACA;oBACA;gBACA;IACF;AACF;AAEA,SAAS,6CAAuB,QAAmB,EAAE,MAAc,EAAE,MAAc;IACjF,IAAI,SAAS,aAAa,MAAM,SAAS,SAAS,SAAS;IAE3D,4BAA4B;IAC5B,IAAI,SAAS,MAAM,KAAK,SAAS,MAAM,GAAG;QACxC,MAAM,gBAAgB,OAAO,QAAQ,GAAG,KAAK,CAAC;QAC9C,MAAM,gBAAgB,OAAO,QAAQ,GAAG,KAAK,CAAC;QAC9C,MAAM,sBAAsB,AAAC,aAAa,CAAC,EAAE,IAAI,aAAa,CAAC,EAAE,CAAC,MAAM,IAAK;QAC7E,MAAM,sBAAsB,AAAC,aAAa,CAAC,EAAE,IAAI,aAAa,CAAC,EAAE,CAAC,MAAM,IAAK;QAC7E,MAAM,aAAa,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,qBAAqB;QAE9D,4DAA4D;QAC5D,SAAS,KAAK,KAAK,CAAC,SAAS;QAC7B,SAAS,KAAK,KAAK,CAAC,SAAS;QAE7B,2FAA2F;QAC3F,SAAS,aAAa,MAAM,SAAS,SAAS,SAAS;QAEvD,+CAA+C;QAC/C,UAAU;IACZ;IAEA,OAAO;AACT;;CDvSC","sources":["packages/@react-stately/numberfield/src/index.ts","packages/@react-stately/numberfield/src/useNumberFieldState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useNumberFieldState} from './useNumberFieldState';\n\nexport type {NumberFieldStateOptions} from './useNumberFieldState';\nexport type {NumberFieldState} from './useNumberFieldState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useMemo, useState} from 'react';\n\nexport interface NumberFieldState extends FormValidationState {\n /**\n * The current text value of the input. Updated as the user types,\n * and formatted according to `formatOptions` on blur.\n */\n inputValue: string,\n /**\n * The currently parsed number value, or NaN if a valid number could not be parsed.\n * Updated based on the `inputValue` as the user types.\n */\n numberValue: number,\n /** The minimum value of the number field. */\n minValue?: number,\n /** The maximum value of the number field. */\n maxValue?: number,\n /** Whether the current value can be incremented according to the maximum value and step. */\n canIncrement: boolean,\n /** Whether the current value can be decremented according to the minimum value and step. */\n canDecrement: boolean,\n /**\n * Validates a user input string according to the current locale and format options.\n * Values can be partially entered, and may be valid even if they cannot currently be parsed to a number.\n * Can be used to implement validation as a user types.\n */\n validate(value: string): boolean,\n /** Sets the current text value of the input. */\n setInputValue(val: string): void,\n /** Sets the number value. */\n setNumberValue(val: number): void,\n /**\n * Commits the current input value. The value is parsed to a number, clamped according\n * to the minimum and maximum values of the field, and snapped to the nearest step value.\n * This will fire the `onChange` prop with the new value, and if uncontrolled, update the `numberValue`.\n * Typically this is called when the field is blurred.\n */\n commit(): void,\n /** Increments the current input value to the next step boundary, and fires `onChange`. */\n increment(): void,\n /** Decrements the current input value to the next step boundary, and fires `onChange`. */\n decrement(): void,\n /** Sets the current value to the `maxValue` if any, and fires `onChange`. */\n incrementToMax(): void,\n /** Sets the current value to the `minValue` if any, and fires `onChange`. */\n decrementToMin(): void\n}\n\nexport interface NumberFieldStateOptions extends NumberFieldProps {\n /**\n * The locale that should be used for parsing.\n * @default 'en-US'\n */\n locale: string\n}\n\n/**\n * Provides state management for a number field component. Number fields allow users to enter a number,\n * and increment or decrement the value using stepper buttons.\n */\nexport function useNumberFieldState(\n props: NumberFieldStateOptions\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue = NaN,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\n\n if (value === null) {\n value = NaN;\n }\n\n if (value !== undefined && !isNaN(value)) {\n if (step !== undefined && !isNaN(step)) {\n value = snapValueToStep(value, minValue, maxValue, step);\n } else {\n value = clamp(value, minValue, maxValue);\n }\n }\n\n if (!isNaN(defaultValue)) {\n if (step !== undefined && !isNaN(step)) {\n defaultValue = snapValueToStep(defaultValue, minValue, maxValue, step);\n } else {\n defaultValue = clamp(defaultValue, minValue, maxValue);\n }\n }\n\n let [numberValue, setNumberValue] = useControlledState<number>(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);\n let [inputValue, setInputValue] = useState(() => isNaN(numberValue) ? '' : new NumberFormatter(locale, formatOptions).format(numberValue));\n\n let numberParser = useMemo(() => new NumberParser(locale, formatOptions), [locale, formatOptions]);\n let numberingSystem = useMemo(() => numberParser.getNumberingSystem(inputValue), [numberParser, inputValue]);\n let formatter = useMemo(() => new NumberFormatter(locale, {...formatOptions, numberingSystem}), [locale, formatOptions, numberingSystem]);\n let intlOptions = useMemo(() => formatter.resolvedOptions(), [formatter]);\n let format = useCallback((value: number) => (isNaN(value) || value === null) ? '' : formatter.format(value), [formatter]);\n\n let validation = useFormValidationState({\n ...props,\n value: numberValue\n });\n\n let clampStep = (step !== undefined && !isNaN(step)) ? step : 1;\n if (intlOptions.style === 'percent' && (step === undefined || isNaN(step))) {\n clampStep = 0.01;\n }\n\n // Update the input value when the number value or format options change. This is done\n // in a useEffect so that the controlled behavior is correct and we only update the\n // textfield after prop changes.\n let [prevValue, setPrevValue] = useState(numberValue);\n let [prevLocale, setPrevLocale] = useState(locale);\n let [prevFormatOptions, setPrevFormatOptions] = useState(formatOptions);\n if (!Object.is(numberValue, prevValue) || locale !== prevLocale || formatOptions !== prevFormatOptions) {\n setInputValue(format(numberValue));\n setPrevValue(numberValue);\n setPrevLocale(locale);\n setPrevFormatOptions(formatOptions);\n }\n\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let commit = () => {\n // Set to empty state if input value is empty\n if (!inputValue.length) {\n setNumberValue(NaN);\n setInputValue(value === undefined ? '' : format(numberValue));\n return;\n }\n\n // if it failed to parse, then reset input to formatted version of current number\n if (isNaN(parsedValue)) {\n setInputValue(format(numberValue));\n return;\n }\n\n // Clamp to min and max, round to the nearest step, and round to specified number of digits\n let clampedValue: number;\n if (step === undefined || isNaN(step)) {\n clampedValue = clamp(parsedValue, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsedValue, minValue, maxValue, step);\n }\n\n clampedValue = numberParser.parse(format(clampedValue));\n setNumberValue(clampedValue);\n\n // in a controlled state, the numberValue won't change, so we won't go back to our old input without help\n setInputValue(format(value === undefined ? clampedValue : numberValue));\n };\n\n let safeNextStep = (operation: '+' | '-', minMax: number = 0) => {\n let prev = parsedValue;\n\n if (isNaN(prev)) {\n // if the input is empty, start from the min/max value when incrementing/decrementing,\n // or zero if there is no min/max value defined.\n let newValue = isNaN(minMax) ? 0 : minMax;\n return snapValueToStep(newValue, minValue, maxValue, clampStep);\n } else {\n // otherwise, first snap the current value to the nearest step. if it moves in the direction\n // we're going, use that value, otherwise add the step and snap that value.\n let newValue = snapValueToStep(prev, minValue, maxValue, clampStep);\n if ((operation === '+' && newValue > prev) || (operation === '-' && newValue < prev)) {\n return newValue;\n }\n\n return snapValueToStep(\n handleDecimalOperation(operation, prev, clampStep),\n minValue,\n maxValue,\n clampStep\n );\n }\n };\n\n let increment = () => {\n let newValue = safeNextStep('+', minValue);\n\n // if we've arrived at the same value that was previously in the state, the\n // input value should be updated to match\n // ex type 4, press increment, highlight the number in the input, type 4 again, press increment\n // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n validation.commitValidation();\n };\n\n let decrement = () => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n validation.commitValidation();\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n validation.commitValidation();\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n validation.commitValidation();\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n (maxValue === undefined || isNaN(maxValue)) ||\n snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue ||\n handleDecimalOperation('+', parsedValue, clampStep) <= maxValue\n )\n ), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);\n\n let canDecrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n (minValue === undefined || isNaN(minValue)) ||\n snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue ||\n handleDecimalOperation('-', parsedValue, clampStep) >= minValue\n )\n ), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);\n\n let validate = (value: string) => numberParser.isValidPartialNumber(value, minValue, maxValue);\n\n return {\n ...validation,\n validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\n setNumberValue,\n setInputValue,\n inputValue,\n commit\n };\n}\n\nfunction handleDecimalOperation(operator: '-' | '+', value1: number, value2: number): number {\n let result = operator === '+' ? value1 + value2 : value1 - value2;\n\n // Check if we have decimals\n if (value1 % 1 !== 0 || value2 % 1 !== 0) {\n const value1Decimal = value1.toString().split('.');\n const value2Decimal = value2.toString().split('.');\n const value1DecimalLength = (value1Decimal[1] && value1Decimal[1].length) || 0;\n const value2DecimalLength = (value2Decimal[1] && value2Decimal[1].length) || 0;\n const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));\n\n // Transform the decimals to integers based on the precision\n value1 = Math.round(value1 * multiplier);\n value2 = Math.round(value2 * multiplier);\n\n // Perform the operation on integers values to make sure we don't get a fancy decimal value\n result = operator === '+' ? value1 + value2 : value1 - value2;\n\n // Transform the integer result back to decimal\n result /= multiplier;\n }\n\n return result;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/numberfield/src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useNumberFieldState} from './useNumberFieldState';\n\nexport type {NumberFieldStateOptions} from './useNumberFieldState';\nexport type {NumberFieldState} from './useNumberFieldState';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,203 @@
1
+ var $gPCRT$reactstatelyutils = require("@react-stately/utils");
2
+ var $gPCRT$reactstatelyform = require("@react-stately/form");
3
+ var $gPCRT$internationalizednumber = require("@internationalized/number");
4
+ var $gPCRT$react = require("react");
5
+
6
+
7
+ function $parcel$export(e, n, v, s) {
8
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
9
+ }
10
+
11
+ $parcel$export(module.exports, "useNumberFieldState", () => $e18a693eeaf9c060$export$7f629e9dc1ecf37c);
12
+ /*
13
+ * Copyright 2020 Adobe. All rights reserved.
14
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
15
+ * you may not use this file except in compliance with the License. You may obtain a copy
16
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software distributed under
19
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
20
+ * OF ANY KIND, either express or implied. See the License for the specific language
21
+ * governing permissions and limitations under the License.
22
+ */
23
+
24
+
25
+
26
+ function $e18a693eeaf9c060$export$7f629e9dc1ecf37c(props) {
27
+ let { minValue: minValue, maxValue: maxValue, step: step, formatOptions: formatOptions, value: value, defaultValue: defaultValue = NaN, onChange: onChange, locale: locale, isDisabled: isDisabled, isReadOnly: isReadOnly } = props;
28
+ if (value === null) value = NaN;
29
+ if (value !== undefined && !isNaN(value)) {
30
+ if (step !== undefined && !isNaN(step)) value = (0, $gPCRT$reactstatelyutils.snapValueToStep)(value, minValue, maxValue, step);
31
+ else value = (0, $gPCRT$reactstatelyutils.clamp)(value, minValue, maxValue);
32
+ }
33
+ if (!isNaN(defaultValue)) {
34
+ if (step !== undefined && !isNaN(step)) defaultValue = (0, $gPCRT$reactstatelyutils.snapValueToStep)(defaultValue, minValue, maxValue, step);
35
+ else defaultValue = (0, $gPCRT$reactstatelyutils.clamp)(defaultValue, minValue, maxValue);
36
+ }
37
+ let [numberValue, setNumberValue] = (0, $gPCRT$reactstatelyutils.useControlledState)(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);
38
+ let [inputValue, setInputValue] = (0, $gPCRT$react.useState)(()=>isNaN(numberValue) ? "" : new (0, $gPCRT$internationalizednumber.NumberFormatter)(locale, formatOptions).format(numberValue));
39
+ let numberParser = (0, $gPCRT$react.useMemo)(()=>new (0, $gPCRT$internationalizednumber.NumberParser)(locale, formatOptions), [
40
+ locale,
41
+ formatOptions
42
+ ]);
43
+ let numberingSystem = (0, $gPCRT$react.useMemo)(()=>numberParser.getNumberingSystem(inputValue), [
44
+ numberParser,
45
+ inputValue
46
+ ]);
47
+ let formatter = (0, $gPCRT$react.useMemo)(()=>new (0, $gPCRT$internationalizednumber.NumberFormatter)(locale, {
48
+ ...formatOptions,
49
+ numberingSystem: numberingSystem
50
+ }), [
51
+ locale,
52
+ formatOptions,
53
+ numberingSystem
54
+ ]);
55
+ let intlOptions = (0, $gPCRT$react.useMemo)(()=>formatter.resolvedOptions(), [
56
+ formatter
57
+ ]);
58
+ let format = (0, $gPCRT$react.useCallback)((value)=>isNaN(value) || value === null ? "" : formatter.format(value), [
59
+ formatter
60
+ ]);
61
+ let validation = (0, $gPCRT$reactstatelyform.useFormValidationState)({
62
+ ...props,
63
+ value: numberValue
64
+ });
65
+ let clampStep = step !== undefined && !isNaN(step) ? step : 1;
66
+ if (intlOptions.style === "percent" && (step === undefined || isNaN(step))) clampStep = 0.01;
67
+ // Update the input value when the number value or format options change. This is done
68
+ // in a useEffect so that the controlled behavior is correct and we only update the
69
+ // textfield after prop changes.
70
+ let [prevValue, setPrevValue] = (0, $gPCRT$react.useState)(numberValue);
71
+ let [prevLocale, setPrevLocale] = (0, $gPCRT$react.useState)(locale);
72
+ let [prevFormatOptions, setPrevFormatOptions] = (0, $gPCRT$react.useState)(formatOptions);
73
+ if (!Object.is(numberValue, prevValue) || locale !== prevLocale || formatOptions !== prevFormatOptions) {
74
+ setInputValue(format(numberValue));
75
+ setPrevValue(numberValue);
76
+ setPrevLocale(locale);
77
+ setPrevFormatOptions(formatOptions);
78
+ }
79
+ let parsedValue = (0, $gPCRT$react.useMemo)(()=>numberParser.parse(inputValue), [
80
+ numberParser,
81
+ inputValue
82
+ ]);
83
+ let commit = ()=>{
84
+ // Set to empty state if input value is empty
85
+ if (!inputValue.length) {
86
+ setNumberValue(NaN);
87
+ setInputValue(value === undefined ? "" : format(numberValue));
88
+ return;
89
+ }
90
+ // if it failed to parse, then reset input to formatted version of current number
91
+ if (isNaN(parsedValue)) {
92
+ setInputValue(format(numberValue));
93
+ return;
94
+ }
95
+ // Clamp to min and max, round to the nearest step, and round to specified number of digits
96
+ let clampedValue;
97
+ if (step === undefined || isNaN(step)) clampedValue = (0, $gPCRT$reactstatelyutils.clamp)(parsedValue, minValue, maxValue);
98
+ else clampedValue = (0, $gPCRT$reactstatelyutils.snapValueToStep)(parsedValue, minValue, maxValue, step);
99
+ clampedValue = numberParser.parse(format(clampedValue));
100
+ setNumberValue(clampedValue);
101
+ // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
102
+ setInputValue(format(value === undefined ? clampedValue : numberValue));
103
+ };
104
+ let safeNextStep = (operation, minMax = 0)=>{
105
+ let prev = parsedValue;
106
+ if (isNaN(prev)) {
107
+ // if the input is empty, start from the min/max value when incrementing/decrementing,
108
+ // or zero if there is no min/max value defined.
109
+ let newValue = isNaN(minMax) ? 0 : minMax;
110
+ return (0, $gPCRT$reactstatelyutils.snapValueToStep)(newValue, minValue, maxValue, clampStep);
111
+ } else {
112
+ // otherwise, first snap the current value to the nearest step. if it moves in the direction
113
+ // we're going, use that value, otherwise add the step and snap that value.
114
+ let newValue = (0, $gPCRT$reactstatelyutils.snapValueToStep)(prev, minValue, maxValue, clampStep);
115
+ if (operation === "+" && newValue > prev || operation === "-" && newValue < prev) return newValue;
116
+ return (0, $gPCRT$reactstatelyutils.snapValueToStep)($e18a693eeaf9c060$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
117
+ }
118
+ };
119
+ let increment = ()=>{
120
+ let newValue = safeNextStep("+", minValue);
121
+ // if we've arrived at the same value that was previously in the state, the
122
+ // input value should be updated to match
123
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
124
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
125
+ if (newValue === numberValue) setInputValue(format(newValue));
126
+ setNumberValue(newValue);
127
+ validation.commitValidation();
128
+ };
129
+ let decrement = ()=>{
130
+ let newValue = safeNextStep("-", maxValue);
131
+ if (newValue === numberValue) setInputValue(format(newValue));
132
+ setNumberValue(newValue);
133
+ validation.commitValidation();
134
+ };
135
+ let incrementToMax = ()=>{
136
+ if (maxValue != null) {
137
+ setNumberValue((0, $gPCRT$reactstatelyutils.snapValueToStep)(maxValue, minValue, maxValue, clampStep));
138
+ validation.commitValidation();
139
+ }
140
+ };
141
+ let decrementToMin = ()=>{
142
+ if (minValue != null) {
143
+ setNumberValue(minValue);
144
+ validation.commitValidation();
145
+ }
146
+ };
147
+ let canIncrement = (0, $gPCRT$react.useMemo)(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || maxValue === undefined || isNaN(maxValue) || (0, $gPCRT$reactstatelyutils.snapValueToStep)(parsedValue, minValue, maxValue, clampStep) > parsedValue || $e18a693eeaf9c060$var$handleDecimalOperation("+", parsedValue, clampStep) <= maxValue), [
148
+ isDisabled,
149
+ isReadOnly,
150
+ minValue,
151
+ maxValue,
152
+ clampStep,
153
+ parsedValue
154
+ ]);
155
+ let canDecrement = (0, $gPCRT$react.useMemo)(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || minValue === undefined || isNaN(minValue) || (0, $gPCRT$reactstatelyutils.snapValueToStep)(parsedValue, minValue, maxValue, clampStep) < parsedValue || $e18a693eeaf9c060$var$handleDecimalOperation("-", parsedValue, clampStep) >= minValue), [
156
+ isDisabled,
157
+ isReadOnly,
158
+ minValue,
159
+ maxValue,
160
+ clampStep,
161
+ parsedValue
162
+ ]);
163
+ let validate = (value)=>numberParser.isValidPartialNumber(value, minValue, maxValue);
164
+ return {
165
+ ...validation,
166
+ validate: validate,
167
+ increment: increment,
168
+ incrementToMax: incrementToMax,
169
+ decrement: decrement,
170
+ decrementToMin: decrementToMin,
171
+ canIncrement: canIncrement,
172
+ canDecrement: canDecrement,
173
+ minValue: minValue,
174
+ maxValue: maxValue,
175
+ numberValue: parsedValue,
176
+ setNumberValue: setNumberValue,
177
+ setInputValue: setInputValue,
178
+ inputValue: inputValue,
179
+ commit: commit
180
+ };
181
+ }
182
+ function $e18a693eeaf9c060$var$handleDecimalOperation(operator, value1, value2) {
183
+ let result = operator === "+" ? value1 + value2 : value1 - value2;
184
+ // Check if we have decimals
185
+ if (value1 % 1 !== 0 || value2 % 1 !== 0) {
186
+ const value1Decimal = value1.toString().split(".");
187
+ const value2Decimal = value2.toString().split(".");
188
+ const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
189
+ const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
190
+ const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));
191
+ // Transform the decimals to integers based on the precision
192
+ value1 = Math.round(value1 * multiplier);
193
+ value2 = Math.round(value2 * multiplier);
194
+ // Perform the operation on integers values to make sure we don't get a fancy decimal value
195
+ result = operator === "+" ? value1 + value2 : value1 - value2;
196
+ // Transform the integer result back to decimal
197
+ result /= multiplier;
198
+ }
199
+ return result;
200
+ }
201
+
202
+
203
+ //# sourceMappingURL=useNumberFieldState.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;AAkEM,SAAS,0CACd,KAA8B;IAE9B,IAAI,YACF,QAAQ,YACR,QAAQ,QACR,IAAI,iBACJ,aAAa,SACb,KAAK,gBACL,eAAe,eACf,QAAQ,UACR,MAAM,cACN,UAAU,cACV,UAAU,EACX,GAAG;IAEJ,IAAI,UAAU,MACZ,QAAQ;IAGV,IAAI,UAAU,aAAa,CAAC,MAAM;QAChC,IAAI,SAAS,aAAa,CAAC,MAAM,O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/D;QAAC;QAAY;QAAY;QAAU;QAAU;QAAW;KAAY;IAEvE,IAAI,WAAW,CAAC,QAAkB,aAAa,oBAAoB,CAAC,OAAO,UAAU;IAErF,OAAO;QACL,GAAG,UAAU;kBACb;mBACA;wBACA;mBACA;wBACA;sBACA;sBACA;kBACA;kBACA;QACA,aAAa;wBACb;uBACA;oBACA;gBACA;IACF;AACF;AAEA,SAAS,6CAAuB,QAAmB,EAAE,MAAc,EAAE,MAAc;IACjF,IAAI,SAAS,aAAa,MAAM,SAAS,SAAS,SAAS;IAE3D,4BAA4B;IAC5B,IAAI,SAAS,MAAM,KAAK,SAAS,MAAM,GAAG;QACxC,MAAM,gBAAgB,OAAO,QAAQ,GAAG,KAAK,CAAC;QAC9C,MAAM,gBAAgB,OAAO,QAAQ,GAAG,KAAK,CAAC;QAC9C,MAAM,sBAAsB,AAAC,aAAa,CAAC,EAAE,IAAI,aAAa,CAAC,EAAE,CAAC,MAAM,IAAK;QAC7E,MAAM,sBAAsB,AAAC,aAAa,CAAC,EAAE,IAAI,aAAa,CAAC,EAAE,CAAC,MAAM,IAAK;QAC7E,MAAM,aAAa,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,qBAAqB;QAE9D,4DAA4D;QAC5D,SAAS,KAAK,KAAK,CAAC,SAAS;QAC7B,SAAS,KAAK,KAAK,CAAC,SAAS;QAE7B,2FAA2F;QAC3F,SAAS,aAAa,MAAM,SAAS,SAAS,SAAS;QAEvD,+CAA+C;QAC/C,UAAU;IACZ;IAEA,OAAO;AACT","sources":["packages/@react-stately/numberfield/src/useNumberFieldState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useMemo, useState} from 'react';\n\nexport interface NumberFieldState extends FormValidationState {\n /**\n * The current text value of the input. Updated as the user types,\n * and formatted according to `formatOptions` on blur.\n */\n inputValue: string,\n /**\n * The currently parsed number value, or NaN if a valid number could not be parsed.\n * Updated based on the `inputValue` as the user types.\n */\n numberValue: number,\n /** The minimum value of the number field. */\n minValue?: number,\n /** The maximum value of the number field. */\n maxValue?: number,\n /** Whether the current value can be incremented according to the maximum value and step. */\n canIncrement: boolean,\n /** Whether the current value can be decremented according to the minimum value and step. */\n canDecrement: boolean,\n /**\n * Validates a user input string according to the current locale and format options.\n * Values can be partially entered, and may be valid even if they cannot currently be parsed to a number.\n * Can be used to implement validation as a user types.\n */\n validate(value: string): boolean,\n /** Sets the current text value of the input. */\n setInputValue(val: string): void,\n /** Sets the number value. */\n setNumberValue(val: number): void,\n /**\n * Commits the current input value. The value is parsed to a number, clamped according\n * to the minimum and maximum values of the field, and snapped to the nearest step value.\n * This will fire the `onChange` prop with the new value, and if uncontrolled, update the `numberValue`.\n * Typically this is called when the field is blurred.\n */\n commit(): void,\n /** Increments the current input value to the next step boundary, and fires `onChange`. */\n increment(): void,\n /** Decrements the current input value to the next step boundary, and fires `onChange`. */\n decrement(): void,\n /** Sets the current value to the `maxValue` if any, and fires `onChange`. */\n incrementToMax(): void,\n /** Sets the current value to the `minValue` if any, and fires `onChange`. */\n decrementToMin(): void\n}\n\nexport interface NumberFieldStateOptions extends NumberFieldProps {\n /**\n * The locale that should be used for parsing.\n * @default 'en-US'\n */\n locale: string\n}\n\n/**\n * Provides state management for a number field component. Number fields allow users to enter a number,\n * and increment or decrement the value using stepper buttons.\n */\nexport function useNumberFieldState(\n props: NumberFieldStateOptions\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue = NaN,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\n\n if (value === null) {\n value = NaN;\n }\n\n if (value !== undefined && !isNaN(value)) {\n if (step !== undefined && !isNaN(step)) {\n value = snapValueToStep(value, minValue, maxValue, step);\n } else {\n value = clamp(value, minValue, maxValue);\n }\n }\n\n if (!isNaN(defaultValue)) {\n if (step !== undefined && !isNaN(step)) {\n defaultValue = snapValueToStep(defaultValue, minValue, maxValue, step);\n } else {\n defaultValue = clamp(defaultValue, minValue, maxValue);\n }\n }\n\n let [numberValue, setNumberValue] = useControlledState<number>(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);\n let [inputValue, setInputValue] = useState(() => isNaN(numberValue) ? '' : new NumberFormatter(locale, formatOptions).format(numberValue));\n\n let numberParser = useMemo(() => new NumberParser(locale, formatOptions), [locale, formatOptions]);\n let numberingSystem = useMemo(() => numberParser.getNumberingSystem(inputValue), [numberParser, inputValue]);\n let formatter = useMemo(() => new NumberFormatter(locale, {...formatOptions, numberingSystem}), [locale, formatOptions, numberingSystem]);\n let intlOptions = useMemo(() => formatter.resolvedOptions(), [formatter]);\n let format = useCallback((value: number) => (isNaN(value) || value === null) ? '' : formatter.format(value), [formatter]);\n\n let validation = useFormValidationState({\n ...props,\n value: numberValue\n });\n\n let clampStep = (step !== undefined && !isNaN(step)) ? step : 1;\n if (intlOptions.style === 'percent' && (step === undefined || isNaN(step))) {\n clampStep = 0.01;\n }\n\n // Update the input value when the number value or format options change. This is done\n // in a useEffect so that the controlled behavior is correct and we only update the\n // textfield after prop changes.\n let [prevValue, setPrevValue] = useState(numberValue);\n let [prevLocale, setPrevLocale] = useState(locale);\n let [prevFormatOptions, setPrevFormatOptions] = useState(formatOptions);\n if (!Object.is(numberValue, prevValue) || locale !== prevLocale || formatOptions !== prevFormatOptions) {\n setInputValue(format(numberValue));\n setPrevValue(numberValue);\n setPrevLocale(locale);\n setPrevFormatOptions(formatOptions);\n }\n\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let commit = () => {\n // Set to empty state if input value is empty\n if (!inputValue.length) {\n setNumberValue(NaN);\n setInputValue(value === undefined ? '' : format(numberValue));\n return;\n }\n\n // if it failed to parse, then reset input to formatted version of current number\n if (isNaN(parsedValue)) {\n setInputValue(format(numberValue));\n return;\n }\n\n // Clamp to min and max, round to the nearest step, and round to specified number of digits\n let clampedValue: number;\n if (step === undefined || isNaN(step)) {\n clampedValue = clamp(parsedValue, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsedValue, minValue, maxValue, step);\n }\n\n clampedValue = numberParser.parse(format(clampedValue));\n setNumberValue(clampedValue);\n\n // in a controlled state, the numberValue won't change, so we won't go back to our old input without help\n setInputValue(format(value === undefined ? clampedValue : numberValue));\n };\n\n let safeNextStep = (operation: '+' | '-', minMax: number = 0) => {\n let prev = parsedValue;\n\n if (isNaN(prev)) {\n // if the input is empty, start from the min/max value when incrementing/decrementing,\n // or zero if there is no min/max value defined.\n let newValue = isNaN(minMax) ? 0 : minMax;\n return snapValueToStep(newValue, minValue, maxValue, clampStep);\n } else {\n // otherwise, first snap the current value to the nearest step. if it moves in the direction\n // we're going, use that value, otherwise add the step and snap that value.\n let newValue = snapValueToStep(prev, minValue, maxValue, clampStep);\n if ((operation === '+' && newValue > prev) || (operation === '-' && newValue < prev)) {\n return newValue;\n }\n\n return snapValueToStep(\n handleDecimalOperation(operation, prev, clampStep),\n minValue,\n maxValue,\n clampStep\n );\n }\n };\n\n let increment = () => {\n let newValue = safeNextStep('+', minValue);\n\n // if we've arrived at the same value that was previously in the state, the\n // input value should be updated to match\n // ex type 4, press increment, highlight the number in the input, type 4 again, press increment\n // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n validation.commitValidation();\n };\n\n let decrement = () => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n validation.commitValidation();\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n validation.commitValidation();\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n validation.commitValidation();\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n (maxValue === undefined || isNaN(maxValue)) ||\n snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue ||\n handleDecimalOperation('+', parsedValue, clampStep) <= maxValue\n )\n ), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);\n\n let canDecrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n (minValue === undefined || isNaN(minValue)) ||\n snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue ||\n handleDecimalOperation('-', parsedValue, clampStep) >= minValue\n )\n ), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);\n\n let validate = (value: string) => numberParser.isValidPartialNumber(value, minValue, maxValue);\n\n return {\n ...validation,\n validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\n setNumberValue,\n setInputValue,\n inputValue,\n commit\n };\n}\n\nfunction handleDecimalOperation(operator: '-' | '+', value1: number, value2: number): number {\n let result = operator === '+' ? value1 + value2 : value1 - value2;\n\n // Check if we have decimals\n if (value1 % 1 !== 0 || value2 % 1 !== 0) {\n const value1Decimal = value1.toString().split('.');\n const value2Decimal = value2.toString().split('.');\n const value1DecimalLength = (value1Decimal[1] && value1Decimal[1].length) || 0;\n const value2DecimalLength = (value2Decimal[1] && value2Decimal[1].length) || 0;\n const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));\n\n // Transform the decimals to integers based on the precision\n value1 = Math.round(value1 * multiplier);\n value2 = Math.round(value2 * multiplier);\n\n // Perform the operation on integers values to make sure we don't get a fancy decimal value\n result = operator === '+' ? value1 + value2 : value1 - value2;\n\n // Transform the integer result back to decimal\n result /= multiplier;\n }\n\n return result;\n}\n"],"names":[],"version":3,"file":"useNumberFieldState.main.js.map"}