feat(numberinput): add type='text' and locale-based formatting#19471
Conversation
…mberinput-exploration
…mberinput-exploration
…mberinput-exploration
| /** | ||
| * The currently parsed number value. | ||
| * Only used when type=text | ||
| * Updated based on the `value` as the user types. | ||
| */ | ||
| const [numberValue, setNumberValue, isControlled] = useControllableState({ |
There was a problem hiding this comment.
Adding new state variables instead of using the existing ones is intentional. I tried to be really clear and separate out the type='number' and type='text' code paths from one another to avoid regressions.
This comment was marked as off-topic.
This comment was marked as off-topic.
| if (type === 'number') { | ||
| const state = { | ||
| value: | ||
| allowEmpty && event.target.value === '' | ||
| ? '' | ||
| : Number(event.target.value), | ||
| direction: value < event.target.value ? 'up' : 'down', | ||
| }; | ||
| setValue(state.value); | ||
|
|
||
| if (onChange) { | ||
| onChange(event, state); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (type === 'text') { | ||
| const _value = | ||
| allowEmpty && event.target.value === '' ? '' : event.target.value; | ||
| setNumberValue(numberParser.parse(_value)); | ||
| setInputValue(_value); | ||
| // The onChange prop isn't called here because it will be called on blur | ||
| // or on click of a stepper, after the number is parsed and formatted | ||
| // according to the locale. |
There was a problem hiding this comment.
Same thing here - I know this is verbose and a bit duplicative but trying to clearly separate these code paths to avoid confusion and potential regressions.
| type="number" | ||
| value={value} | ||
| type={type} | ||
| value={type === 'number' ? value : inputValue} |
There was a problem hiding this comment.
This is the crux of how the code paths are separated.
| export * from '@internationalized/number'; | ||
| export type * from '@internationalized/number'; |
There was a problem hiding this comment.
Intentionally avoiding a peerDependency by doing this, to avoid issues like we've had with react-is.
✅ Deploy Preview for v11-carbon-react ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #19471 +/- ##
==========================================
+ Coverage 84.87% 84.91% +0.04%
==========================================
Files 372 373 +1
Lines 14438 14504 +66
Branches 4701 4742 +41
==========================================
+ Hits 12254 12316 +62
- Misses 2038 2041 +3
- Partials 146 147 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
kennylam
left a comment
There was a problem hiding this comment.
Looks like the styling is a bit off in the following stories:
|
With |
tay1orjones
left a comment
There was a problem hiding this comment.
I updated a couple FluidNumberInput selectors that were causing that visual regression.
Ready for a re-review 👍
| withAILabel.argTypes = { ...sharedArgTypes }; | ||
|
|
||
| export const WithTypeOfText = (args) => { | ||
| const locale = useDocumentLang(); |
There was a problem hiding this comment.
@kennylam I forgot we needed to track changes to lang instead of just reading it once. I had so far been testing by changing the locale and then refreshing the page. The component now rerenders when the locale changes though.
2025-05-23.at.12.42.18-NUMBERINPUT-Google.Chrome.mp4
This new hook is powered by some new documentLang utilities that we can test out here in storybook for now. Eventually we should be able to reuse them when we implement this in web-components. It could also be the beginning of how we could add locale/lang detection to NumberInput so the consumer wouldn't have to pass a locale at all.
781e9d0
…bon-design-system#19471) * fix(numberinput): add type of text, updated styles, example localization * wip * wip * feat(numberinput): add type='text' and locale-based formatting * chore: remove test stories * chore: yarn dedupe * docs(numberinput): update type prop comments * fix(numberinput): make inputMode optional * docs(numberinput): clean up stories * fix(numberinput): revert min/max on story for avt tests * chore: min/max arg types for default story * fix(fluidnumberinput): remove duplicate style selector, correct invalid selector * feat(utilities): add documentLang utilities to track html lang updates
…bon-design-system#19471) * fix(numberinput): add type of text, updated styles, example localization * wip * wip * feat(numberinput): add type='text' and locale-based formatting * chore: remove test stories * chore: yarn dedupe * docs(numberinput): update type prop comments * fix(numberinput): make inputMode optional * docs(numberinput): clean up stories * fix(numberinput): revert min/max on story for avt tests * chore: min/max arg types for default story * fix(fluidnumberinput): remove duplicate style selector, correct invalid selector * feat(utilities): add documentLang utilities to track html lang updates
Closes #18763
Closes #7819
Closes #18755
Changelog
New
@internationalized/numbertype,locale,formatOptions,pattern,inputModeGermanlocale option to the switcher in the storybook toolbardocumentLangutilities for tracking updates todocumentElement.langChanged
Testing / Reviewing
NumberInput / With Type of Textstory,foren-US,.forde-DE, to input such as5000.foren-US,,forde-DEsuch as5000.55/5000,555000.5599999999should be corrected to5000.56input[type='text']minormaxthe invalid styling should all come throughOverviewpage updatesPR Checklist
As the author of this PR, before marking ready for review, confirm you:
More details can be found in the pull request guide