@mantine/core 9.0.0-alpha.1 → 9.0.0-alpha.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/cjs/components/NumberInput/NumberInput.cjs +482 -290
- package/cjs/components/NumberInput/NumberInput.cjs.map +1 -1
- package/cjs/core/MantineProvider/color-functions/default-variant-colors-resolver/v8-default-variant-colors-resolver.cjs +196 -0
- package/cjs/core/MantineProvider/color-functions/default-variant-colors-resolver/v8-default-variant-colors-resolver.cjs.map +1 -0
- package/cjs/index.cjs +2 -0
- package/cjs/index.cjs.map +1 -1
- package/esm/components/NumberInput/NumberInput.mjs +483 -291
- package/esm/components/NumberInput/NumberInput.mjs.map +1 -1
- package/esm/core/MantineProvider/color-functions/default-variant-colors-resolver/v8-default-variant-colors-resolver.mjs +194 -0
- package/esm/core/MantineProvider/color-functions/default-variant-colors-resolver/v8-default-variant-colors-resolver.mjs.map +1 -0
- package/esm/index.mjs +1 -0
- package/esm/index.mjs.map +1 -1
- package/lib/components/Input/use-input-props.d.ts +4 -4
- package/lib/components/NumberInput/NumberInput.d.ts +42 -10
- package/lib/components/NumberInput/index.d.ts +3 -3
- package/lib/core/MantineProvider/color-functions/default-variant-colors-resolver/v8-default-variant-colors-resolver.d.ts +2 -0
- package/lib/core/MantineProvider/color-functions/get-contrast-color/get-contrast-color.d.ts +2 -2
- package/lib/core/MantineProvider/color-functions/index.d.ts +1 -0
- package/package.json +2 -2
|
@@ -17,7 +17,7 @@ import '../../core/MantineProvider/MantineThemeProvider/MantineThemeProvider.mjs
|
|
|
17
17
|
import { useProps } from '../../core/MantineProvider/use-props/use-props.mjs';
|
|
18
18
|
import '../../core/MantineProvider/MantineCssVariables/MantineCssVariables.mjs';
|
|
19
19
|
import '../../core/Box/Box.mjs';
|
|
20
|
-
import {
|
|
20
|
+
import { genericFactory } from '../../core/factory/factory.mjs';
|
|
21
21
|
import '../../core/DirectionProvider/DirectionProvider.mjs';
|
|
22
22
|
import { InputBase } from '../InputBase/InputBase.mjs';
|
|
23
23
|
import { UnstyledButton } from '../UnstyledButton/UnstyledButton.mjs';
|
|
@@ -31,12 +31,61 @@ const trailingDecimalSeparatorPattern = /^-?\d+\.$/;
|
|
|
31
31
|
function isNumberString(value) {
|
|
32
32
|
return typeof value === "string" && value !== "" && !Number.isNaN(Number(value));
|
|
33
33
|
}
|
|
34
|
+
function isBigIntValue(value) {
|
|
35
|
+
return typeof value === "bigint";
|
|
36
|
+
}
|
|
34
37
|
function canStep(value) {
|
|
35
38
|
if (typeof value === "number") {
|
|
36
39
|
return value < Number.MAX_SAFE_INTEGER;
|
|
37
40
|
}
|
|
38
41
|
return value === "" || isNumberString(value) && Number(value) < Number.MAX_SAFE_INTEGER;
|
|
39
42
|
}
|
|
43
|
+
function isValidBigIntString(value, allowNegative) {
|
|
44
|
+
if (value === "") {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (value === "-") {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (!allowNegative && value.startsWith("-")) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return /^-?\d+$/.test(value);
|
|
54
|
+
}
|
|
55
|
+
function canStepBigInt(value, allowNegative) {
|
|
56
|
+
if (typeof value === "bigint") {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return value === "" || isValidBigIntString(value, allowNegative);
|
|
60
|
+
}
|
|
61
|
+
function parseBigIntFromString(value) {
|
|
62
|
+
if (!/^-?\d+$/.test(value)) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
return BigInt(value);
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function toBigIntOrUndefined(value) {
|
|
72
|
+
if (typeof value === "bigint") {
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
if (typeof value === "number" && Number.isFinite(value) && Number.isInteger(value)) {
|
|
76
|
+
return BigInt(value);
|
|
77
|
+
}
|
|
78
|
+
return void 0;
|
|
79
|
+
}
|
|
80
|
+
function clampBigInt(value, min, max) {
|
|
81
|
+
if (min !== void 0 && value < min) {
|
|
82
|
+
return min;
|
|
83
|
+
}
|
|
84
|
+
if (max !== void 0 && value > max) {
|
|
85
|
+
return max;
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
40
89
|
function getTotalDigits(inputValue) {
|
|
41
90
|
return inputValue.toString().replace(".", "").length;
|
|
42
91
|
}
|
|
@@ -85,306 +134,449 @@ function clampAndSanitizeInput(sanitizedValue, max, min) {
|
|
|
85
134
|
}
|
|
86
135
|
return clamped;
|
|
87
136
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
rightSection,
|
|
105
|
-
isAllowed,
|
|
106
|
-
clampBehavior,
|
|
107
|
-
onBlur,
|
|
108
|
-
allowDecimal,
|
|
109
|
-
decimalScale,
|
|
110
|
-
onKeyDown,
|
|
111
|
-
onKeyDownCapture,
|
|
112
|
-
handlersRef,
|
|
113
|
-
startValue,
|
|
114
|
-
disabled,
|
|
115
|
-
rightSectionPointerEvents,
|
|
116
|
-
allowNegative,
|
|
117
|
-
readOnly,
|
|
118
|
-
size,
|
|
119
|
-
rightSectionWidth,
|
|
120
|
-
stepHoldInterval,
|
|
121
|
-
stepHoldDelay,
|
|
122
|
-
allowLeadingZeros,
|
|
123
|
-
withKeyboardEvents,
|
|
124
|
-
trimLeadingZeroesOnBlur,
|
|
125
|
-
allowedDecimalSeparators,
|
|
126
|
-
selectAllOnFocus,
|
|
127
|
-
onMinReached,
|
|
128
|
-
onMaxReached,
|
|
129
|
-
onFocus,
|
|
130
|
-
attributes,
|
|
131
|
-
ref,
|
|
132
|
-
...others
|
|
133
|
-
} = props;
|
|
134
|
-
const getStyles = useStyles({
|
|
135
|
-
name: "NumberInput",
|
|
136
|
-
classes,
|
|
137
|
-
props,
|
|
138
|
-
classNames,
|
|
139
|
-
styles,
|
|
140
|
-
unstyled,
|
|
141
|
-
attributes,
|
|
142
|
-
vars,
|
|
143
|
-
varsResolver
|
|
144
|
-
});
|
|
145
|
-
const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi({
|
|
146
|
-
classNames,
|
|
147
|
-
styles,
|
|
148
|
-
props
|
|
149
|
-
});
|
|
150
|
-
const [_value, setValue] = useUncontrolled({
|
|
151
|
-
value,
|
|
152
|
-
defaultValue,
|
|
153
|
-
finalValue: "",
|
|
154
|
-
onChange
|
|
155
|
-
});
|
|
156
|
-
const shouldUseStepInterval = stepHoldDelay !== void 0 && stepHoldInterval !== void 0;
|
|
157
|
-
const inputRef = useRef(null);
|
|
158
|
-
const onStepTimeoutRef = useRef(null);
|
|
159
|
-
const stepCountRef = useRef(0);
|
|
160
|
-
const handleValueChange = (payload, event) => {
|
|
161
|
-
if (event.source === "event") {
|
|
162
|
-
setValue(
|
|
163
|
-
isValidNumber(payload.floatValue, payload.value) && !leadingDecimalZeroPattern.test(payload.value) && !(allowLeadingZeros ? leadingZerosPattern.test(payload.value) : false) && !trailingZerosPattern.test(payload.value) && !trailingDecimalSeparatorPattern.test(payload.value) ? payload.floatValue : payload.value
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
onValueChange?.(payload, event);
|
|
167
|
-
};
|
|
168
|
-
const getDecimalPlaces = (inputValue) => {
|
|
169
|
-
const match = String(inputValue).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
|
|
170
|
-
if (!match) {
|
|
171
|
-
return 0;
|
|
172
|
-
}
|
|
173
|
-
return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
|
|
174
|
-
};
|
|
175
|
-
const adjustCursor = (position) => {
|
|
176
|
-
if (inputRef.current && typeof position !== "undefined") {
|
|
177
|
-
inputRef.current.setSelectionRange(position, position);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
const incrementRef = useRef(noop);
|
|
181
|
-
incrementRef.current = () => {
|
|
182
|
-
if (!canStep(_value)) {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
let val;
|
|
186
|
-
const currentValuePrecision = getDecimalPlaces(_value);
|
|
187
|
-
const stepPrecision = getDecimalPlaces(step);
|
|
188
|
-
const maxPrecision = Math.max(currentValuePrecision, stepPrecision);
|
|
189
|
-
const factor = 10 ** maxPrecision;
|
|
190
|
-
if (!isNumberString(_value) && (typeof _value !== "number" || Number.isNaN(_value))) {
|
|
191
|
-
val = clamp(startValue, min, max);
|
|
192
|
-
} else if (max !== void 0) {
|
|
193
|
-
const incrementedValue = (Math.round(Number(_value) * factor) + Math.round(step * factor)) / factor;
|
|
194
|
-
if (incrementedValue > max) {
|
|
195
|
-
onMaxReached?.();
|
|
196
|
-
}
|
|
197
|
-
val = incrementedValue <= max ? incrementedValue : max;
|
|
198
|
-
} else {
|
|
199
|
-
val = (Math.round(Number(_value) * factor) + Math.round(step * factor)) / factor;
|
|
200
|
-
}
|
|
201
|
-
const formattedValue = val.toFixed(maxPrecision);
|
|
202
|
-
setValue(parseFloat(formattedValue));
|
|
203
|
-
onValueChange?.(
|
|
204
|
-
{ floatValue: parseFloat(formattedValue), formattedValue, value: formattedValue },
|
|
205
|
-
{ source: "increment" }
|
|
137
|
+
function clampAndSanitizeBigIntInput(sanitizedValue, options) {
|
|
138
|
+
if (sanitizedValue === "" || sanitizedValue === "-") {
|
|
139
|
+
return sanitizedValue;
|
|
140
|
+
}
|
|
141
|
+
const parsed = parseBigIntFromString(sanitizedValue);
|
|
142
|
+
if (parsed === null) {
|
|
143
|
+
return sanitizedValue;
|
|
144
|
+
}
|
|
145
|
+
return options.clampBehavior === "blur" ? clampBigInt(parsed, options.min, options.max) : parsed;
|
|
146
|
+
}
|
|
147
|
+
const NumberInput = genericFactory(
|
|
148
|
+
(_props) => {
|
|
149
|
+
const props = useProps(
|
|
150
|
+
"NumberInput",
|
|
151
|
+
defaultProps,
|
|
152
|
+
_props
|
|
206
153
|
);
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
154
|
+
const {
|
|
155
|
+
className,
|
|
156
|
+
classNames,
|
|
157
|
+
styles,
|
|
158
|
+
unstyled,
|
|
159
|
+
vars,
|
|
160
|
+
onChange,
|
|
161
|
+
onValueChange,
|
|
162
|
+
value,
|
|
163
|
+
defaultValue,
|
|
164
|
+
max,
|
|
165
|
+
min,
|
|
166
|
+
step,
|
|
167
|
+
hideControls,
|
|
168
|
+
rightSection,
|
|
169
|
+
isAllowed,
|
|
170
|
+
clampBehavior,
|
|
171
|
+
onBlur,
|
|
172
|
+
allowDecimal,
|
|
173
|
+
decimalScale,
|
|
174
|
+
onKeyDown,
|
|
175
|
+
onKeyDownCapture,
|
|
176
|
+
handlersRef,
|
|
177
|
+
startValue,
|
|
178
|
+
disabled,
|
|
179
|
+
rightSectionPointerEvents,
|
|
180
|
+
allowNegative,
|
|
181
|
+
readOnly,
|
|
182
|
+
size,
|
|
183
|
+
rightSectionWidth,
|
|
184
|
+
stepHoldInterval,
|
|
185
|
+
stepHoldDelay,
|
|
186
|
+
allowLeadingZeros,
|
|
187
|
+
withKeyboardEvents,
|
|
188
|
+
trimLeadingZeroesOnBlur,
|
|
189
|
+
allowedDecimalSeparators,
|
|
190
|
+
selectAllOnFocus,
|
|
191
|
+
onMinReached,
|
|
192
|
+
onMaxReached,
|
|
193
|
+
onFocus,
|
|
194
|
+
attributes,
|
|
195
|
+
ref,
|
|
196
|
+
...others
|
|
197
|
+
} = props;
|
|
198
|
+
const allowNegativeResolved = allowNegative ?? true;
|
|
199
|
+
const allowLeadingZerosResolved = allowLeadingZeros ?? true;
|
|
200
|
+
const getStyles = useStyles({
|
|
201
|
+
name: "NumberInput",
|
|
202
|
+
classes,
|
|
203
|
+
props,
|
|
204
|
+
classNames,
|
|
205
|
+
styles,
|
|
206
|
+
unstyled,
|
|
207
|
+
attributes,
|
|
208
|
+
vars,
|
|
209
|
+
varsResolver
|
|
210
|
+
});
|
|
211
|
+
const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi({
|
|
212
|
+
classNames,
|
|
213
|
+
styles,
|
|
214
|
+
props
|
|
215
|
+
});
|
|
216
|
+
const valueModeRef = useRef(
|
|
217
|
+
isBigIntValue(value) || isBigIntValue(defaultValue) ? "bigint" : "number"
|
|
234
218
|
);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (readOnly || !withKeyboardEvents) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
if (event.key === "ArrowUp") {
|
|
243
|
-
event.preventDefault();
|
|
244
|
-
incrementRef.current?.();
|
|
245
|
-
}
|
|
246
|
-
if (event.key === "ArrowDown") {
|
|
247
|
-
event.preventDefault();
|
|
248
|
-
decrementRef.current?.();
|
|
219
|
+
if (isBigIntValue(value)) {
|
|
220
|
+
valueModeRef.current = "bigint";
|
|
221
|
+
} else if (typeof value === "number") {
|
|
222
|
+
valueModeRef.current = "number";
|
|
249
223
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
224
|
+
const isBigIntMode = valueModeRef.current === "bigint";
|
|
225
|
+
const [_value, setValue] = useUncontrolled({
|
|
226
|
+
value,
|
|
227
|
+
defaultValue,
|
|
228
|
+
finalValue: "",
|
|
229
|
+
onChange
|
|
230
|
+
});
|
|
231
|
+
const shouldUseStepInterval = stepHoldDelay !== void 0 && stepHoldInterval !== void 0;
|
|
232
|
+
const inputRef = useRef(null);
|
|
233
|
+
const onStepTimeoutRef = useRef(null);
|
|
234
|
+
const stepCountRef = useRef(0);
|
|
235
|
+
const minNumber = typeof min === "number" ? min : void 0;
|
|
236
|
+
const maxNumber = typeof max === "number" ? max : void 0;
|
|
237
|
+
const stepNumber = typeof step === "number" ? step : defaultProps.step;
|
|
238
|
+
const startValueNumber = typeof startValue === "number" ? startValue : defaultProps.startValue;
|
|
239
|
+
const minBigInt = toBigIntOrUndefined(min);
|
|
240
|
+
const maxBigInt = toBigIntOrUndefined(max);
|
|
241
|
+
const stepBigInt = toBigIntOrUndefined(step) ?? BigInt(1);
|
|
242
|
+
const startValueBigInt = toBigIntOrUndefined(startValue) ?? BigInt(0);
|
|
243
|
+
const parseBigIntOrString = (inputValue) => {
|
|
244
|
+
if (!isValidBigIntString(inputValue, allowNegativeResolved) || allowLeadingZerosResolved && leadingZerosPattern.test(inputValue)) {
|
|
245
|
+
return inputValue;
|
|
246
|
+
}
|
|
247
|
+
const parsed = parseBigIntFromString(inputValue);
|
|
248
|
+
return parsed ?? inputValue;
|
|
249
|
+
};
|
|
250
|
+
const getBigIntFloatValue = (inputValue) => {
|
|
251
|
+
const numericValue = Number(inputValue);
|
|
252
|
+
return Number.isSafeInteger(numericValue) ? numericValue : void 0;
|
|
253
|
+
};
|
|
254
|
+
const handleValueChange = (payload, event) => {
|
|
255
|
+
if (event.source === "event") {
|
|
256
|
+
if (isBigIntMode) {
|
|
257
|
+
setValue(parseBigIntOrString(payload.value));
|
|
258
|
+
} else {
|
|
259
|
+
setValue(
|
|
260
|
+
isValidNumber(payload.floatValue, payload.value) && !leadingDecimalZeroPattern.test(payload.value) && !(allowLeadingZerosResolved ? leadingZerosPattern.test(payload.value) : false) && !trailingZerosPattern.test(payload.value) && !trailingDecimalSeparatorPattern.test(payload.value) ? payload.floatValue : payload.value
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
onValueChange?.(payload, event);
|
|
265
|
+
};
|
|
266
|
+
const getDecimalPlaces = (inputValue) => {
|
|
267
|
+
const match = String(inputValue).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
|
|
268
|
+
if (!match) {
|
|
269
|
+
return 0;
|
|
270
|
+
}
|
|
271
|
+
return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0));
|
|
272
|
+
};
|
|
273
|
+
const adjustCursor = (position) => {
|
|
274
|
+
if (inputRef.current && typeof position !== "undefined") {
|
|
275
|
+
inputRef.current.setSelectionRange(position, position);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
const incrementRef = useRef(noop);
|
|
279
|
+
incrementRef.current = () => {
|
|
280
|
+
if (isBigIntMode) {
|
|
281
|
+
if (!canStepBigInt(_value, allowNegativeResolved)) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
let val2;
|
|
285
|
+
const currentValue = _value;
|
|
286
|
+
if (typeof currentValue === "bigint") {
|
|
287
|
+
const incrementedValue = currentValue + stepBigInt;
|
|
288
|
+
if (maxBigInt !== void 0 && incrementedValue > maxBigInt) {
|
|
289
|
+
onMaxReached?.();
|
|
290
|
+
}
|
|
291
|
+
val2 = maxBigInt !== void 0 && incrementedValue > maxBigInt ? maxBigInt : incrementedValue;
|
|
292
|
+
} else if (typeof currentValue === "string" && currentValue !== "") {
|
|
293
|
+
const parsed = parseBigIntFromString(currentValue);
|
|
294
|
+
if (parsed === null) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const incrementedValue = parsed + stepBigInt;
|
|
298
|
+
if (maxBigInt !== void 0 && incrementedValue > maxBigInt) {
|
|
299
|
+
onMaxReached?.();
|
|
300
|
+
}
|
|
301
|
+
val2 = maxBigInt !== void 0 && incrementedValue > maxBigInt ? maxBigInt : incrementedValue;
|
|
302
|
+
} else {
|
|
303
|
+
val2 = clampBigInt(startValueBigInt, minBigInt, maxBigInt);
|
|
304
|
+
}
|
|
305
|
+
const formattedValue2 = val2.toString();
|
|
306
|
+
setValue(val2);
|
|
307
|
+
onValueChange?.(
|
|
308
|
+
{ floatValue: getBigIntFloatValue(val2), formattedValue: formattedValue2, value: formattedValue2 },
|
|
309
|
+
{ source: "increment" }
|
|
310
|
+
);
|
|
311
|
+
setTimeout(() => adjustCursor(inputRef.current?.value.length), 0);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!canStep(_value)) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
let val;
|
|
318
|
+
const currentValuePrecision = getDecimalPlaces(_value);
|
|
319
|
+
const stepPrecision = getDecimalPlaces(stepNumber);
|
|
320
|
+
const maxPrecision = Math.max(currentValuePrecision, stepPrecision);
|
|
321
|
+
const factor = 10 ** maxPrecision;
|
|
322
|
+
if (!isNumberString(_value) && (typeof _value !== "number" || Number.isNaN(_value))) {
|
|
323
|
+
val = clamp(startValueNumber, minNumber, maxNumber);
|
|
324
|
+
} else if (maxNumber !== void 0) {
|
|
325
|
+
const incrementedValue = (Math.round(Number(_value) * factor) + Math.round(stepNumber * factor)) / factor;
|
|
326
|
+
if (incrementedValue > maxNumber) {
|
|
327
|
+
onMaxReached?.();
|
|
328
|
+
}
|
|
329
|
+
val = incrementedValue <= maxNumber ? incrementedValue : maxNumber;
|
|
330
|
+
} else {
|
|
331
|
+
val = (Math.round(Number(_value) * factor) + Math.round(stepNumber * factor)) / factor;
|
|
332
|
+
}
|
|
333
|
+
const formattedValue = val.toFixed(maxPrecision);
|
|
334
|
+
setValue(parseFloat(formattedValue));
|
|
335
|
+
onValueChange?.(
|
|
336
|
+
{ floatValue: parseFloat(formattedValue), formattedValue, value: formattedValue },
|
|
337
|
+
{ source: "increment" }
|
|
338
|
+
);
|
|
339
|
+
setTimeout(() => adjustCursor(inputRef.current?.value.length), 0);
|
|
340
|
+
};
|
|
341
|
+
const decrementRef = useRef(noop);
|
|
342
|
+
decrementRef.current = () => {
|
|
343
|
+
if (isBigIntMode) {
|
|
344
|
+
if (!canStepBigInt(_value, allowNegativeResolved)) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
let val2;
|
|
348
|
+
const minValue2 = minBigInt !== void 0 ? minBigInt : !allowNegativeResolved ? BigInt(0) : void 0;
|
|
349
|
+
const currentValue = _value;
|
|
350
|
+
if (typeof currentValue === "bigint") {
|
|
351
|
+
const decrementedValue = currentValue - stepBigInt;
|
|
352
|
+
if (minValue2 !== void 0 && decrementedValue < minValue2) {
|
|
353
|
+
onMinReached?.();
|
|
354
|
+
}
|
|
355
|
+
val2 = minValue2 !== void 0 && decrementedValue < minValue2 ? minValue2 : decrementedValue;
|
|
356
|
+
} else if (typeof currentValue === "string" && currentValue !== "") {
|
|
357
|
+
const parsed = parseBigIntFromString(currentValue);
|
|
358
|
+
if (parsed === null) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const decrementedValue = parsed - stepBigInt;
|
|
362
|
+
if (minValue2 !== void 0 && decrementedValue < minValue2) {
|
|
363
|
+
onMinReached?.();
|
|
364
|
+
}
|
|
365
|
+
val2 = minValue2 !== void 0 && decrementedValue < minValue2 ? minValue2 : decrementedValue;
|
|
366
|
+
} else {
|
|
367
|
+
val2 = clampBigInt(startValueBigInt, minValue2, maxBigInt);
|
|
368
|
+
}
|
|
369
|
+
const formattedValue2 = val2.toString();
|
|
370
|
+
setValue(val2);
|
|
371
|
+
onValueChange?.(
|
|
372
|
+
{ floatValue: getBigIntFloatValue(val2), formattedValue: formattedValue2, value: formattedValue2 },
|
|
373
|
+
{ source: "decrement" }
|
|
374
|
+
);
|
|
375
|
+
setTimeout(() => adjustCursor(inputRef.current?.value.length), 0);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (!canStep(_value)) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
let val;
|
|
382
|
+
const minValue = minNumber !== void 0 ? minNumber : !allowNegativeResolved ? 0 : Number.MIN_SAFE_INTEGER;
|
|
383
|
+
const currentValuePrecision = getDecimalPlaces(_value);
|
|
384
|
+
const stepPrecision = getDecimalPlaces(stepNumber);
|
|
385
|
+
const maxPrecision = Math.max(currentValuePrecision, stepPrecision);
|
|
386
|
+
const factor = 10 ** maxPrecision;
|
|
387
|
+
if (!isNumberString(_value) && typeof _value !== "number" || Number.isNaN(_value)) {
|
|
388
|
+
val = clamp(startValueNumber, minValue, maxNumber);
|
|
389
|
+
} else {
|
|
390
|
+
const decrementedValue = (Math.round(Number(_value) * factor) - Math.round(stepNumber * factor)) / factor;
|
|
391
|
+
if (minValue !== void 0 && decrementedValue < minValue) {
|
|
392
|
+
onMinReached?.();
|
|
393
|
+
}
|
|
394
|
+
val = minValue !== void 0 && decrementedValue < minValue ? minValue : decrementedValue;
|
|
395
|
+
}
|
|
396
|
+
const formattedValue = val.toFixed(maxPrecision);
|
|
397
|
+
setValue(parseFloat(formattedValue));
|
|
398
|
+
onValueChange?.(
|
|
399
|
+
{ floatValue: parseFloat(formattedValue), formattedValue, value: formattedValue },
|
|
400
|
+
{ source: "decrement" }
|
|
401
|
+
);
|
|
402
|
+
setTimeout(() => adjustCursor(inputRef.current?.value.length), 0);
|
|
403
|
+
};
|
|
404
|
+
const handleKeyDown = (event) => {
|
|
405
|
+
onKeyDown?.(event);
|
|
406
|
+
if (readOnly || !withKeyboardEvents) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (event.key === "ArrowUp") {
|
|
256
410
|
event.preventDefault();
|
|
257
|
-
|
|
411
|
+
incrementRef.current?.();
|
|
258
412
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (selectAllOnFocus) {
|
|
263
|
-
setTimeout(() => event.currentTarget.select(), 0);
|
|
264
|
-
}
|
|
265
|
-
onFocus?.(event);
|
|
266
|
-
};
|
|
267
|
-
const handleBlur = (event) => {
|
|
268
|
-
let sanitizedValue = _value;
|
|
269
|
-
if (clampBehavior === "blur" && typeof sanitizedValue === "number") {
|
|
270
|
-
sanitizedValue = clamp(sanitizedValue, min, max);
|
|
271
|
-
}
|
|
272
|
-
if (trimLeadingZeroesOnBlur && typeof sanitizedValue === "string" && getDecimalPlaces(sanitizedValue) < 15) {
|
|
273
|
-
sanitizedValue = clampAndSanitizeInput(sanitizedValue, max, min);
|
|
274
|
-
}
|
|
275
|
-
if (_value !== sanitizedValue) {
|
|
276
|
-
setValue(sanitizedValue);
|
|
277
|
-
}
|
|
278
|
-
onBlur?.(event);
|
|
279
|
-
};
|
|
280
|
-
assignRef(handlersRef, { increment: incrementRef.current, decrement: decrementRef.current });
|
|
281
|
-
const onStepHandleChange = (isIncrement) => {
|
|
282
|
-
if (isIncrement) {
|
|
283
|
-
incrementRef.current?.();
|
|
284
|
-
} else {
|
|
285
|
-
decrementRef.current?.();
|
|
286
|
-
}
|
|
287
|
-
stepCountRef.current += 1;
|
|
288
|
-
};
|
|
289
|
-
const onStepLoop = (isIncrement) => {
|
|
290
|
-
onStepHandleChange(isIncrement);
|
|
291
|
-
if (shouldUseStepInterval) {
|
|
292
|
-
const interval = typeof stepHoldInterval === "number" ? stepHoldInterval : stepHoldInterval(stepCountRef.current);
|
|
293
|
-
onStepTimeoutRef.current = window.setTimeout(() => onStepLoop(isIncrement), interval);
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
const onStep = (event, isIncrement) => {
|
|
297
|
-
event.preventDefault();
|
|
298
|
-
inputRef.current?.focus();
|
|
299
|
-
onStepHandleChange(isIncrement);
|
|
300
|
-
if (shouldUseStepInterval) {
|
|
301
|
-
onStepTimeoutRef.current = window.setTimeout(() => onStepLoop(isIncrement), stepHoldDelay);
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
const onStepDone = () => {
|
|
305
|
-
if (onStepTimeoutRef.current) {
|
|
306
|
-
window.clearTimeout(onStepTimeoutRef.current);
|
|
307
|
-
}
|
|
308
|
-
onStepTimeoutRef.current = null;
|
|
309
|
-
stepCountRef.current = 0;
|
|
310
|
-
};
|
|
311
|
-
const controls = /* @__PURE__ */ jsxs("div", { ...getStyles("controls"), children: [
|
|
312
|
-
/* @__PURE__ */ jsx(
|
|
313
|
-
UnstyledButton,
|
|
314
|
-
{
|
|
315
|
-
...getStyles("control"),
|
|
316
|
-
tabIndex: -1,
|
|
317
|
-
"aria-hidden": true,
|
|
318
|
-
disabled: disabled || typeof _value === "number" && max !== void 0 && _value >= max,
|
|
319
|
-
mod: { direction: "up" },
|
|
320
|
-
onMouseDown: (event) => event.preventDefault(),
|
|
321
|
-
onPointerDown: (event) => {
|
|
322
|
-
onStep(event, true);
|
|
323
|
-
},
|
|
324
|
-
onPointerUp: onStepDone,
|
|
325
|
-
onPointerLeave: onStepDone,
|
|
326
|
-
children: /* @__PURE__ */ jsx(NumberInputChevron, { direction: "up" })
|
|
413
|
+
if (event.key === "ArrowDown") {
|
|
414
|
+
event.preventDefault();
|
|
415
|
+
decrementRef.current?.();
|
|
327
416
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
{
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
onMouseDown: (event) => event.preventDefault(),
|
|
338
|
-
onPointerDown: (event) => {
|
|
339
|
-
onStep(event, false);
|
|
340
|
-
},
|
|
341
|
-
onPointerUp: onStepDone,
|
|
342
|
-
onPointerLeave: onStepDone,
|
|
343
|
-
children: /* @__PURE__ */ jsx(NumberInputChevron, { direction: "down" })
|
|
417
|
+
};
|
|
418
|
+
const handleKeyDownCapture = (event) => {
|
|
419
|
+
onKeyDownCapture?.(event);
|
|
420
|
+
if (event.key === "Backspace") {
|
|
421
|
+
const input = inputRef.current;
|
|
422
|
+
if (input && input.selectionStart === 0 && input.selectionStart === input.selectionEnd) {
|
|
423
|
+
event.preventDefault();
|
|
424
|
+
window.setTimeout(() => adjustCursor(0), 0);
|
|
425
|
+
}
|
|
344
426
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
427
|
+
};
|
|
428
|
+
const handleFocus = (event) => {
|
|
429
|
+
if (selectAllOnFocus) {
|
|
430
|
+
setTimeout(() => event.currentTarget.select(), 0);
|
|
431
|
+
}
|
|
432
|
+
onFocus?.(event);
|
|
433
|
+
};
|
|
434
|
+
const handleBlur = (event) => {
|
|
435
|
+
let sanitizedValue = _value;
|
|
436
|
+
if (isBigIntMode) {
|
|
437
|
+
if (clampBehavior === "blur" && typeof sanitizedValue === "bigint") {
|
|
438
|
+
sanitizedValue = clampBigInt(sanitizedValue, minBigInt, maxBigInt);
|
|
439
|
+
}
|
|
440
|
+
if (trimLeadingZeroesOnBlur && typeof sanitizedValue === "string") {
|
|
441
|
+
sanitizedValue = clampAndSanitizeBigIntInput(sanitizedValue, {
|
|
442
|
+
min: minBigInt,
|
|
443
|
+
max: maxBigInt,
|
|
444
|
+
clampBehavior
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
if (clampBehavior === "blur" && typeof sanitizedValue === "number") {
|
|
449
|
+
sanitizedValue = clamp(sanitizedValue, minNumber, maxNumber);
|
|
450
|
+
}
|
|
451
|
+
if (trimLeadingZeroesOnBlur && typeof sanitizedValue === "string" && getDecimalPlaces(sanitizedValue) < 15) {
|
|
452
|
+
sanitizedValue = clampAndSanitizeInput(sanitizedValue, maxNumber, minNumber);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (_value !== sanitizedValue) {
|
|
456
|
+
setValue(sanitizedValue);
|
|
457
|
+
}
|
|
458
|
+
onBlur?.(event);
|
|
459
|
+
};
|
|
460
|
+
assignRef(handlersRef, { increment: incrementRef.current, decrement: decrementRef.current });
|
|
461
|
+
const onStepHandleChange = (isIncrement) => {
|
|
462
|
+
if (isIncrement) {
|
|
463
|
+
incrementRef.current?.();
|
|
464
|
+
} else {
|
|
465
|
+
decrementRef.current?.();
|
|
466
|
+
}
|
|
467
|
+
stepCountRef.current += 1;
|
|
468
|
+
};
|
|
469
|
+
const onStepLoop = (isIncrement) => {
|
|
470
|
+
onStepHandleChange(isIncrement);
|
|
471
|
+
if (shouldUseStepInterval) {
|
|
472
|
+
const interval = typeof stepHoldInterval === "number" ? stepHoldInterval : stepHoldInterval(stepCountRef.current);
|
|
473
|
+
onStepTimeoutRef.current = window.setTimeout(() => onStepLoop(isIncrement), interval);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
const onStep = (event, isIncrement) => {
|
|
477
|
+
event.preventDefault();
|
|
478
|
+
inputRef.current?.focus();
|
|
479
|
+
onStepHandleChange(isIncrement);
|
|
480
|
+
if (shouldUseStepInterval) {
|
|
481
|
+
onStepTimeoutRef.current = window.setTimeout(() => onStepLoop(isIncrement), stepHoldDelay);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
const onStepDone = () => {
|
|
485
|
+
if (onStepTimeoutRef.current) {
|
|
486
|
+
window.clearTimeout(onStepTimeoutRef.current);
|
|
487
|
+
}
|
|
488
|
+
onStepTimeoutRef.current = null;
|
|
489
|
+
stepCountRef.current = 0;
|
|
490
|
+
};
|
|
491
|
+
const controls = /* @__PURE__ */ jsxs("div", { ...getStyles("controls"), children: [
|
|
492
|
+
/* @__PURE__ */ jsx(
|
|
493
|
+
UnstyledButton,
|
|
494
|
+
{
|
|
495
|
+
...getStyles("control"),
|
|
496
|
+
tabIndex: -1,
|
|
497
|
+
"aria-hidden": true,
|
|
498
|
+
disabled: disabled || typeof _value === "number" && maxNumber !== void 0 && _value >= maxNumber || typeof _value === "bigint" && maxBigInt !== void 0 && _value >= maxBigInt,
|
|
499
|
+
mod: { direction: "up" },
|
|
500
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
501
|
+
onPointerDown: (event) => {
|
|
502
|
+
onStep(event, true);
|
|
503
|
+
},
|
|
504
|
+
onPointerUp: onStepDone,
|
|
505
|
+
onPointerLeave: onStepDone,
|
|
506
|
+
children: /* @__PURE__ */ jsx(NumberInputChevron, { direction: "up" })
|
|
507
|
+
}
|
|
508
|
+
),
|
|
509
|
+
/* @__PURE__ */ jsx(
|
|
510
|
+
UnstyledButton,
|
|
511
|
+
{
|
|
512
|
+
...getStyles("control"),
|
|
513
|
+
tabIndex: -1,
|
|
514
|
+
"aria-hidden": true,
|
|
515
|
+
disabled: disabled || typeof _value === "number" && minNumber !== void 0 && _value <= minNumber || typeof _value === "bigint" && minBigInt !== void 0 && _value <= minBigInt,
|
|
516
|
+
mod: { direction: "down" },
|
|
517
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
518
|
+
onPointerDown: (event) => {
|
|
519
|
+
onStep(event, false);
|
|
520
|
+
},
|
|
521
|
+
onPointerUp: onStepDone,
|
|
522
|
+
onPointerLeave: onStepDone,
|
|
523
|
+
children: /* @__PURE__ */ jsx(NumberInputChevron, { direction: "down" })
|
|
524
|
+
}
|
|
525
|
+
)
|
|
526
|
+
] });
|
|
527
|
+
return /* @__PURE__ */ jsx(
|
|
528
|
+
InputBase,
|
|
529
|
+
{
|
|
530
|
+
component: NumericFormat,
|
|
531
|
+
allowNegative,
|
|
532
|
+
className: cx(classes.root, className),
|
|
533
|
+
size,
|
|
534
|
+
...others,
|
|
535
|
+
inputMode: isBigIntMode ? "numeric" : "decimal",
|
|
536
|
+
readOnly,
|
|
537
|
+
disabled,
|
|
538
|
+
value: typeof _value === "bigint" ? _value.toString() : _value,
|
|
539
|
+
getInputRef: useMergedRef(ref, inputRef),
|
|
540
|
+
onValueChange: handleValueChange,
|
|
541
|
+
rightSection: hideControls || readOnly || !(isBigIntMode ? canStepBigInt(_value, allowNegativeResolved) : canStep(_value)) ? rightSection : rightSection || controls,
|
|
542
|
+
classNames: resolvedClassNames,
|
|
543
|
+
styles: resolvedStyles,
|
|
544
|
+
unstyled,
|
|
545
|
+
__staticSelector: "NumberInput",
|
|
546
|
+
decimalScale: isBigIntMode ? 0 : allowDecimal ? decimalScale : 0,
|
|
547
|
+
onFocus: handleFocus,
|
|
548
|
+
onKeyDown: handleKeyDown,
|
|
549
|
+
onKeyDownCapture: handleKeyDownCapture,
|
|
550
|
+
rightSectionPointerEvents: rightSectionPointerEvents ?? (disabled ? "none" : void 0),
|
|
551
|
+
rightSectionWidth: rightSectionWidth ?? `var(--ni-right-section-width-${size || "sm"})`,
|
|
552
|
+
allowLeadingZeros,
|
|
553
|
+
allowedDecimalSeparators,
|
|
554
|
+
onBlur: handleBlur,
|
|
555
|
+
attributes,
|
|
556
|
+
isAllowed: (val) => {
|
|
557
|
+
const userAllowed = isAllowed ? isAllowed(val) : true;
|
|
558
|
+
if (!userAllowed) {
|
|
559
|
+
return false;
|
|
380
560
|
}
|
|
381
|
-
|
|
561
|
+
if (clampBehavior !== "strict") {
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
if (!isBigIntMode) {
|
|
565
|
+
return isInRange(val.floatValue, minNumber, maxNumber);
|
|
566
|
+
}
|
|
567
|
+
if (val.value === "" || val.value === "-") {
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
const parsed = parseBigIntFromString(val.value);
|
|
571
|
+
if (parsed === null) {
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
return (minBigInt === void 0 || parsed >= minBigInt) && (maxBigInt === void 0 || parsed <= maxBigInt);
|
|
382
575
|
}
|
|
383
|
-
return isAllowed ? isAllowed(val) : true;
|
|
384
576
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
);
|
|
388
580
|
NumberInput.classes = { ...InputBase.classes, ...classes };
|
|
389
581
|
NumberInput.varsResolver = varsResolver;
|
|
390
582
|
NumberInput.displayName = "@mantine/core/NumberInput";
|