@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
|
@@ -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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
-
|
|
417
|
+
incrementRef.current?.();
|
|
264
418
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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
|
-
|
|
336
|
-
|
|
337
|
-
{
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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
|
-
|
|
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
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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
|
-
|
|
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";
|