Skip to content

Commit d8cc45e

Browse files
committed
docs: number input
1 parent 81879cb commit d8cc45e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

website/src/content/pages/components/number-input.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ smooth dragging interactions.
9999
> **Note:** Browsers may show a notification when the Pointer Lock API is activated. The scrubber is automatically
100100
> disabled in Safari to prevent layout shifts.
101101
102+
### Controlling the value
103+
104+
When controlling the NumberInput component, it's recommended to use string values instead of converting to numbers. This
105+
is especially important when using `formatOptions` for currency or locale-specific formatting.
106+
107+
```tsx
108+
const [value, setValue] = useState('0')
109+
110+
<NumberInput.Root value={value} onValueChange={(details) => setValue(details.value)}>
111+
{/* ... */}
112+
</NumberInput.Root>
113+
```
114+
115+
Converting values to numbers can cause issues with locale-specific formatting, particularly for currencies that use
116+
different decimal and thousands separators (e.g., `1.523,30` vs `1,523.30`). By keeping values as strings, you preserve
117+
the correct formatting and avoid parsing issues.
118+
119+
If you need to submit a numeric value in your form, use a hidden input that reads `valueAsNumber` from
120+
`NumberInput.Context`:
121+
122+
```tsx
123+
<NumberInput.Root value={value} onValueChange={(details) => setValue(details.value)}>
124+
<NumberInput.Input />
125+
<NumberInput.Context>
126+
{(context) => <input type="hidden" name="amount" value={context.valueAsNumber} />}
127+
</NumberInput.Context>
128+
</NumberInput.Root>
129+
```
130+
102131
## API Reference
103132

104133
<ComponentTypes id="number-input" />

0 commit comments

Comments
 (0)