@react-stately/numberfield 3.0.2 → 3.0.3

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/main.js CHANGED
@@ -1,202 +1,198 @@
1
- var {
2
- useCallback,
3
- useEffect,
4
- useMemo,
5
- useRef,
6
- useState
7
- } = require("react");
1
+ var $cG3HT$reactstatelyutils = require("@react-stately/utils");
2
+ var $cG3HT$internationalizednumber = require("@internationalized/number");
3
+ var $cG3HT$react = require("react");
8
4
 
9
- var {
10
- NumberFormatter,
11
- NumberParser
12
- } = require("@internationalized/number");
13
-
14
- var {
15
- clamp,
16
- snapValueToStep,
17
- useControlledState
18
- } = require("@react-stately/utils");
19
-
20
- var _babelRuntimeHelpersExtends = $parcel$interopDefault(require("@babel/runtime/helpers/extends"));
21
-
22
- function $parcel$interopDefault(a) {
23
- return a && a.__esModule ? a.default : a;
24
- }
25
-
26
- /**
27
- * Provides state management for a number field component. Number fields allow users to enter a number,
28
- * and increment or decrement the value using stepper buttons.
29
- */
30
- function useNumberFieldState(props) {
31
- let {
32
- minValue,
33
- maxValue,
34
- step,
35
- formatOptions,
36
- value,
37
- defaultValue,
38
- onChange,
39
- locale,
40
- isDisabled,
41
- isReadOnly
42
- } = props;
43
- let [numberValue, setNumberValue] = useControlledState(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);
44
- let [inputValue, setInputValue] = useState(() => isNaN(numberValue) ? '' : new NumberFormatter(locale, formatOptions).format(numberValue));
45
- let numberParser = useMemo(() => new NumberParser(locale, formatOptions), [locale, formatOptions]);
46
- let numberingSystem = useMemo(() => numberParser.getNumberingSystem(inputValue), [numberParser, inputValue]);
47
- let formatter = useMemo(() => new NumberFormatter(locale, _babelRuntimeHelpersExtends({}, formatOptions, {
48
- numberingSystem
49
- })), [locale, formatOptions, numberingSystem]);
50
- let intlOptions = useMemo(() => formatter.resolvedOptions(), [formatter]);
51
- let format = useCallback(value => isNaN(value) ? '' : formatter.format(value), [formatter]);
52
- let clampStep = !isNaN(step) ? step : 1;
53
-
54
- if (intlOptions.style === 'percent' && isNaN(step)) {
55
- clampStep = 0.01;
56
- } // Update the input value when the number value or format options change. This is done
57
- // in a useEffect so that the controlled behavior is correct and we only update the
58
- // textfield after prop changes.
59
-
60
-
61
- useEffect(() => {
62
- setInputValue(format(numberValue));
63
- }, [numberValue, locale, formatOptions]); // Store last parsed value in a ref so it can be used by increment/decrement below
64
-
65
- let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);
66
- let parsed = useRef(0);
67
- parsed.current = parsedValue;
68
-
69
- let commit = () => {
70
- // Set to empty state if input value is empty
71
- if (!inputValue.length) {
72
- setNumberValue(NaN);
73
- setInputValue(value === undefined ? '' : format(numberValue));
74
- return;
75
- } // if it failed to parse, then reset input to formatted version of current number
76
-
77
-
78
- if (isNaN(parsed.current)) {
79
- setInputValue(format(numberValue));
5
+ function $parcel$exportWildcard(dest, source) {
6
+ Object.keys(source).forEach(function(key) {
7
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
80
8
  return;
81
- } // Clamp to min and max, round to the nearest step, and round to specified number of digits
82
-
83
-
84
- let clampedValue;
85
-
86
- if (isNaN(step)) {
87
- clampedValue = clamp(parsed.current, minValue, maxValue);
88
- } else {
89
- clampedValue = snapValueToStep(parsed.current, minValue, maxValue, step);
90
9
  }
91
10
 
92
- clampedValue = numberParser.parse(format(clampedValue));
93
- setNumberValue(clampedValue); // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
94
-
95
- setInputValue(format(value === undefined ? clampedValue : numberValue));
96
- };
97
-
98
- let safeNextStep = (operation, minMax) => {
99
- let prev = parsed.current;
100
-
101
- if (isNaN(prev)) {
102
- // if the input is empty, start from the min/max value when incrementing/decrementing,
103
- // or zero if there is no min/max value defined.
104
- let newValue = isNaN(minMax) ? 0 : minMax;
105
- return snapValueToStep(newValue, minValue, maxValue, clampStep);
106
- } else {
107
- // otherwise, first snap the current value to the nearest step. if it moves in the direction
108
- // we're going, use that value, otherwise add the step and snap that value.
109
- let newValue = snapValueToStep(prev, minValue, maxValue, clampStep);
110
-
111
- if (operation === '+' && newValue > prev || operation === '-' && newValue < prev) {
112
- return newValue;
11
+ Object.defineProperty(dest, key, {
12
+ enumerable: true,
13
+ get: function get() {
14
+ return source[key];
113
15
  }
114
-
115
- return snapValueToStep($bb3f0c77930f1734ecd987a3e5d3aa28$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
116
- }
117
- };
118
-
119
- let increment = () => {
120
- setNumberValue(previousValue => {
121
- let newValue = safeNextStep('+', minValue); // 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
-
126
- if (newValue === previousValue) {
127
- setInputValue(format(newValue));
128
- }
129
-
130
- return newValue;
131
16
  });
132
- };
133
-
134
- let decrement = () => {
135
- setNumberValue(previousValue => {
136
- let newValue = safeNextStep('-', maxValue);
137
-
138
- if (newValue === previousValue) {
139
- setInputValue(format(newValue));
140
- }
141
-
142
- return newValue;
143
- });
144
- };
145
-
146
- let incrementToMax = () => {
147
- if (maxValue != null) {
148
- setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));
149
- }
150
- };
17
+ });
151
18
 
152
- let decrementToMin = () => {
153
- if (minValue != null) {
154
- setNumberValue(minValue);
19
+ return dest;
20
+ }
21
+ function $parcel$export(e, n, v, s) {
22
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
23
+ }
24
+ var $ef13160816a10239$exports = {};
25
+
26
+ $parcel$export($ef13160816a10239$exports, "useNumberFieldState", () => $ef13160816a10239$export$7f629e9dc1ecf37c);
27
+
28
+
29
+
30
+ function $ef13160816a10239$export$7f629e9dc1ecf37c(props) {
31
+ let { minValue: minValue , maxValue: maxValue , step: step , formatOptions: formatOptions , value: value1 , defaultValue: defaultValue , onChange: onChange , locale: locale , isDisabled: isDisabled , isReadOnly: isReadOnly } = props;
32
+ let [numberValue, setNumberValue] = $cG3HT$reactstatelyutils.useControlledState(value1, isNaN(defaultValue) ? NaN : defaultValue, onChange);
33
+ let [inputValue, setInputValue] = $cG3HT$react.useState(()=>isNaN(numberValue) ? '' : new $cG3HT$internationalizednumber.NumberFormatter(locale, formatOptions).format(numberValue)
34
+ );
35
+ let numberParser = $cG3HT$react.useMemo(()=>new $cG3HT$internationalizednumber.NumberParser(locale, formatOptions)
36
+ , [
37
+ locale,
38
+ formatOptions
39
+ ]);
40
+ let numberingSystem = $cG3HT$react.useMemo(()=>numberParser.getNumberingSystem(inputValue)
41
+ , [
42
+ numberParser,
43
+ inputValue
44
+ ]);
45
+ let formatter = $cG3HT$react.useMemo(()=>new $cG3HT$internationalizednumber.NumberFormatter(locale, {
46
+ ...formatOptions,
47
+ numberingSystem: numberingSystem
48
+ })
49
+ , [
50
+ locale,
51
+ formatOptions,
52
+ numberingSystem
53
+ ]);
54
+ let intlOptions = $cG3HT$react.useMemo(()=>formatter.resolvedOptions()
55
+ , [
56
+ formatter
57
+ ]);
58
+ let format = $cG3HT$react.useCallback((value)=>isNaN(value) ? '' : formatter.format(value)
59
+ , [
60
+ formatter
61
+ ]);
62
+ let clampStep = !isNaN(step) ? step : 1;
63
+ if (intlOptions.style === 'percent' && isNaN(step)) clampStep = 0.01;
64
+ // Update the input value when the number value or format options change. This is done
65
+ // in a useEffect so that the controlled behavior is correct and we only update the
66
+ // textfield after prop changes.
67
+ $cG3HT$react.useEffect(()=>{
68
+ setInputValue(format(numberValue));
69
+ }, [
70
+ numberValue,
71
+ locale,
72
+ formatOptions
73
+ ]);
74
+ // Store last parsed value in a ref so it can be used by increment/decrement below
75
+ let parsedValue = $cG3HT$react.useMemo(()=>numberParser.parse(inputValue)
76
+ , [
77
+ numberParser,
78
+ inputValue
79
+ ]);
80
+ let parsed = $cG3HT$react.useRef(0);
81
+ parsed.current = parsedValue;
82
+ let commit = ()=>{
83
+ // Set to empty state if input value is empty
84
+ if (!inputValue.length) {
85
+ setNumberValue(NaN);
86
+ setInputValue(value1 === undefined ? '' : format(numberValue));
87
+ return;
88
+ }
89
+ // if it failed to parse, then reset input to formatted version of current number
90
+ if (isNaN(parsed.current)) {
91
+ setInputValue(format(numberValue));
92
+ return;
93
+ }
94
+ // Clamp to min and max, round to the nearest step, and round to specified number of digits
95
+ let clampedValue;
96
+ if (isNaN(step)) clampedValue = $cG3HT$reactstatelyutils.clamp(parsed.current, minValue, maxValue);
97
+ else clampedValue = $cG3HT$reactstatelyutils.snapValueToStep(parsed.current, minValue, maxValue, step);
98
+ clampedValue = numberParser.parse(format(clampedValue));
99
+ setNumberValue(clampedValue);
100
+ // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
101
+ setInputValue(format(value1 === undefined ? clampedValue : numberValue));
102
+ };
103
+ let safeNextStep = (operation, minMax)=>{
104
+ let prev = parsed.current;
105
+ if (isNaN(prev)) {
106
+ // if the input is empty, start from the min/max value when incrementing/decrementing,
107
+ // or zero if there is no min/max value defined.
108
+ let newValue = isNaN(minMax) ? 0 : minMax;
109
+ return $cG3HT$reactstatelyutils.snapValueToStep(newValue, minValue, maxValue, clampStep);
110
+ } else {
111
+ // otherwise, first snap the current value to the nearest step. if it moves in the direction
112
+ // we're going, use that value, otherwise add the step and snap that value.
113
+ let newValue = $cG3HT$reactstatelyutils.snapValueToStep(prev, minValue, maxValue, clampStep);
114
+ if (operation === '+' && newValue > prev || operation === '-' && newValue < prev) return newValue;
115
+ return $cG3HT$reactstatelyutils.snapValueToStep($ef13160816a10239$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
116
+ }
117
+ };
118
+ let increment = ()=>{
119
+ let newValue = safeNextStep('+', minValue);
120
+ // if we've arrived at the same value that was previously in the state, the
121
+ // input value should be updated to match
122
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
123
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
124
+ if (newValue === numberValue) setInputValue(format(newValue));
125
+ setNumberValue(newValue);
126
+ };
127
+ let decrement = ()=>{
128
+ let newValue = safeNextStep('-', maxValue);
129
+ if (newValue === numberValue) setInputValue(format(newValue));
130
+ setNumberValue(newValue);
131
+ };
132
+ let incrementToMax = ()=>{
133
+ if (maxValue != null) setNumberValue($cG3HT$reactstatelyutils.snapValueToStep(maxValue, minValue, maxValue, clampStep));
134
+ };
135
+ let decrementToMin = ()=>{
136
+ if (minValue != null) setNumberValue(minValue);
137
+ };
138
+ let canIncrement = $cG3HT$react.useMemo(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(maxValue) || $cG3HT$reactstatelyutils.snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue || $ef13160816a10239$var$handleDecimalOperation('+', parsedValue, clampStep) <= maxValue)
139
+ , [
140
+ isDisabled,
141
+ isReadOnly,
142
+ minValue,
143
+ maxValue,
144
+ clampStep,
145
+ parsedValue
146
+ ]);
147
+ let canDecrement = $cG3HT$react.useMemo(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(minValue) || $cG3HT$reactstatelyutils.snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue || $ef13160816a10239$var$handleDecimalOperation('-', parsedValue, clampStep) >= minValue)
148
+ , [
149
+ isDisabled,
150
+ isReadOnly,
151
+ minValue,
152
+ maxValue,
153
+ clampStep,
154
+ parsedValue
155
+ ]);
156
+ let validate = (value)=>numberParser.isValidPartialNumber(value, minValue, maxValue)
157
+ ;
158
+ return {
159
+ validate: validate,
160
+ increment: increment,
161
+ incrementToMax: incrementToMax,
162
+ decrement: decrement,
163
+ decrementToMin: decrementToMin,
164
+ canIncrement: canIncrement,
165
+ canDecrement: canDecrement,
166
+ minValue: minValue,
167
+ maxValue: maxValue,
168
+ numberValue: parsedValue,
169
+ setInputValue: setInputValue,
170
+ inputValue: inputValue,
171
+ commit: commit
172
+ };
173
+ }
174
+ function $ef13160816a10239$var$handleDecimalOperation(operator, value1, value2) {
175
+ let result = operator === '+' ? value1 + value2 : value1 - value2;
176
+ // Check if we have decimals
177
+ if (value1 % 1 !== 0 || value2 % 1 !== 0) {
178
+ const value1Decimal = value1.toString().split('.');
179
+ const value2Decimal = value2.toString().split('.');
180
+ const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
181
+ const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
182
+ const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));
183
+ // Transform the decimals to integers based on the precision
184
+ value1 = Math.round(value1 * multiplier);
185
+ value2 = Math.round(value2 * multiplier);
186
+ // Perform the operation on integers values to make sure we don't get a fancy decimal value
187
+ result = operator === '+' ? value1 + value2 : value1 - value2;
188
+ // Transform the integer result back to decimal
189
+ result /= multiplier;
155
190
  }
156
- };
157
-
158
- let canIncrement = useMemo(() => !isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(maxValue) || snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue || $bb3f0c77930f1734ecd987a3e5d3aa28$var$handleDecimalOperation('+', parsedValue, clampStep) <= maxValue), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);
159
- let canDecrement = useMemo(() => !isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(minValue) || snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue || $bb3f0c77930f1734ecd987a3e5d3aa28$var$handleDecimalOperation('-', parsedValue, clampStep) >= minValue), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);
160
-
161
- let validate = value => numberParser.isValidPartialNumber(value, minValue, maxValue);
162
-
163
- return {
164
- validate,
165
- increment,
166
- incrementToMax,
167
- decrement,
168
- decrementToMin,
169
- canIncrement,
170
- canDecrement,
171
- minValue,
172
- maxValue,
173
- numberValue: parsedValue,
174
- setInputValue,
175
- inputValue,
176
- commit
177
- };
191
+ return result;
178
192
  }
179
193
 
180
- exports.useNumberFieldState = useNumberFieldState;
181
-
182
- function $bb3f0c77930f1734ecd987a3e5d3aa28$var$handleDecimalOperation(operator, value1, value2) {
183
- let result = operator === '+' ? value1 + value2 : value1 - value2; // Check if we have decimals
184
-
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)); // Transform the decimals to integers based on the precision
191
194
 
192
- value1 = Math.round(value1 * multiplier);
193
- value2 = Math.round(value2 * multiplier); // Perform the operation on integers values to make sure we don't get a fancy decimal value
195
+ $parcel$exportWildcard(module.exports, $ef13160816a10239$exports);
194
196
 
195
- result = operator === '+' ? value1 + value2 : value1 - value2; // Transform the integer result back to decimal
196
197
 
197
- result /= multiplier;
198
- }
199
-
200
- return result;
201
- }
202
198
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,sBAAT,CAAgCC,CAAhC,EAAmC;AACjC,SAAOA,CAAC,IAAIA,CAAC,CAACC,UAAP,GAAoBD,CAAC,CAACE,OAAtB,GAAgCF,CAAvC;AACD;;ACmED;;;;AAIO,SAASG,mBAAT,CACLC,KADK,EAEa;AAClB,MAAI;AACFC,IAAAA,QADE;AAEFC,IAAAA,QAFE;AAGFC,IAAAA,IAHE;AAIFC,IAAAA,aAJE;AAKFC,IAAAA,KALE;AAMFC,IAAAA,YANE;AAOFC,IAAAA,QAPE;AAQFC,IAAAA,MARE;AASFC,IAAAA,UATE;AAUFC,IAAAA;AAVE,MAWAV,KAXJ;AAaA,MAAI,CAACW,WAAD,EAAcC,cAAd,IAAgCC,kBAAkB,CAASR,KAAT,EAAgBS,KAAK,CAACR,YAAD,CAAL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AA7E;AACA,UAAM6B,mBAAmB,GAAIF,aAAa,CAAC,CAAD,CAAb,IAAoBA,aAAa,CAAC,CAAD,CAAb,CAAiB3B,MAAtC,IAAiD,CAA7E;AACA,UAAM8B,UAAU,GAAGC,IAAI,CAACC,GAAL,CAAS,EAAT,EAAaD,IAAI,CAACE,GAAL,CAASL,mBAAT,EAA8BC,mBAA9B,CAAb,CAAnB,CALwC,CAOxC;;AACAR,IAAAA,MAAM,GAAGU,IAAI,CAACG,KAAL,CAAWb,MAAM,GAAGS,UAApB,CAAT;AACAR,IAAAA,MAAM,GAAGS,IAAI,CAACG,KAAL,CAAWZ,MAAM,GAAGQ,UAApB,CAAT,CATwC,CAWxC;;AACAP,IAAAA,MAAM,GAAGH,QAAQ,KAAK,GAAb,GAAmBC,MAAM,GAAGC,MAA5B,GAAqCD,MAAM,GAAGC,MAAvD,CAZwC,CAcxC;;AACAC,IAAAA,MAAM,IAAIO,UAAV;AACD;;AAED,SAAOP,MAAP;AACD","sources":["./node_modules/@parcel/scope-hoisting/lib/helpers.js","./packages/@react-stately/numberfield/src/useNumberFieldState.ts"],"sourcesContent":["function $parcel$interopDefault(a) {\n return a && a.__esModule ? a.default : a;\n}\n\nfunction $parcel$defineInteropFlag(a) {\n Object.defineProperty(a, '__esModule', {value: true});\n}\n\nfunction $parcel$exportWildcard(dest, source) {\n Object.keys(source).forEach(function(key) {\n if (key === 'default' || key === '__esModule') {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function get() {\n return source[key];\n },\n });\n });\n\n return dest;\n}\n\nfunction $parcel$missingModule(name) {\n var err = new Error(\"Cannot find module '\" + name + \"'\");\n err.code = 'MODULE_NOT_FOUND';\n throw err;\n}\n\nvar $parcel$global =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\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 {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react';\n\nexport interface NumberFieldState {\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 /**\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\ninterface NumberFieldStateProps 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: NumberFieldStateProps\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\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) ? '' : formatter.format(value), [formatter]);\n\n let clampStep = !isNaN(step) ? step : 1;\n if (intlOptions.style === 'percent' && 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 useEffect(() => {\n setInputValue(format(numberValue));\n }, [numberValue, locale, formatOptions]);\n\n // Store last parsed value in a ref so it can be used by increment/decrement below\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let parsed = useRef(0);\n parsed.current = parsedValue;\n\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(parsed.current)) {\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 (isNaN(step)) {\n clampedValue = clamp(parsed.current, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsed.current, 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) => {\n let prev = parsed.current;\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 setNumberValue((previousValue) => {\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 === previousValue) {\n setInputValue(format(newValue));\n }\n\n return newValue;\n });\n };\n\n let decrement = () => {\n setNumberValue((previousValue) => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === previousValue) {\n setInputValue(format(newValue));\n }\n\n return newValue;\n });\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n 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 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 validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\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":["$parcel$interopDefault","a","__esModule","default","useNumberFieldState","props","minValue","maxValue","step","formatOptions","value","defaultValue","onChange","locale","isDisabled","isReadOnly","numberValue","setNumberValue","useControlledState","isNaN","NaN","inputValue","setInputValue","useState","NumberFormatter","format","numberParser","useMemo","NumberParser","numberingSystem","getNumberingSystem","formatter","intlOptions","resolvedOptions","useCallback","clampStep","style","useEffect","parsedValue","parse","parsed","useRef","current","commit","length","undefined","clampedValue","clamp","snapValueToStep","safeNextStep","operation","minMax","prev","newValue","handleDecimalOperation","increment","previousValue","decrement","incrementToMax","decrementToMin","canIncrement","canDecrement","validate","isValidPartialNumber","operator","value1","value2","result","value1Decimal","toString","split","value2Decimal","value1DecimalLength","value2DecimalLength","multiplier","Math","pow","max","round"],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;SCyEgB,yCAAmB,CACjC,KAA4B,EACV,CAAC;IACnB,GAAG,CAAC,CAAC,WACH,QAAQ,aACR,QAAQ,SACR,IAAI,kBACJ,aAAa,UACb,MAAK,iBACL,YAAY,aACZ,QAAQ,WACR,MAAM,eACN,UAAU,eACV,UAAU,EACZ,CAAC,GAAG,KAAK;IAET,GAAG,EAAE,WAAW,EAAE,cAAc,IAAI,2CAAkB,CAAS,MAAK,EAAE,KAAK,CAAC,YAAY,IAAI,GAAG,GAAG,YAAY,EAAE,QAAQ;IACxH,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,qBAAQ,KAAO,KAAK,CAAC,WAAW,IAAI,CAAE,IAAG,GAAG,CAAC,8CAAe,CAAC,MAAM,EAAE,aAAa,EAAE,MAAM,CAAC,WAAW;;IAExI,GAAG,CAAC,YAAY,GAAG,oBAAO,KAAO,GAAG,CAAC,2CAAY,CAAC,MAAM,EAAE,aAAa;MAAG,CAAC;QAAA,MAAM;QAAE,aAAa;IAAA,CAAC;IACjG,GAAG,CAAC,eAAe,GAAG,oBAAO,KAAO,YAAY,CAAC,kBAAkB,CAAC,UAAU;MAAG,CAAC;QAAA,YAAY;QAAE,UAAU;IAAA,CAAC;IAC3G,GAAG,CAAC,SAAS,GAAG,oBAAO,KAAO,GAAG,CAAC,8CAAe,CAAC,MAAM,EAAE,CAAC;eAAG,aAAa;6BAAE,eAAe;QAAA,CAAC;MAAG,CAAC;QAAA,MAAM;QAA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,IAAK,CAAC;QAC9E,KAAK,CAAC,mBAAmB,GAAI,aAAa,CAAC,CAAC,KAAK,aAAa,CAAC,CAAC,EAAE,MAAM,IAAK,CAAC;QAC9E,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,mBAAmB,EAAE,mBAAmB;QAEjF,EAA4D,AAA5D,0DAA4D;QAC5D,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,UAAU;QACvC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,UAAU;QAEvC,EAA2F,AAA3F,yFAA2F;QAC3F,MAAM,GAAG,QAAQ,KAAK,CAAG,KAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM;QAE7D,EAA+C,AAA/C,6CAA+C;QAC/C,MAAM,IAAI,UAAU;IACtB,CAAC;IAED,MAAM,CAAC,MAAM;AACf,CAAC","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\n/// <reference types=\"intl-types-extension\" />\n\nexport * 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 {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react';\n\nexport interface NumberFieldState {\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 /**\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\ninterface NumberFieldStateProps 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: NumberFieldStateProps\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\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) ? '' : formatter.format(value), [formatter]);\n\n let clampStep = !isNaN(step) ? step : 1;\n if (intlOptions.style === 'percent' && 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 useEffect(() => {\n setInputValue(format(numberValue));\n }, [numberValue, locale, formatOptions]);\n\n // Store last parsed value in a ref so it can be used by increment/decrement below\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let parsed = useRef(0);\n parsed.current = parsedValue;\n\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(parsed.current)) {\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 (isNaN(step)) {\n clampedValue = clamp(parsed.current, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsed.current, 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) => {\n let prev = parsed.current;\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 };\n\n let decrement = () => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n 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 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 validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\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":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,180 +1,182 @@
1
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
- import { NumberFormatter, NumberParser } from "@internationalized/number";
3
- import { clamp, snapValueToStep, useControlledState } from "@react-stately/utils";
4
- import _babelRuntimeHelpersEsmExtends from "@babel/runtime/helpers/esm/extends";
1
+ import {useControlledState as $aCqtE$useControlledState, clamp as $aCqtE$clamp, snapValueToStep as $aCqtE$snapValueToStep} from "@react-stately/utils";
2
+ import {NumberFormatter as $aCqtE$NumberFormatter, NumberParser as $aCqtE$NumberParser} from "@internationalized/number";
3
+ import {useState as $aCqtE$useState, useMemo as $aCqtE$useMemo, useCallback as $aCqtE$useCallback, useEffect as $aCqtE$useEffect, useRef as $aCqtE$useRef} from "react";
5
4
 
6
- /**
7
- * Provides state management for a number field component. Number fields allow users to enter a number,
8
- * and increment or decrement the value using stepper buttons.
9
- */
10
- export function useNumberFieldState(props) {
11
- let {
12
- minValue,
13
- maxValue,
14
- step,
15
- formatOptions,
16
- value,
17
- defaultValue,
18
- onChange,
19
- locale,
20
- isDisabled,
21
- isReadOnly
22
- } = props;
23
- let [numberValue, setNumberValue] = useControlledState(value, isNaN(defaultValue) ? NaN : defaultValue, onChange);
24
- let [inputValue, setInputValue] = useState(() => isNaN(numberValue) ? '' : new NumberFormatter(locale, formatOptions).format(numberValue));
25
- let numberParser = useMemo(() => new NumberParser(locale, formatOptions), [locale, formatOptions]);
26
- let numberingSystem = useMemo(() => numberParser.getNumberingSystem(inputValue), [numberParser, inputValue]);
27
- let formatter = useMemo(() => new NumberFormatter(locale, _babelRuntimeHelpersEsmExtends({}, formatOptions, {
28
- numberingSystem
29
- })), [locale, formatOptions, numberingSystem]);
30
- let intlOptions = useMemo(() => formatter.resolvedOptions(), [formatter]);
31
- let format = useCallback(value => isNaN(value) ? '' : formatter.format(value), [formatter]);
32
- let clampStep = !isNaN(step) ? step : 1;
33
-
34
- if (intlOptions.style === 'percent' && isNaN(step)) {
35
- clampStep = 0.01;
36
- } // Update the input value when the number value or format options change. This is done
37
- // in a useEffect so that the controlled behavior is correct and we only update the
38
- // textfield after prop changes.
39
-
40
-
41
- useEffect(() => {
42
- setInputValue(format(numberValue));
43
- }, [numberValue, locale, formatOptions]); // Store last parsed value in a ref so it can be used by increment/decrement below
44
-
45
- let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);
46
- let parsed = useRef(0);
47
- parsed.current = parsedValue;
48
-
49
- let commit = () => {
50
- // Set to empty state if input value is empty
51
- if (!inputValue.length) {
52
- setNumberValue(NaN);
53
- setInputValue(value === undefined ? '' : format(numberValue));
54
- return;
55
- } // if it failed to parse, then reset input to formatted version of current number
56
-
57
-
58
- if (isNaN(parsed.current)) {
59
- setInputValue(format(numberValue));
60
- return;
61
- } // Clamp to min and max, round to the nearest step, and round to specified number of digits
62
-
63
-
64
- let clampedValue;
65
-
66
- if (isNaN(step)) {
67
- clampedValue = clamp(parsed.current, minValue, maxValue);
68
- } else {
69
- clampedValue = snapValueToStep(parsed.current, minValue, maxValue, step);
70
- }
71
-
72
- clampedValue = numberParser.parse(format(clampedValue));
73
- setNumberValue(clampedValue); // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
74
-
75
- setInputValue(format(value === undefined ? clampedValue : numberValue));
76
- };
77
-
78
- let safeNextStep = (operation, minMax) => {
79
- let prev = parsed.current;
80
-
81
- if (isNaN(prev)) {
82
- // if the input is empty, start from the min/max value when incrementing/decrementing,
83
- // or zero if there is no min/max value defined.
84
- let newValue = isNaN(minMax) ? 0 : minMax;
85
- return snapValueToStep(newValue, minValue, maxValue, clampStep);
86
- } else {
87
- // otherwise, first snap the current value to the nearest step. if it moves in the direction
88
- // we're going, use that value, otherwise add the step and snap that value.
89
- let newValue = snapValueToStep(prev, minValue, maxValue, clampStep);
90
-
91
- if (operation === '+' && newValue > prev || operation === '-' && newValue < prev) {
92
- return newValue;
93
- }
94
-
95
- return snapValueToStep($b6f3c0268c2239b947a18381443e4550$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
96
- }
97
- };
98
-
99
- let increment = () => {
100
- setNumberValue(previousValue => {
101
- let newValue = safeNextStep('+', minValue); // if we've arrived at the same value that was previously in the state, the
102
- // input value should be updated to match
103
- // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
104
- // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
105
-
106
- if (newValue === previousValue) {
107
- setInputValue(format(newValue));
108
- }
109
-
110
- return newValue;
111
- });
112
- };
113
-
114
- let decrement = () => {
115
- setNumberValue(previousValue => {
116
- let newValue = safeNextStep('-', maxValue);
117
-
118
- if (newValue === previousValue) {
119
- setInputValue(format(newValue));
120
- }
121
-
122
- return newValue;
123
- });
124
- };
125
-
126
- let incrementToMax = () => {
127
- if (maxValue != null) {
128
- setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));
129
- }
130
- };
131
-
132
- let decrementToMin = () => {
133
- if (minValue != null) {
134
- setNumberValue(minValue);
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+ var $87aa52afafab9e9f$exports = {};
9
+
10
+ $parcel$export($87aa52afafab9e9f$exports, "useNumberFieldState", () => $87aa52afafab9e9f$export$7f629e9dc1ecf37c);
11
+
12
+
13
+
14
+ function $87aa52afafab9e9f$export$7f629e9dc1ecf37c(props) {
15
+ let { minValue: minValue , maxValue: maxValue , step: step , formatOptions: formatOptions , value: value1 , defaultValue: defaultValue , onChange: onChange , locale: locale , isDisabled: isDisabled , isReadOnly: isReadOnly } = props;
16
+ let [numberValue, setNumberValue] = $aCqtE$useControlledState(value1, isNaN(defaultValue) ? NaN : defaultValue, onChange);
17
+ let [inputValue, setInputValue] = $aCqtE$useState(()=>isNaN(numberValue) ? '' : new $aCqtE$NumberFormatter(locale, formatOptions).format(numberValue)
18
+ );
19
+ let numberParser = $aCqtE$useMemo(()=>new $aCqtE$NumberParser(locale, formatOptions)
20
+ , [
21
+ locale,
22
+ formatOptions
23
+ ]);
24
+ let numberingSystem = $aCqtE$useMemo(()=>numberParser.getNumberingSystem(inputValue)
25
+ , [
26
+ numberParser,
27
+ inputValue
28
+ ]);
29
+ let formatter = $aCqtE$useMemo(()=>new $aCqtE$NumberFormatter(locale, {
30
+ ...formatOptions,
31
+ numberingSystem: numberingSystem
32
+ })
33
+ , [
34
+ locale,
35
+ formatOptions,
36
+ numberingSystem
37
+ ]);
38
+ let intlOptions = $aCqtE$useMemo(()=>formatter.resolvedOptions()
39
+ , [
40
+ formatter
41
+ ]);
42
+ let format = $aCqtE$useCallback((value)=>isNaN(value) ? '' : formatter.format(value)
43
+ , [
44
+ formatter
45
+ ]);
46
+ let clampStep = !isNaN(step) ? step : 1;
47
+ if (intlOptions.style === 'percent' && isNaN(step)) clampStep = 0.01;
48
+ // Update the input value when the number value or format options change. This is done
49
+ // in a useEffect so that the controlled behavior is correct and we only update the
50
+ // textfield after prop changes.
51
+ $aCqtE$useEffect(()=>{
52
+ setInputValue(format(numberValue));
53
+ }, [
54
+ numberValue,
55
+ locale,
56
+ formatOptions
57
+ ]);
58
+ // Store last parsed value in a ref so it can be used by increment/decrement below
59
+ let parsedValue = $aCqtE$useMemo(()=>numberParser.parse(inputValue)
60
+ , [
61
+ numberParser,
62
+ inputValue
63
+ ]);
64
+ let parsed = $aCqtE$useRef(0);
65
+ parsed.current = parsedValue;
66
+ let commit = ()=>{
67
+ // Set to empty state if input value is empty
68
+ if (!inputValue.length) {
69
+ setNumberValue(NaN);
70
+ setInputValue(value1 === undefined ? '' : format(numberValue));
71
+ return;
72
+ }
73
+ // if it failed to parse, then reset input to formatted version of current number
74
+ if (isNaN(parsed.current)) {
75
+ setInputValue(format(numberValue));
76
+ return;
77
+ }
78
+ // Clamp to min and max, round to the nearest step, and round to specified number of digits
79
+ let clampedValue;
80
+ if (isNaN(step)) clampedValue = $aCqtE$clamp(parsed.current, minValue, maxValue);
81
+ else clampedValue = $aCqtE$snapValueToStep(parsed.current, minValue, maxValue, step);
82
+ clampedValue = numberParser.parse(format(clampedValue));
83
+ setNumberValue(clampedValue);
84
+ // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
85
+ setInputValue(format(value1 === undefined ? clampedValue : numberValue));
86
+ };
87
+ let safeNextStep = (operation, minMax)=>{
88
+ let prev = parsed.current;
89
+ if (isNaN(prev)) {
90
+ // if the input is empty, start from the min/max value when incrementing/decrementing,
91
+ // or zero if there is no min/max value defined.
92
+ let newValue = isNaN(minMax) ? 0 : minMax;
93
+ return $aCqtE$snapValueToStep(newValue, minValue, maxValue, clampStep);
94
+ } else {
95
+ // otherwise, first snap the current value to the nearest step. if it moves in the direction
96
+ // we're going, use that value, otherwise add the step and snap that value.
97
+ let newValue = $aCqtE$snapValueToStep(prev, minValue, maxValue, clampStep);
98
+ if (operation === '+' && newValue > prev || operation === '-' && newValue < prev) return newValue;
99
+ return $aCqtE$snapValueToStep($87aa52afafab9e9f$var$handleDecimalOperation(operation, prev, clampStep), minValue, maxValue, clampStep);
100
+ }
101
+ };
102
+ let increment = ()=>{
103
+ let newValue = safeNextStep('+', minValue);
104
+ // if we've arrived at the same value that was previously in the state, the
105
+ // input value should be updated to match
106
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
107
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
108
+ if (newValue === numberValue) setInputValue(format(newValue));
109
+ setNumberValue(newValue);
110
+ };
111
+ let decrement = ()=>{
112
+ let newValue = safeNextStep('-', maxValue);
113
+ if (newValue === numberValue) setInputValue(format(newValue));
114
+ setNumberValue(newValue);
115
+ };
116
+ let incrementToMax = ()=>{
117
+ if (maxValue != null) setNumberValue($aCqtE$snapValueToStep(maxValue, minValue, maxValue, clampStep));
118
+ };
119
+ let decrementToMin = ()=>{
120
+ if (minValue != null) setNumberValue(minValue);
121
+ };
122
+ let canIncrement = $aCqtE$useMemo(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(maxValue) || $aCqtE$snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue || $87aa52afafab9e9f$var$handleDecimalOperation('+', parsedValue, clampStep) <= maxValue)
123
+ , [
124
+ isDisabled,
125
+ isReadOnly,
126
+ minValue,
127
+ maxValue,
128
+ clampStep,
129
+ parsedValue
130
+ ]);
131
+ let canDecrement = $aCqtE$useMemo(()=>!isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(minValue) || $aCqtE$snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue || $87aa52afafab9e9f$var$handleDecimalOperation('-', parsedValue, clampStep) >= minValue)
132
+ , [
133
+ isDisabled,
134
+ isReadOnly,
135
+ minValue,
136
+ maxValue,
137
+ clampStep,
138
+ parsedValue
139
+ ]);
140
+ let validate = (value)=>numberParser.isValidPartialNumber(value, minValue, maxValue)
141
+ ;
142
+ return {
143
+ validate: validate,
144
+ increment: increment,
145
+ incrementToMax: incrementToMax,
146
+ decrement: decrement,
147
+ decrementToMin: decrementToMin,
148
+ canIncrement: canIncrement,
149
+ canDecrement: canDecrement,
150
+ minValue: minValue,
151
+ maxValue: maxValue,
152
+ numberValue: parsedValue,
153
+ setInputValue: setInputValue,
154
+ inputValue: inputValue,
155
+ commit: commit
156
+ };
157
+ }
158
+ function $87aa52afafab9e9f$var$handleDecimalOperation(operator, value1, value2) {
159
+ let result = operator === '+' ? value1 + value2 : value1 - value2;
160
+ // Check if we have decimals
161
+ if (value1 % 1 !== 0 || value2 % 1 !== 0) {
162
+ const value1Decimal = value1.toString().split('.');
163
+ const value2Decimal = value2.toString().split('.');
164
+ const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
165
+ const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
166
+ const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));
167
+ // Transform the decimals to integers based on the precision
168
+ value1 = Math.round(value1 * multiplier);
169
+ value2 = Math.round(value2 * multiplier);
170
+ // Perform the operation on integers values to make sure we don't get a fancy decimal value
171
+ result = operator === '+' ? value1 + value2 : value1 - value2;
172
+ // Transform the integer result back to decimal
173
+ result /= multiplier;
135
174
  }
136
- };
137
-
138
- let canIncrement = useMemo(() => !isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(maxValue) || snapValueToStep(parsedValue, minValue, maxValue, clampStep) > parsedValue || $b6f3c0268c2239b947a18381443e4550$var$handleDecimalOperation('+', parsedValue, clampStep) <= maxValue), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);
139
- let canDecrement = useMemo(() => !isDisabled && !isReadOnly && (isNaN(parsedValue) || isNaN(minValue) || snapValueToStep(parsedValue, minValue, maxValue, clampStep) < parsedValue || $b6f3c0268c2239b947a18381443e4550$var$handleDecimalOperation('-', parsedValue, clampStep) >= minValue), [isDisabled, isReadOnly, minValue, maxValue, clampStep, parsedValue]);
140
-
141
- let validate = value => numberParser.isValidPartialNumber(value, minValue, maxValue);
142
-
143
- return {
144
- validate,
145
- increment,
146
- incrementToMax,
147
- decrement,
148
- decrementToMin,
149
- canIncrement,
150
- canDecrement,
151
- minValue,
152
- maxValue,
153
- numberValue: parsedValue,
154
- setInputValue,
155
- inputValue,
156
- commit
157
- };
175
+ return result;
158
176
  }
159
177
 
160
- function $b6f3c0268c2239b947a18381443e4550$var$handleDecimalOperation(operator, value1, value2) {
161
- let result = operator === '+' ? value1 + value2 : value1 - value2; // Check if we have decimals
162
-
163
- if (value1 % 1 !== 0 || value2 % 1 !== 0) {
164
- const value1Decimal = value1.toString().split('.');
165
- const value2Decimal = value2.toString().split('.');
166
- const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
167
- const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
168
- const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength)); // Transform the decimals to integers based on the precision
169
178
 
170
- value1 = Math.round(value1 * multiplier);
171
- value2 = Math.round(value2 * multiplier); // Perform the operation on integers values to make sure we don't get a fancy decimal value
172
179
 
173
- result = operator === '+' ? value1 + value2 : value1 - value2; // Transform the integer result back to decimal
174
180
 
175
- result /= multiplier;
176
- }
177
-
178
- return result;
179
- }
181
+ export {$87aa52afafab9e9f$export$7f629e9dc1ecf37c as useNumberFieldState};
180
182
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;AAqEA;;;;OAIO,SAASA,mBAAT,CACLC,KADK,EAEa;AAClB,MAAI;AACFC,IAAAA,QADE;AAEFC,IAAAA,QAFE;AAGFC,IAAAA,IAHE;AAIFC,IAAAA,aAJE;AAKFC,IAAAA,KALE;AAMF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AxB,CAAtB;AACA,UAAMC,aAAa,GAAGL,MAAM,CAACG,QAAP,GAAkBC,KAAlB,CAAwB,GAAxB,CAAtB;AACA,UAAME,mBAAmB,GAAIJ,aAAa,CAAC,CAAD,CAAb,IAAoBA,aAAa,CAAC,CAAD,CAAb,CAAiBxB,MAAtC,IAAiD,CAA7E;AACA,UAAM6B,mBAAmB,GAAIF,aAAa,CAAC,CAAD,CAAb,IAAoBA,aAAa,CAAC,CAAD,CAAb,CAAiB3B,MAAtC,IAAiD,CAA7E;AACA,UAAM8B,UAAU,GAAGC,IAAI,CAACC,GAAL,CAAS,EAAT,EAAaD,IAAI,CAACE,GAAL,CAASL,mBAAT,EAA8BC,mBAA9B,CAAb,CAAnB,CALwC,CAOxC;;AACAR,IAAAA,MAAM,GAAGU,IAAI,CAACG,KAAL,CAAWb,MAAM,GAAGS,UAApB,CAAT;AACAR,IAAAA,MAAM,GAAGS,IAAI,CAACG,KAAL,CAAWZ,MAAM,GAAGQ,UAApB,CAAT,CATwC,CAWxC;;AACAP,IAAAA,MAAM,GAAGH,QAAQ,KAAK,GAAb,GAAmBC,MAAM,GAAGC,MAA5B,GAAqCD,MAAM,GAAGC,MAAvD,CAZwC,CAcxC;;AACAC,IAAAA,MAAM,IAAIO,UAAV;AACD;;AAED,SAAOP,MAAP;AACD","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 {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react';\n\nexport interface NumberFieldState {\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 /**\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\ninterface NumberFieldStateProps 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: NumberFieldStateProps\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\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) ? '' : formatter.format(value), [formatter]);\n\n let clampStep = !isNaN(step) ? step : 1;\n if (intlOptions.style === 'percent' && 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 useEffect(() => {\n setInputValue(format(numberValue));\n }, [numberValue, locale, formatOptions]);\n\n // Store last parsed value in a ref so it can be used by increment/decrement below\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let parsed = useRef(0);\n parsed.current = parsedValue;\n\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(parsed.current)) {\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 (isNaN(step)) {\n clampedValue = clamp(parsed.current, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsed.current, 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) => {\n let prev = parsed.current;\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 setNumberValue((previousValue) => {\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 === previousValue) {\n setInputValue(format(newValue));\n }\n\n return newValue;\n });\n };\n\n let decrement = () => {\n setNumberValue((previousValue) => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === previousValue) {\n setInputValue(format(newValue));\n }\n\n return newValue;\n });\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n 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 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 validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\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":["useNumberFieldState","props","minValue","maxValue","step","formatOptions","value","defaultValue","onChange","locale","isDisabled","isReadOnly","numberValue","setNumberValue","useControlledState","isNaN","NaN","inputValue","setInputValue","useState","NumberFormatter","format","numberParser","useMemo","NumberParser","numberingSystem","getNumberingSystem","formatter","intlOptions","resolvedOptions","useCallback","clampStep","style","useEffect","parsedValue","parse","parsed","useRef","current","commit","length","undefined","clampedValue","clamp","snapValueToStep","safeNextStep","operation","minMax","prev","newValue","handleDecimalOperation","increment","previousValue","decrement","incrementToMax","decrementToMin","canIncrement","canDecrement","validate","isValidPartialNumber","operator","value1","value2","result","value1Decimal","toString","split","value2Decimal","value1DecimalLength","value2DecimalLength","multiplier","Math","pow","max","round"],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;SCyEgB,yCAAmB,CACjC,KAA4B,EACV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U;QAEvC,EAA2F,AAA3F,yFAA2F;QAC3F,MAAM,GAAG,QAAQ,KAAK,CAAG,KAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM;QAE7D,EAA+C,AAA/C,6CAA+C;QAC/C,MAAM,IAAI,UAAU;IACtB,CAAC;IAED,MAAM,CAAC,MAAM;AACf,CAAC","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\n/// <reference types=\"intl-types-extension\" />\n\nexport * 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 {NumberFieldProps} from '@react-types/numberfield';\nimport {NumberFormatter, NumberParser} from '@internationalized/number';\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react';\n\nexport interface NumberFieldState {\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 /**\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\ninterface NumberFieldStateProps 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: NumberFieldStateProps\n): NumberFieldState {\n let {\n minValue,\n maxValue,\n step,\n formatOptions,\n value,\n defaultValue,\n onChange,\n locale,\n isDisabled,\n isReadOnly\n } = props;\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) ? '' : formatter.format(value), [formatter]);\n\n let clampStep = !isNaN(step) ? step : 1;\n if (intlOptions.style === 'percent' && 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 useEffect(() => {\n setInputValue(format(numberValue));\n }, [numberValue, locale, formatOptions]);\n\n // Store last parsed value in a ref so it can be used by increment/decrement below\n let parsedValue = useMemo(() => numberParser.parse(inputValue), [numberParser, inputValue]);\n let parsed = useRef(0);\n parsed.current = parsedValue;\n\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(parsed.current)) {\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 (isNaN(step)) {\n clampedValue = clamp(parsed.current, minValue, maxValue);\n } else {\n clampedValue = snapValueToStep(parsed.current, 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) => {\n let prev = parsed.current;\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 };\n\n let decrement = () => {\n let newValue = safeNextStep('-', maxValue);\n\n if (newValue === numberValue) {\n setInputValue(format(newValue));\n }\n\n setNumberValue(newValue);\n };\n\n let incrementToMax = () => {\n if (maxValue != null) {\n setNumberValue(snapValueToStep(maxValue, minValue, maxValue, clampStep));\n }\n };\n\n let decrementToMin = () => {\n if (minValue != null) {\n setNumberValue(minValue);\n }\n };\n\n let canIncrement = useMemo(() => (\n !isDisabled &&\n !isReadOnly &&\n (\n isNaN(parsedValue) ||\n 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 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 validate,\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n canIncrement,\n canDecrement,\n minValue,\n maxValue,\n numberValue: parsedValue,\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 +1 @@
1
- {"mappings":"A;AAiBA;IACE;A;A;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;A;A;OAGG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,4FAA4F;IAC5F,YAAY,EAAE,OAAO,CAAC;IACtB,4FAA4F;IAC5F,YAAY,EAAE,OAAO,CAAC;IACtB;A;A;A;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,gDAAgD;IAChD,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC;A;A;A;A;OAKG;IACH,MAAM,IAAI,IAAI,CAAC;IACf,0FAA0F;IAC1F,SAAS,IAAI,IAAI,CAAC;IAClB,0FAA0F;IAC1F,SAAS,IAAI,IAAI,CAAC;IAClB,6EAA6E;IAC7E,cAAc,IAAI,IAAI,CAAC;IACvB,6EAA6E;IAC7E,cAAc,IAAI,IAAI,CAAA;CACvB;AAED,+BAAgC,SAAQ,gBAAgB;IACtD;A;A;OAGG;IACH,MAAM,EAAE,MAAM,CAAA;CACf;AAED;A;A;GAGG;AACH,oCACE,KAAK,EAAE,qBAAqB,GAC3B,gBAAgB,CA6KlB","sources":["./packages/@react-stately/numberfield/src/packages/@react-stately/numberfield/src/useNumberFieldState.ts","./packages/@react-stately/numberfield/src/packages/@react-stately/numberfield/src/index.ts"],"sourcesContent":[null,null],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";AAiBA;IACE;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,4FAA4F;IAC5F,YAAY,EAAE,OAAO,CAAC;IACtB,4FAA4F;IAC5F,YAAY,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,gDAAgD;IAChD,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC;;;;;OAKG;IACH,MAAM,IAAI,IAAI,CAAC;IACf,0FAA0F;IAC1F,SAAS,IAAI,IAAI,CAAC;IAClB,0FAA0F;IAC1F,SAAS,IAAI,IAAI,CAAC;IAClB,6EAA6E;IAC7E,cAAc,IAAI,IAAI,CAAC;IACvB,6EAA6E;IAC7E,cAAc,IAAI,IAAI,CAAA;CACvB;AAED,+BAAgC,SAAQ,gBAAgB;IACtD;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAA;CACf;AAED;;;GAGG;AACH,oCACE,KAAK,EAAE,qBAAqB,GAC3B,gBAAgB,CAyKlB","sources":["packages/@react-stately/numberfield/src/packages/@react-stately/numberfield/src/useNumberFieldState.ts","packages/@react-stately/numberfield/src/packages/@react-stately/numberfield/src/index.ts","packages/@react-stately/numberfield/src/index.ts"],"sourcesContent":[null,null,"/*\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\n/// <reference types=\"intl-types-extension\" />\n\nexport * from './useNumberFieldState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/numberfield",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@babel/runtime": "^7.6.2",
21
- "@internationalized/number": "^3.0.2",
22
- "@react-stately/utils": "^3.2.2",
23
- "@react-types/numberfield": "^3.0.1",
24
- "@react-types/shared": "^3.8.0"
21
+ "@internationalized/number": "^3.0.4",
22
+ "@react-stately/utils": "^3.4.0",
23
+ "@react-types/numberfield": "^3.1.1",
24
+ "@react-types/shared": "^3.11.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": "^16.8.0 || ^17.0.0-rc.1"
@@ -29,5 +29,5 @@
29
29
  "publishConfig": {
30
30
  "access": "public"
31
31
  },
32
- "gitHead": "9204e1b8cb61dac767e91087fb16203611dc67c5"
32
+ "gitHead": "54c2366c4f31bd4bf619126131cd583c12972acc"
33
33
  }
@@ -168,31 +168,27 @@ export function useNumberFieldState(
168
168
  };
169
169
 
170
170
  let increment = () => {
171
- setNumberValue((previousValue) => {
172
- let newValue = safeNextStep('+', minValue);
173
-
174
- // if we've arrived at the same value that was previously in the state, the
175
- // input value should be updated to match
176
- // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
177
- // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
178
- if (newValue === previousValue) {
179
- setInputValue(format(newValue));
180
- }
171
+ let newValue = safeNextStep('+', minValue);
172
+
173
+ // if we've arrived at the same value that was previously in the state, the
174
+ // input value should be updated to match
175
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
176
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
177
+ if (newValue === numberValue) {
178
+ setInputValue(format(newValue));
179
+ }
181
180
 
182
- return newValue;
183
- });
181
+ setNumberValue(newValue);
184
182
  };
185
183
 
186
184
  let decrement = () => {
187
- setNumberValue((previousValue) => {
188
- let newValue = safeNextStep('-', maxValue);
185
+ let newValue = safeNextStep('-', maxValue);
189
186
 
190
- if (newValue === previousValue) {
191
- setInputValue(format(newValue));
192
- }
187
+ if (newValue === numberValue) {
188
+ setInputValue(format(newValue));
189
+ }
193
190
 
194
- return newValue;
195
- });
191
+ setNumberValue(newValue);
196
192
  };
197
193
 
198
194
  let incrementToMax = () => {