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 minValue and maxValue, 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 React Aria NumberField 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 { FieldLabel } from "@/components/ui/field"
import {
NumberField,
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
} from "@/components/ui/number-field"<NumberField defaultValue={1} minValue={0} maxValue={10}>
<FieldLabel>Quantity</FieldLabel>
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>Composition#
Use the following composition to build a NumberField:
NumberField
├── FieldLabel
└── 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 field with aria-label on NumberField.
Steppers at the end#
Min, max and step#
The buttons and the arrow keys stop at minValue and maxValue, and a stepper turns disabled at its limit. A typed value is clamped and snapped to the step when the field loses focus. step sets the amount.
From 0 to 5, in steps of 0.5.
Formatting#
formatOptions takes Intl.NumberFormat options. The value stays a plain number: 0.15 shows as 15% with style: "percent". The locale comes from React Aria's I18nProvider, and defaults to the reader's.
Field#
Put FieldLabel inside NumberField: React Aria wires the label to the input. Point aria-describedby on NumberField at the FieldDescription.
Billing follows the seats in use. Up to 50 on the Team plan.
Disabled and read-only#
isDisabled dims the field and turns it and its steppers off. isReadOnly keeps the value focusable and selectable, but it can't change.
Invalid#
Set isInvalid on NumberField 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 onChange. The value is NaN when the input is empty.
Total $48.00
API Reference#
NumberField, NumberFieldGroup and NumberFieldInput take every prop of their React Aria part. See the React Aria NumberField documentation.
NumberField#
| Prop | Type | Default |
|---|---|---|
value | number | - |
defaultValue | number | - |
onChange | (value: number) => void | - |
minValue | number | - |
maxValue | number | - |
step | number | 1 |
formatOptions | Intl.NumberFormatOptions | - |
isDisabled | boolean | false |
isReadOnly | boolean | false |
isRequired | boolean | false |
isInvalid | boolean | false |
name | string | - |
NumberFieldDecrement and NumberFieldIncrement#
Every Button prop except slot, 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 |
React Aria also sets data-disabled, data-readonly, data-invalid and data-required on the root.
Accessibility#
- React Aria's NumberField: the input is a text input described as a "Number field". ArrowUp and ArrowDown step by
step, PageUp and PageDown step by a larger amount, and Home and End jump tominValueandmaxValuewhen they're set. - The steppers stay out of the tab order, so Tab moves from the field to the next control. React Aria names them "Increase" and "Decrease" with the field's label, in the reader's language.
- Give the field a name: a
FieldLabelinsideNumberField, oraria-labelon it. - 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.