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