Repository navigation
[number field] Fix keyboard editing with multi-character format symbols - #5111
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,337.87 ms -57.52 ms(-4.1%) | Renders: 78 (+0)
12 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
b184097 to
6345cb6
Compare
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
6566700 to
bf53a5a
Compare
Currency symbols longer than one character (e.g. pt-BR BRL `R$`), units (`km/h`, `°C`), and scientific notation could not be edited by typing: the allowed-key set added these symbols as whole strings, but the input validates the typed text character by character, so every keystroke was rejected.
Derive the allowed non-numeric characters from `Intl.NumberFormat.formatToParts` and decompose them per character, which covers multi-character and locale-specific symbols of every part type uniformly. `compact` suffixes stay excluded because `parseNumber` can't reverse them.
Also stop the input validator from rejecting Unicode format/bidi controls (`\p{Cf}`): RTL locales insert marks such as U+200E around the exponent sign in scientific notation, and `parseNumber` already strips them, so the validator must treat them as ignorable rather than discarding the keystroke.
Fixes mui#5107.
bf53a5a to
7949988
Compare
There was a problem hiding this comment.
Pull request overview
Fixes NumberField keyboard editing for locales/formats whose Intl.NumberFormat output contains multi-character symbols (e.g. R$, km/h, localized scientific notation), by deriving allowed non-numeric input characters from formatToParts() and treating Unicode format/bidi control characters as ignorable during validation.
Changes:
- Added a shared
getFormatParts()helper and refactored locale detail derivation to use it. - Updated allowed-character derivation to include per-character symbols from
Intl.NumberFormat.formatToParts()(excludingcompact), enabling multi-character currency/unit/exponent decorations. - Updated input validation and parsing to tolerate Unicode format/bidi controls (
\p{Cf}), and added regression tests for affected formats/locales.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/react/src/number-field/utils/parse.ts | Adds \p{Cf} handling and introduces getFormatParts() to unify formatToParts() enumeration. |
| packages/react/src/number-field/root/NumberFieldRoot.tsx | Reworks getAllowedNonNumericKeys() to derive allowed characters from formatToParts() per character. |
| packages/react/src/number-field/input/NumberFieldInput.tsx | Allows \p{Cf} characters through per-character validation (since parsing strips them). |
| packages/react/src/number-field/root/NumberFieldRoot.test.tsx | Adds regression tests for multi-character currency/unit/exponent symbols and RTL scientific notation cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The fa-IR scientific assertion relied on ICU emitting a U+200E control character, which varies by engine/ICU version. Inject a known U+200E into an otherwise-valid value instead so the test exercises the control-char path deterministically.
Closes #5107
Problem
A
NumberFieldwhoseIntlformat produces a multi-character symbol could not be edited by typing — every keystroke was silently rejected, the text reverted, and the caret jumped to the end. The reported case is pt-BR currency (R$ 1.234,56), but the same flaw affected any multi-character or non-enumerated decoration:format{ style: 'currency', currency: 'BRL' }pt-BRR$ 1.234,56{ style: 'unit', unit: 'kilometer-per-hour' }en-US12 km/h{ notation: 'scientific' }en-US1.5E3{ notation: 'scientific' }fa-IR۱×۱۰^−۱{ style: 'currency', currency: 'USD' }en-US$1,234.56Cause
getAllowedNonNumericKeysadded the currency symbol to the allowed set as a whole string (keys.add(currency)→"R$"), but the input's change handler validates the typed text one character at a time (allowedNonNumericKeys.has(ch))."R"and"$"were never in the set, so the whole value failed validation and the edit was reverted. Single-character symbols like$happened to work because the set entry matched the single char; units and exponent separators were never added at all.Fix
Derive the allowed characters from
Intl.NumberFormat.formatToParts, per character. This covers multi-character and locale-specific symbols of every part type uniformly (currency, unit, exponent separator, literals) and replaced the previous per-symbolcurrency/literal/groupbranches.compactsuffixes (K/M) are intentionally excluded —parseNumbercan't reverse them (11K→11), so allowing them would produce silently-wrong values.Ignore Unicode format/bidi controls (
\p{Cf}) in the validator. RTL locales insert marks such asU+200Earound the exponent sign in scientific notation, andparseNumberalready strips them, so the validator must treat them as ignorable rather than discarding the keystroke.Also extracted the shared
11111.1sample (already used bygetNumberLocaleDetails) into agetFormatPartshelper so both derivations enumerate the same parts.Testing
Added tests for the currency (issue repro), unit, scientific-notation, and RTL-scientific (
fa-IR, with bidi controls) cases — each confirmed to fail before the fix. Existing input-masking tests still pass. FullNumberFieldsuite passes in jsdom and Chromium (the issue was browser-confirmed).