-
-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathNumberFieldInput.tsx
More file actions
485 lines (421 loc) · 17.4 KB
/
Copy pathNumberFieldInput.tsx
File metadata and controls
485 lines (421 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
'use client';
import * as React from 'react';
import { SafeReact } from '@base-ui/utils/safeReact';
import { warn } from '@base-ui/utils/warn';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { useNumberFieldRootContext } from '../root/NumberFieldRootContext';
import type { BaseUIComponentProps } from '../../internals/types';
import { useFieldRootContext } from '../../internals/field-root-context/FieldRootContext';
import { useRegisterFieldControl } from '../../internals/field-register-control/useRegisterFieldControl';
import { useFormContext } from '../../internals/form-context/FormContext';
import { useLabelableContext } from '../../internals/labelable-provider/LabelableContext';
import {
getNumberLocaleDetails,
isNumeralChar,
isValidInputString,
parseNumber,
ANY_MINUS_RE,
ANY_PLUS_RE,
ANY_MINUS_DETECT_RE,
ANY_PLUS_DETECT_RE,
} from '../utils/parse';
import type { NumberFieldRootState } from '../root/NumberFieldRoot';
import { stateAttributesMapping } from '../utils/stateAttributesMapping';
import { useRenderElement } from '../../internals/useRenderElement';
import {
createChangeEventDetails,
createGenericEventDetails,
} from '../../internals/createBaseUIEventDetails';
import { formatNumber } from '../../utils/formatNumber';
import { useValueChanged } from '../../internals/useValueChanged';
import { REASONS } from '../../internals/reasons';
import { hasNumberFormatRoundingOptions, removeFloatingPointErrors } from '../utils/validate';
const NAVIGATE_KEYS = new Set([
'Backspace',
'Delete',
'ArrowLeft',
'ArrowRight',
'Tab',
'Enter',
'Escape',
]);
/**
* The native input control in the number field.
* Renders an `<input>` element.
*
* Documentation: [Base UI Number Field](https://base-ui.com/react/components/number-field)
*/
export const NumberFieldInput = React.forwardRef(function NumberFieldInput(
componentProps: NumberFieldInput.Props,
forwardedRef: React.ForwardedRef<HTMLInputElement>,
) {
const { render, className, style, ...elementProps } = componentProps;
const {
allowInputSyncRef,
formatOptionsRef,
getAllowedNonNumericKeys,
getStepAmount,
id,
incrementValue,
inputMode,
max,
min,
name,
nameProp,
setValue,
state,
setInputValue,
locale,
inputRef,
onValueCommitted,
lastChangedValueRef,
hasPendingCommitRef,
valueRef,
} = useNumberFieldRootContext();
const { disabled, readOnly, required, value, inputValue } = state;
const { clearErrors } = useFormContext();
const { validationMode, setTouched, setFocused, invalid, shouldValidateOnChange, validation } =
useFieldRootContext();
const { labelId } = useLabelableContext();
const hasTouchedInputRef = React.useRef(false);
const blockRevalidationRef = React.useRef(false);
const pendingCaretRef = React.useRef<number | null>(null);
useRegisterFieldControl(inputRef, id, value, undefined, !disabled, nameProp);
// After a paste splices text into the controlled value, the browser would otherwise drop the
// caret at the end of the new value. Restore it just after the inserted text.
useIsoLayoutEffect(() => {
if (pendingCaretRef.current != null) {
const caret = pendingCaretRef.current;
pendingCaretRef.current = null;
inputRef.current?.setSelectionRange(caret, caret);
}
});
useValueChanged(value, () => {
clearErrors(name);
if (blockRevalidationRef.current && !shouldValidateOnChange()) {
blockRevalidationRef.current = false;
return;
}
validation.change(value);
});
const inputProps: React.ComponentProps<'input'> = {
id,
required,
disabled,
readOnly,
inputMode,
value: inputValue,
type: 'text',
autoComplete: 'off',
autoCorrect: 'off',
spellCheck: 'false',
'aria-roledescription': 'Number field',
'aria-invalid': !disabled && invalid ? true : undefined,
'aria-labelledby': labelId,
// If the server's locale does not match the client's locale, the formatting may not match,
// causing a hydration mismatch.
suppressHydrationWarning: true,
onFocus(event) {
// Read-only inputs are still focusable; only the value-changing handlers stay gated on it.
if (event.defaultPrevented || disabled) {
return;
}
setFocused(true);
if (hasTouchedInputRef.current) {
return;
}
hasTouchedInputRef.current = true;
// Browsers set selection at the start of the input field by default. We want to set it at
// the end for the first focus.
const target = event.currentTarget;
const length = target.value.length;
target.setSelectionRange(length, length);
},
onBlur(event) {
if (event.defaultPrevented || disabled) {
return;
}
setTouched(true);
setFocused(false);
if (readOnly) {
return;
}
const hadManualInput = !allowInputSyncRef.current;
const hadPendingProgrammaticChange = hasPendingCommitRef.current;
allowInputSyncRef.current = true;
if (inputValue.trim() === '') {
const clearDetails = createChangeEventDetails(REASONS.inputClear, event.nativeEvent);
setValue(null, clearDetails);
// Respect a canceled clear, mirroring the non-empty blur path below.
if (clearDetails.isCanceled) {
return;
}
if (validationMode === 'onBlur') {
validation.commit(null);
}
// Don't report a commit when blurring an already-empty field that the user never
// interacted with: nothing was cleared and no programmatic change is pending.
if (hadManualInput || hadPendingProgrammaticChange || value !== null) {
onValueCommitted(null, createGenericEventDetails(REASONS.inputClear, event.nativeEvent));
}
return;
}
const formatOptions = formatOptionsRef.current;
const parsedValue = parseNumber(inputValue, locale, formatOptions);
if (parsedValue === null) {
return;
}
// Avoid applying Intl's default precision unless the format opts into rounding.
const hasRoundingOptions = hasNumberFormatRoundingOptions(formatOptions);
let committed: number | null;
if (!hadManualInput && !hasRoundingOptions) {
// No rounding options and no manual edit: the visible text is purely formatted
// display, so keep the authoritative numeric value as-is rather than re-parsing the
// rounded text and discarding precision (e.g. focus/blur with no edits, or blur after
// a programmatic change).
committed = value;
} else if (hasRoundingOptions) {
// Explicit rounding options apply to the committed value, whether typed or external.
committed = removeFloatingPointErrors(parsedValue, formatOptions);
} else {
committed = parsedValue;
}
const nextEventDetails = createGenericEventDetails(REASONS.inputBlur, event.nativeEvent);
const shouldUpdateValue = value !== committed;
const shouldCommit = hadManualInput || shouldUpdateValue || hadPendingProgrammaticChange;
// Use the stored value after `setValue` clamps it.
let committedValue = committed;
if (shouldUpdateValue) {
const changeDetails = createChangeEventDetails(REASONS.inputBlur, event.nativeEvent);
blockRevalidationRef.current = true;
setValue(committed, changeDetails);
if (changeDetails.isCanceled) {
blockRevalidationRef.current = false;
return;
}
committedValue = lastChangedValueRef.current;
// If validation normalized back to the current value, `useValueChanged` won't fire to
// reset the flag, so reset it here or the next external change won't revalidate.
if (committedValue === value) {
blockRevalidationRef.current = false;
}
}
if (validationMode === 'onBlur') {
validation.commit(committedValue);
}
if (shouldCommit) {
onValueCommitted(committedValue, nextEventDetails);
}
// Normalize only the displayed text
const canonicalText = formatNumber(committedValue, locale, formatOptions);
if (inputValue !== canonicalText) {
setInputValue(canonicalText);
}
},
onChange(event) {
// Workaround for https://github.com/react/react/issues/9023
if (event.nativeEvent.defaultPrevented) {
return;
}
allowInputSyncRef.current = false;
const targetValue = event.currentTarget.value;
if (targetValue.trim() === '') {
setInputValue(targetValue);
setValue(null, createChangeEventDetails(REASONS.inputClear, event.nativeEvent));
return;
}
// Update the input text immediately and only fire onValueChange if the typed value is
// currently parseable into a number. This preserves good UX for IME
// composition/partial input while still providing live numeric updates when possible.
if (!isValidInputString(targetValue, getAllowedNonNumericKeys())) {
return;
}
const parsedValue = parseNumber(targetValue, locale, formatOptionsRef.current);
setInputValue(targetValue);
if (parsedValue !== null) {
setValue(parsedValue, createChangeEventDetails(REASONS.inputChange, event.nativeEvent));
}
},
onKeyDown(event) {
if (event.defaultPrevented || readOnly || disabled) {
return;
}
const nativeEvent = event.nativeEvent;
// Snapshot the dirty state without clearing it: navigation/allowed keys (ArrowLeft, Tab,
// Enter, Escape, …) return early without changing the value, so marking the input synced
// here would wrongly discard dirty-input authority. Only the value-changing branches below
// mark it synced.
const hadManualInput = !allowInputSyncRef.current;
const allowedNonNumericKeys = getAllowedNonNumericKeys();
let isAllowedNonNumericKey = allowedNonNumericKeys.has(event.key);
const { decimal, currency, percentSign } = getNumberLocaleDetails(
locale,
formatOptionsRef.current,
);
const selectionStart = event.currentTarget.selectionStart;
const selectionEnd = event.currentTarget.selectionEnd;
const isAllSelected = selectionStart === 0 && selectionEnd === inputValue.length;
const selectionContainsIndex = (index: number) =>
selectionStart != null &&
selectionEnd != null &&
index >= selectionStart &&
index < selectionEnd;
// Only allow a single sign character: permit it when there is no existing sign of either
// kind, when all text is selected, or when the selection covers the existing sign so it's
// being replaced.
const signGroups = [
[ANY_MINUS_DETECT_RE, ANY_MINUS_RE],
[ANY_PLUS_DETECT_RE, ANY_PLUS_RE],
] as const;
signGroups.forEach(([detectRe, globalRe]) => {
if (
detectRe.test(event.key) &&
Array.from(allowedNonNumericKeys).some((k) => detectRe.test(k))
) {
const existingIndex = inputValue.search(globalRe);
const isReplacingExisting = existingIndex !== -1 && selectionContainsIndex(existingIndex);
isAllowedNonNumericKey =
!(ANY_MINUS_DETECT_RE.test(inputValue) || ANY_PLUS_DETECT_RE.test(inputValue)) ||
isAllSelected ||
isReplacingExisting;
}
});
// Only allow one of each symbol.
[decimal, currency, percentSign].forEach((symbol) => {
if (event.key === symbol) {
const symbolIndex = inputValue.indexOf(symbol);
const isSymbolHighlighted = selectionContainsIndex(symbolIndex);
isAllowedNonNumericKey = symbolIndex === -1 || isAllSelected || isSymbolHighlighted;
}
});
const isNavigateKey = NAVIGATE_KEYS.has(event.key);
// Alt+ArrowUp/ArrowDown selects smallStep, so don't treat it as a bypass modifier.
const isStepKey = event.key === 'ArrowUp' || event.key === 'ArrowDown';
if (
// Allow composition events (e.g., pinyin)
// event.nativeEvent.isComposing does not work in Safari:
// https://bugs.webkit.org/show_bug.cgi?id=165004
event.which === 229 ||
(event.altKey && !isStepKey) ||
event.ctrlKey ||
event.metaKey ||
isAllowedNonNumericKey ||
isNumeralChar(event.key) ||
isNavigateKey
) {
return;
}
// Home/End jump to the corresponding bound, but only when that bound is defined.
let boundaryValue: number | null = null;
if (event.key === 'Home' && min != null) {
boundaryValue = min;
} else if (event.key === 'End' && max != null) {
boundaryValue = max;
}
// Let the browser handle multi-character keys we don't act on (PageUp, Insert, F-keys,
// Home/End without min/max); invalid single characters are still blocked below.
if (event.key.length > 1 && !isStepKey && boundaryValue === null) {
return;
}
// Step from the authoritative numeric value unless the input has unsaved manual edits.
// When the text is already synced, parsing the rounded display would collapse precision,
// so pass no `currentValue` and let `incrementValue` fall back to the numeric state
// (mirrors the button path).
const currentValue = hadManualInput
? parseNumber(inputValue, locale, formatOptionsRef.current)
: null;
const amount = getStepAmount(event);
// Prevent insertion of text or caret from moving.
event.preventDefault();
event.stopPropagation();
const commitDetails = createGenericEventDetails(REASONS.keyboard, nativeEvent);
let changed = false;
if (isStepKey || boundaryValue !== null) {
allowInputSyncRef.current = true;
}
if (isStepKey) {
// When stepping from the synced numeric state, refresh the commit ref to the current
// value so a canceled step can't commit a stale `lastChangedValueRef` left over from an
// earlier change (mirrors the button path).
if (!hadManualInput) {
lastChangedValueRef.current = valueRef.current;
}
changed = incrementValue(amount, {
direction: event.key === 'ArrowUp' ? 1 : -1,
currentValue,
event: nativeEvent,
reason: REASONS.keyboard,
});
} else if (boundaryValue !== null) {
changed = setValue(boundaryValue, createChangeEventDetails(REASONS.keyboard, nativeEvent));
}
// `changed` is only true when `setValue` applied the change, which records the stored
// (clamped/snapped) value, so commit that rather than the pre-validation input.
if (changed) {
onValueCommitted(lastChangedValueRef.current, commitDetails);
}
},
onPaste(event) {
if (event.defaultPrevented || readOnly || disabled) {
return;
}
let pastedData = '';
try {
pastedData = event.clipboardData?.getData('text/plain') ?? '';
} catch {
/* istanbul ignore else -- `process.env.NODE_ENV` is a build-time constant under test */
if (process.env.NODE_ENV !== 'production') {
const ownerStackMessage = SafeReact.captureOwnerStack?.() || '';
warn(
'<NumberField.Input> could not read clipboard text during paste handling.',
ownerStackMessage,
);
}
return;
}
// Prevent `onChange` from being called.
event.preventDefault();
// Insert the pasted text at the caret/selection instead of replacing the entire value,
// matching native input behavior (e.g. pasting "5" into "123|" yields "1235").
// The component renders `type="text"`, which always reports a selection range. Overriding
// `type` with a selection-less one (`email`, `number`) is unsupported either way: the caret
// restore above throws on those, so there is no working behavior to preserve here.
const input = event.currentTarget;
const selectionStart = input.selectionStart!;
const selectionEnd = input.selectionEnd!;
const nextText =
inputValue.slice(0, selectionStart) + pastedData + inputValue.slice(selectionEnd);
const parsedValue = parseNumber(nextText, locale, formatOptionsRef.current);
if (parsedValue !== null) {
allowInputSyncRef.current = false;
pendingCaretRef.current = selectionStart + pastedData.length;
setValue(parsedValue, createChangeEventDetails(REASONS.inputPaste, event.nativeEvent));
setInputValue(nextText);
}
},
};
const element = useRenderElement('input', componentProps, {
ref: [forwardedRef, inputRef],
state,
props: [inputProps, elementProps, (props) => validation.getValidationProps(disabled, props)],
stateAttributesMapping,
});
return element;
});
export interface NumberFieldInputState extends NumberFieldRootState {}
export interface NumberFieldInputProps extends BaseUIComponentProps<
'input',
NumberFieldInputState,
React.ComponentPropsWithRef<'input'>
> {
/**
* A user-friendly description of the input's role for assistive tech. This is a role
* description, not an accessible name — use `Field.Label` or `aria-label` to name the control.
* @default 'Number field'
*/
'aria-roledescription'?: React.AriaAttributes['aria-roledescription'] | undefined;
}
export namespace NumberFieldInput {
export type State = NumberFieldInputState;
export type Props = NumberFieldInputProps;
}