A number input with steppers, limits, keyboard stepping and locale formatting.
Number Field is a number input with decrement and increment buttons. It keeps the value within min and max, steps by step from the buttons and the keyboard, and formats the value for the reader's locale: currency, percent and units.
It's built on the Base UI Number Field and drawn by the Input Group, so its frame, focus, hover, invalid and disabled states are the input's. Number Field is part of the Fabricator style.
Installation#
bunx --bun fabricator-ui@latest add number-field
Usage#
import {
NumberField,
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
} from "@/components/ui/number-field"<NumberField defaultValue={1} min={0} max={10}>
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput aria-label="Quantity" />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>Composition#
Use the following composition to build a NumberField:
NumberField
└── NumberFieldGroup
├── NumberFieldDecrement
├── NumberFieldInput
└── NumberFieldIncrementEach stepper sits in its own Input Group addon. The decrement goes at the start and the increment at the end. For both at the end, put the input first and set align="inline-end" on the decrement:
<NumberFieldGroup>
<NumberFieldInput />
<NumberFieldDecrement align="inline-end" />
<NumberFieldIncrement />
</NumberFieldGroup>Examples#
Basic#
Without a visible label, name the input with aria-label.
Steppers at the end#
Min, max and step#
The buttons and the arrow keys stop at min and max, and a stepper turns disabled at its limit. A typed value is clamped when the field loses focus. step sets the amount; hold Shift for largeStep (10) and Alt for smallStep (0.1).
From 0 to 5, in steps of 0.5.
Formatting#
format takes Intl.NumberFormat options. The value stays a plain number: 0.15 shows as 15% with style: "percent". locale sets the locale; it defaults to the reader's.
Field#
Compose it with Field, FieldLabel and FieldDescription. The id on NumberField goes to the input, so htmlFor on the label points at it.
Billing follows the seats in use. Up to 50 on the Team plan.
Disabled and read-only#
disabled dims the field and turns it and its steppers off. readOnly keeps the value focusable and selectable, but it can't change.
Invalid#
Set aria-invalid on NumberFieldInput and data-invalid on Field.
Sizes#
The field follows the density. Set density-compact or density-spacious on it, or on any parent.
Controlled#
Control the value with value and onValueChange. The value is null when the input is empty.
Total $48.00
API Reference#
NumberField, NumberFieldGroup and NumberFieldInput take every prop of their Base UI part. See the Base UI Number Field documentation.
NumberField#
| Prop | Type | Default |
|---|---|---|
value | number | null | - |
defaultValue | number | - |
onValueChange | (value: number | null, details) => void | - |
min | number | - |
max | number | - |
step | number | "any" | 1 |
smallStep | number | 0.1 |
largeStep | number | 10 |
format | Intl.NumberFormatOptions | - |
locale | Intl.LocalesArgument | - |
disabled | boolean | false |
readOnly | boolean | false |
required | boolean | false |
name | string | - |
id | string | - |
NumberFieldDecrement and NumberFieldIncrement#
Every Base UI stepper prop, plus:
| Prop | Type | Default |
|---|---|---|
align | "inline-start" | "inline-end" | "inline-start" (decrement), "inline-end" (increment) |
children | React.ReactNode | a minus or plus icon |
Data attributes#
| Attribute | Element |
|---|---|
data-slot="number-field" | root |
data-slot="number-field-group" | the frame |
data-slot="number-field-input" | the <input> |
data-slot="number-field-decrement" | the decrement button |
data-slot="number-field-increment" | the increment button |
Base UI also sets data-disabled, data-readonly, data-invalid and data-scrubbing on its parts.
Accessibility#
- The input is a text input described as a "Number field". ArrowUp and ArrowDown step by
step, Shift and Alt step bylargeStepandsmallStep, and Home and End jump tominandmaxwhen they're set. - The steppers are named "Decrease" and "Increase" and stay out of the tab order, so Tab moves from the field to the next control. Pass
aria-labelto name them for your field. - Give the field a name: a
FieldLabelwithhtmlFor, oraria-labelon the input. - Typing accepts the locale's digits, separators, currency signs and percent signs. On phones the input opens a numeric keyboard, or the decimal one when the value can have decimals.
- Focus lifts the frame to the card surface with the neutral focus ring. Invalid turns the ring red.