base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

[all components][i18n] Export formatNumber utilities

Open brianle1301 opened this issue 1 year ago • 5 comments

Feature request

Summary

formatNumber utilities should be exported. Whilst the user can write their own format function, exporting these utilities ensure that number formatting works exactly like NumberInput and users benefit from some niceties such as formatter caching.

Examples in other libraries

N/A

Motivation

This is useful for when users need to display number input values in a separate "review" section.

brianle1301 avatar Jul 09 '25 01:07 brianle1301

@brianle1301 do you have a (proper) code example where they'd be useful - are parseNumber + formatNumber the main ones, or is only formatNumber needed?

Do you mean something like:

const [value, setValue] = useState(0);
return <NumberField.Root value={value} onValueChange={setValue} />;
{/* For displaying the state elsewhere */}
<div>
  {NumberField.formatNumber(value)}
</div>

NumberField.parseNumber/NumberField.formatNumber could possibly work. Toast has Toast.createToastManager + Toast.useToastManager, but they're not utils, just core APIs.

atomiks avatar Jul 14 '25 09:07 atomiks

@atomiks Your snippet captures my use case perfectly. I don't know when parseNumber would be useful given that NumberInput accepts and passes real numbers in handlers, but formatNumber is a good addition exactly for the use case you stated

brianle1301 avatar Jul 14 '25 10:07 brianle1301

I can work on this one

Raghuboi avatar Aug 17 '25 15:08 Raghuboi

NumberField.parseNumber/NumberField.formatNumber

It feels like this should be solved the other way around:

  1. Base UI exposes a number formatter that can be used across all components (e.g., I have recently seen https://github.com/mui/mui-x/issues/13222#issuecomment-3184712326), https://github.com/mui/material-ui/issues/24495.
  2. NumberInput has a default one (1.)
  3. NumberInput accepts any custom number formatter

oliviertassinari avatar Aug 17 '25 18:08 oliviertassinari

formatNumber (and several other utilities) are being moved to the @base-ui/utils package in https://github.com/mui/base-ui/pull/5167.

michaldudak avatar Aug 06 '26 07:08 michaldudak