- Label
- Icon label
- Field
- Form
- Frontmatter form
- Questionnaire
- Input
- Input group
- Input OTP
- Password input
- Password strength
- Code input
- Number field
- Tag input
- Textarea
- Checkbox
- Radio group
- Language toggle group
- Emoji appearance toggle group
- Emoji picker
- Avatar picker
- AI provider picker
- Select
- Native select
- Combobox
- Language combobox
- Slider
- Calendar
- Switch
- Alert
- Avatar
- Badge
- Card
- AI provider card
- Model info card
- Model list
- Empty
- Item
- Tree item
- File tree
- Icon media
- File type icon
- Font preview
- Image preview
- Kbd
- Table
- Data table
- Data table column header
- Chart
- Code Block
- Code card
- Markdown view
- Attachment
- Bubble
- Marker
- Message
- Chat message
- Chat suggestion item
- Message scroller
- Trace
- Progress
- Spinner
- Sonner
- Toast
- Unsaved changes toolbar
- Unsaved indicator
- Status indicator
- Permission card
- Separator
- Skeleton
- Animated grid pattern
- Meteors
A compact numeric field for a property inspector that works out arithmetic, clamps to min and max, and commits on return or on leaving the field.
Installation#
pnpm dlx shadcn@latest add @zui/number-fieldThe CLI writes components/data-entry/number-field.tsx, installs
Input group and Utils with the items they need, and writes the
expression evaluator to lib/expr-eval.ts.
Usage#
import { NumberField, NumberFieldInput } from '@/components/data-entry/number-field';
import { InputGroupAddon, InputGroupText } from '@/components/ui/input-group';<NumberField value={width} onValueChange={setWidth} min={0} max={1000} step={1}>
<InputGroupAddon>
<InputGroupText>W</InputGroupText>
</InputGroupAddon>
<NumberFieldInput aria-label="Width" />
<InputGroupAddon align="inline-end">
<InputGroupText>px</InputGroupText>
</InputGroupAddon>
</NumberField>Compose a NumberFieldInput and any InputGroupAddon around it, such as a leading label or a trailing unit, as the
group's direct children. Use it for a compact numeric field in a property inspector, such as a width or a corner
radius; a reader who types 100 + 8 commits 108.
Composition#
NumberField
`-- NumberFieldInputMixed values#
<NumberField value={widths[0]} onValueChange={(value) => setWidths(widths.map(() => value))} mixed={mixed}>
<NumberFieldInput aria-label="Width" placeholder="Mixed" />
</NumberField>Set mixed when the selection holds differing values: the field blanks to its placeholder until the reader types
one, and your own onValueChange applies it to every selected target.
Steps on a touch keyboard#
The arrows step the value only where the keyboard reports them, a hardware keyboard or the web; a phone's keyboard has no arrow keys. Where a screen needs steps on a phone, compose buttons in an addon:
import { Minus, Plus } from 'lucide-react-native';
import { Icon } from '@/components/ui/icon';
import { InputGroupAddon, InputGroupButton } from '@/components/ui/input-group';<NumberField value={width} onValueChange={setWidth} min={0} max={1000}>
<NumberFieldInput aria-label="Width" />
<InputGroupAddon align="inline-end">
<InputGroupButton size="icon-xs" aria-label="Decrease width" onPress={() => setWidth(Math.max(0, width - 1))}>
<Icon as={Minus} />
</InputGroupButton>
<InputGroupButton size="icon-xs" aria-label="Increase width" onPress={() => setWidth(Math.min(1000, width + 1))}>
<Icon as={Plus} />
</InputGroupButton>
</InputGroupAddon>
</NumberField>API reference#
NumberField#
Takes every prop of InputGroup except defaultValue, plus:
| Prop | Type | Default |
|---|---|---|
value | number | required |
onValueChange | (value: number) => void | required |
disabled | boolean | - |
min | number | - |
max | number | - |
step | number | - |
parseRaw | (raw: string) => number | null | - |
mixed | boolean | - |
displayText | string | - |
Parses the draft with parseRaw, or as arithmetic by default, clamps the result to min and max, and commits it
on the return key or when the field is left; a draft that is not a number commits nothing. Escape drops the draft
and commits nothing, and ArrowUp and ArrowDown move the value by step, clamped the same way, where the keyboard
reports those keys. displayText shows a label, such as "Hug" or "Fill", in place of the number until the field
is focused. disabled makes the input not editable, announced as disabled.
NumberFieldInput#
Takes every prop of InputGroupInput except value, defaultValue
and editable, which the root sets. Name it with aria-label.
Shows the field's value, or its draft while focused; its placeholder shows only while the field is mixed. It
opens iOS's numbers-and-punctuation keyboard, since the decimal pad has no minus sign and no operators. Your own
onFocus, onChangeText, onBlur, onSubmitEditing and onKeyPress run before the field's; an onKeyPress
that calls preventDefault keeps Escape and the arrows from reaching it.
Differences from the web#
- On a phone, Escape and the arrows have no key to come from, so leaving the field always commits and
stephas no trigger; compose step buttons as above where a screen needs them. onChangeText,onSubmitEditingandonKeyPressstand in for the web'sonChangeandonKeyDown, andeditablefordisabled; the web'stypehas no counterpart.- Native InputGroup draws its own focus ring, so the web's
cn-number-fieldring is not drawn. - The web's
data-slot,data-mixed,data-editinganddata-disabledattributes are not set; a View takes no data attributes.