Skip to content

fix(NumberInput): parse pasted grouped and localized numbers - #5450

Merged
cixzhang merged 3 commits into
mainfrom
numberinput-format
Aug 25, 2026
Merged

cixzhang merged 3 commits into
mainfrom
numberinput-format

Conversation

@cixzhang

@cixzhang cixzhang commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

What

A pasted 1,234,234,234 now commits as 1234234234.

parseNumberInput was Number(trimmed). Number("1,234,234,234") is NaN, so
the field went aria-invalid and the paste was lost — the most common way
anyone puts a large number into a form. This is the default path: no props
beyond the required ones, no formatValue.

No API change. formatValue is untouched and does not appear in the diff.

The parser

Typed and pasted text is read under the field's locale, from useLocale().
Nothing is hardcoded: the grouping and decimal separators and the group sizes
come from Intl.NumberFormat(locale).formatToParts, so 1.234.234.234 in
de-DE, narrow-no-break-space grouping in fr-FR and lakh grouping in en-IN all
work without a table. Digit values are derived from \p{Nd} by walking back to
each script's zero, so Arabic-Indic, Devanagari and full-width digits parse
without a table either.

Two readings, in order:

  1. The locale's own. 1,5 is 1.5 in de-DE.
  2. A repeated separator with three digits after each occurrence. That can
    only be grouping — a decimal point appears once — so 1,234,234,234 reads
    the same in every locale, which is what makes the spreadsheet paste land in
    a de-DE app.

Anything that could mean two different numbers returns null and the field
stays visibly invalid with its existing role="alert" announcement. 1,5 in
en-US is nothing. Typing is never intercepted: no beforeinput guard, no
keystroke rejection, no rewriting mid-composition. The value commits on blur.

One behaviour change outside the parser: while focused, the raw value renders
with the locale's decimal separator (de-DE shows 1,5, not 1.5), so what the
field renders is what the field can read back. In en-US this is identical to
today.

The corpus

numberParser.test.ts is a single table — input, locale, expected — and it is
the specification. 72 rows: grouping shapes, lakh grouping, ragged grouping,
signs on either end, whitespace and newlines, five digit scripts, currency,
accounting negatives, typographic minus signs, exponents, BOM and zero-width
characters, and junk. null rows are assertions, not fallthrough.

Two calls worth flagging:

  • Currency symbols are stripped ($1,234.56 → 1234.56). A \p{Sc} symbol
    carries no magnitude, so dropping it cannot produce a wrong number — the
    separator rules still decide, and 1.234,56 € in an en-US field still
    refuses.
  • Unit suffixes are not (1,234 GB → null). A trailing letter can
    carry magnitude (k, B), so guessing which letters are inert is exactly
    how a wrong number gets committed.

Testing

  • 72-row corpus, all passing; 122 existing NumberInput tests unchanged.
  • A real Chromium paste against local Storybook for a representative dozen —
    jsdom does not fire insertFromPaste. Screenshots below.
  • tsc --noEmit ✓, ESLint ✓, check:repo ✓, docsite generate ✓.

numberParser.ts is on the no-raw-intl-locale approved-implementation list
(rule source + README + a focused rule test), which is the only sanctioned way
to reach Intl directly.

Verified in real Chromium

A representative dozen, pasted with a real clipboard write + Meta+V against
local Storybook — NumberInput › Paste Formatted Number, also live at the PR's
Storybook preview.
"Shown while focused" is the field's own value mid-paste: the text is never
rewritten.

Field Pasted Shown while focused Committed
en-US 1,234,234,234 1,234,234,234 1234234234
en-US $1,234.56 $1,234.56 1234.56
en-US (1,234) (1,234) -1234
en-US 1<NBSP>234 1 234 1234
en-US ١,٢٣٤ ١,٢٣٤ 1234
en-US 1.23E+09 1.23E+09 1230000000
en-US 1,234 GB 1,234 GB null (aria-invalid, "Invalid number")
en-US 1,5 1,5 null (aria-invalid, "Invalid number")
de-DE 1.234.234.234 1.234.234.234 1234234234
de-DE 1,5 1,5 1.5
de-DE 1.234,56 1.234,56 1234.56
de-DE 1,234,234,234 1,234,234,234 1234234234

The de-DE field in that story carries an ordinary formatValue, so the full
loop is visible: paste 1,234,234,234 → field holds the pasted text while
focused → blur → commits 1234234234 → re-renders 1.234.234.234.

Docsite: pnpm -F @astryxdesign/docsite generate then the rendered
/components/NumberInput page — the new line is the first Best-practices row.
docsZh and docsDense were checked through the doc loader, not assumed.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 24, 2026
@vercel

vercel Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 25, 2026 12:05am

Request Review

@cixzhang
cixzhang marked this pull request as ready for review August 24, 2026 23:43
@github-actions

github-actions Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

NumberInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1020 -
Complexity N/A Very High (164) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 25, 2026
@cixzhang
cixzhang merged commit 32a62fd into main Aug 25, 2026
20 checks passed
@cixzhang
cixzhang deleted the numberinput-format branch August 25, 2026 00:26
cixzhang added a commit that referenced this pull request Aug 25, 2026
…ecimal point (#5459)

#5450 decided which characters may separate digits from the input itself:
unbounded for grouping, locale-only for the decimal point. Both halves are
wrong, and they are the same decision.

- A full stop the locale cannot read as grouping now reads as the decimal
  point it can only be, so de-DE 1.5 commits 1.5 again instead of 1. It is
  tried after the locale reading, not before it like dateParser.ts's ISO
  branch, because 1.234 in de-DE genuinely has two readings and grouping is
  the one a German means.
- Only the characters some locale writes between digits are separators, so
  123-456-789 is not 123456789.
- An explicit sign inside accounting parens refuses instead of flipping.

Every defect gets a corpus row, formatEditableNumber gets its first tests.
cixzhang pushed a commit that referenced this pull request Oct 21, 2026
<!-- template: bug-fix.md -->

## User impact

An app that builds Astryx from source with the documented Next.js setup, but sets no browserslist or targets a browser without Unicode property escapes (such as Safari 11), fails `next build --webpack` with `Unknown property: L` (also `Zs`, `Letter`). The errors point at `Avatar.tsx`, `Markdown/parser.ts` and `Markdown/plugins/headingLinks.ts`, package source the app cannot change, and come from both the PostCSS pass and the JavaScript compile. Any import from the package root reaches them.

## Problem and solution fit

- Why it fails: with no targets, or old ones, `next/babel`'s preset-env rewrites every `\p{…}` regex literal with transform-unicode-property-regex. Next's bundled copy of that transform loads `regenerate-unicode-properties` at runtime, which Next does not ship, so it throws (Next 16.3.6 and 16.4.2 alike).
- Why it harms the task: the throw stops the whole build.
- Why that matters: the app cannot fix it in its own code, and the error names a Unicode property, not the missing target.

## Expected behavior and authority

This is a regression of a settled case. #5450 hit this failure in `NumberInput/numberParser.ts` and builds those expressions with `new RegExp(...)`; its comment records why, and notes that property escapes need no transform in any browser this package supports. #6698, #6911 and #7051 later added four new literals and reintroduced it.

## Smallest restoration

- The four literals become `new RegExp(...)` with the same pattern and flags, built once at module load as before.
- Each site's comment names the cause and when a literal is safe again: once Next's bundled Babel ships the Unicode data.
- A source-build test fails when a Core module writes a property escape as a regex literal.

## Evidence

- Reproduction before the change: a clean registry install of main's documented Next.js example (`apps/example-nextjs-source`, hoisted, browserslist removed). `next build --webpack` fails in the PostCSS pass (`Avatar.tsx: Unknown property: L`) and in the JavaScript compile (`Unknown property: L`, `Zs`, `Letter` from the three files).
- Result after the change: the same install, with this change applied to Core's source, compiles and prerenders, and all 226 rendered class names have a rule in the emitted CSS.
- Representative unchanged path: the Avatar and Markdown suites pass unchanged.
- Regression test or other mutation-sensitive proof: `unicodePropertyEscapes.source-build.test.mjs` fails on main listing the four literals, and passes here.

## Scope

- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

- `pnpm exec vitest run --project ui packages/core/src/Avatar packages/core/src/Markdown packages/core/src/unicodePropertyEscapes.source-build.test.mjs`: 50 files, 1,158 tests pass (the new test alone fails on main).
- `next build --webpack` on the clean install, no browserslist: fails on main in both passes; passes with this change (226 of 226 rendered classes have a rule).
- `pnpm -F @astryxdesign/core typecheck`, ESLint and Prettier on the touched files, `pnpm check:repo`, changeset coverage: pass.

This branch was successfully deployed

1 active deployment
Preview — 78b7d811 Deployed Aug 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant