Number Field
A numeric input with increment and decrement buttons and a draggable label.
Installation
Install the following dependencies:
Follow Installation, including states.ts, state-styles.ts, field-state.ts, and interop.css for icons. Install Input and Input Group for the frame and stepper buttons. Use Field for labels and validation.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
Put name, value, min, max, and step on NumberField. Use FieldLabel or aria-label to name the input.
Bounds and steps
Use min, max, and step to constrain adjustments.
Between 0 and 10, in steps of 2.
Formatting
Pass locale and format to display a formatted number.
Controlled
Use value and onValueChange to control the number; an empty input produces null.
5 guests
Disabled and invalid
Set disabled or invalid on Field, or use readOnly on NumberField to keep the value fixed.
Validation
Use required and allowOutOfRange with Form to show native validation errors for empty or out-of-range values.
API reference
Parts accept Base UI Number Field props, including render. Styled parts accept xstyle, className, and inline style.
NumberFieldIncrement and NumberFieldDecrement
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "secondary" | "ghost" | "destructive" | "link" | "ghost" |
size | "xs" | "sm" | "icon-xs" | "icon-sm" | "icon-xs" |
children | ReactNode | Plus or minus icon |
aria-label | string | "Increase" or "Decrease" |
NumberFieldScrubAreaCursor
| Prop | Type | Default |
|---|---|---|
children | ReactNode | Horizontal arrows icon |
NumberFieldToolbarInput
Use this input inside a Toolbar to join its arrow navigation. It accepts NumberFieldInput props plus focusableWhenDisabled (default true).
numberFieldStyles
| Key | What it is |
|---|---|
root | Label and control layout |
group | InputGroup padding and no wrapping |
input | Centered tabular numbers |
inputBase, inputInGroup | Shared input styles |
undim | Clears dimming already applied by the group |
scrubArea, verticalScrub, inertScrub | Drag area cursor states |
cursor | Custom scrub cursor icon size |
Data attributes
| Attribute | On | Effect |
|---|---|---|
data-slot="number-field" | root | Field anatomy |
data-slot="number-field-group" | group | Shared frame |
data-slot="number-field-input" | input outside a group | Input anatomy |
data-slot="input-group-control" | input inside a group | Shared focus ring |
data-slot="number-field-increment" | increment | Stepper anatomy |
data-slot="number-field-decrement" | decrement | Stepper anatomy |
data-slot="number-field-scrub-area" | scrub area | Drag target |
data-slot="number-field-scrub-area-cursor" | scrub cursor | Cursor anatomy |
data-disabled, data-readonly, data-required, data-scrubbing | parts | Number field state |
data-valid, data-invalid, data-touched, data-dirty, data-filled, data-focused | parts | Field state |