[all components][i18n] Export formatNumber utilities
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 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 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
I can work on this one
NumberField.parseNumber/NumberField.formatNumber
It feels like this should be solved the other way around:
- 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.
- NumberInput has a default one (1.)
- NumberInput accepts any custom number formatter
formatNumber (and several other utilities) are being moved to the @base-ui/utils package in https://github.com/mui/base-ui/pull/5167.