Number field
guests
npx shadcn add @klarhimmel/number-field @klarhimmel/inputCopy for AIExamples
Basic
Decimal
States
DetailsAbout, usage, props and more
A number you can type or step, with minus and plus buttons: the WAI-ARIA spin button.
When to use
Use for
A quantity people may know or want to nudge: guests, nights, quantity.
Not for
A rough value on a range. Use Slider. A choice among named options. Use Select.
Pairs with
Field, a unit, a status line.
Preview
Up and Down add one, Page Up and Page Down add ten.
2 guests
Import
Import
- Files
- number-field.tsx, input.tsx, in components/klarhimmel/ui/. With it: button, editing, input-group, theme, utils. npm: lucide-react.
- Render in
- Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | - | Controlled or uncontrolled. null means empty. |
defaultValue | number | null | null | Controlled or uncontrolled. null means empty. |
onValueChange | (value: number | null) => void | - | Controlled or uncontrolled. null means empty. |
min | number | - | Limits for the keys, the buttons and typed text. |
max | number | - | Limits for the keys, the buttons and typed text. |
step | number | 1 | What the arrows and the buttons add. Default 1. Its decimals set how many the field shows (0.25 shows two). |
largeStep | number | - | What Page Up and Page Down add. Default ten steps. |
unit | string | - | A unit after the number ("kg", "nights"). Said as part of the value to assistive tech. |
name | string | - | Submits the number with a form (a hidden input). |
id | string | - | |
disabled | boolean | - | Error styling; switches it off. |
invalid | boolean | - | Error styling; switches it off. |
aria-label | string | - | Needed when no Field or <label htmlFor> names it. |
aria-labelledby | string | - | Needed when no Field or <label htmlFor> names it. |
aria-describedby | string | - | |
decreaseLabel | string | 'Decrease' | The two buttons' names; pass them translated. |
increaseLabel | string | 'Increase' | The two buttons' names, and the language numbers are written in. Pass them translated. |
locale | string | 'en' | The language numbers are written in (decimal comma or point). |
className | string | - | Extra classes, merged after the component's own. |
Accessibility
| Key | Does |
|---|---|
Up / Down | Add or subtract the step; from empty, Up goes to the minimum |
Page Up / Page Down | Add or subtract ten steps (or largeStep) |
Home / End | Jump to the minimum or the maximum, when there is one |
Enter / leaving the field | Reads what was typed, inside the limits; a non-number is put back |
The WAI-ARIA spin button. The buttons are for the pointer and are not tab stops: the keyboard has the arrow keys. A decimal comma is accepted.
Accessibility