Number Field
NumberField is a field for entering a number, with buttons on each side that decrease and increase it.
Use a NumberField when the value is a number, such as a count of seats, an amount of money or a timeout. It shows the value between a decrease button and an increase button. Use a Slider when an approximate value in a range is enough, and a TextField when the value only looks like a number, such as a postcode.
It matches a TextField in height, border, focus ring and material, so the two line up in one form. Set min and max to keep the value in a range, and step to set how far each press or arrow key moves it. A button becomes disabled when the value reaches a limit, and both are disabled in a read-only field.
Pass format to show the value as a currency, a percentage or a unit. The field formats the number and reads typed text in the user's locale, so someone in Germany can type 1.234,5, and locale sets a different one. Put a unit in format rather than beside the field, so screen readers announce it as part of the value.
The value is a number, or null when the field is empty, which is different from zero. Use value and onValueChange to keep it in your own state, or defaultValue to let the field keep it. Set name, and a hidden input sends the number with the form.
Put it in a Field to give it a label, a description and an error message. Inside a Field it uses that field's size, and a size set on the NumberField itself still wins.
The arrow keys change the value by step, Alt with an arrow key uses smallStep, and Shift with an arrow key uses largeStep. The two buttons aren't in the tab order, because the keyboard already changes the value from the field itself. Set decrementLabel and incrementLabel to name the buttons in your app's language.
Topics
Every symbol this component exports, grouped by the job it does. Each one carries the props it declares; each one has an anchor of its own.
One number
NumberField
NumberField is a field for entering a number, with buttons on each side that decrease and increase it.
| Prop | Type | What it does |
|---|---|---|
allowOutOfRange? | boolean | undefined | Lets a typed value go outside min and max. |
backdrop? | boolean | Set backdrop when the field sits over other content, such as an image. |
className? | string | Your classes, appended to the ones this component resolves rather than replacing them. |
decrementLabel? | string | The accessible name of the decrease button. |
defaultValue? | number | undefined | The start value of an uncontrolled field. |
disabled? | boolean | undefined | Disables the field. |
form? | string | undefined | The id of the form that the field belongs to. |
format? | Intl.NumberFormatOptions | undefined | The Intl.NumberFormat options that format the value. |
id? | string | undefined | The id of the input. |
incrementLabel? | string | The accessible name of the increase button. |
largeStep? | number | undefined | The amount that Shift + an arrow key adds or removes. |
locale? | Intl.LocalesArgument | undefined | The locale that formats and parses the value. |
max? | number | undefined | The highest permitted value. |
min? | number | undefined | The lowest permitted value. |
name? | string | undefined | The name of the value in the submitted form. |
onValueChange? | | ((value: number | null, eventDetails: BaseNumberField.Root.ChangeEventDetails) => void) | undefined | Called on every change of the value. |
onValueCommitted? | | ((value: number | null, eventDetails: BaseNumberField.Root.CommitEventDetails) => void) | undefined | Called when the value is final: on blur after typing, or when a press is released. |
readOnly? | boolean | undefined | Prevents changes to the value. |
required? | boolean | undefined | Prevents form submission while the field is empty. |
scrub? | React.ReactNode | Turns the field into a creative tool's field: a short label at the start, such as X or an icon, that a person drags sideways to change the value. |
size? | "1" | "2" | "3" | "4" | The size step of the field, from "1" to "4". |
smallStep? | number | undefined | The amount that Alt + an arrow key adds or removes. |
snapOnStep? | boolean | undefined | Rounds the value to a multiple of step when it steps. |
step? | number | "any" | undefined | The amount that one button press or one arrow key adds or removes. |
style? | React.CSSProperties | Inline styles, merged last, so your value wins over anything the component resolved. |
value? | number | null | undefined | The value of a controlled field. |
It also takes every prop of a <input>.
Numbers that go together
NumberFieldGroup
Number fields that edit one value, such as X, Y and Z, joined edge to edge in equal columns under one accessible name
| Prop | Type | What it does |
|---|---|---|
label | string | The group's accessible name, such as Position. |
size? | "1" | "2" | "3" | "4" | The size step of every field in the group. |
It also takes every prop of a <div>.
leadingandtrailing— The two buttons already fill both ends of the field. Put a unit or currency informatinstead, so it is shown, announced and read back as part of the number.emphasisandtone— Loudness ranks actions, and no field in a form is more important than the one beside it. To show an unacceptable value, setaria-invalidor mark theFieldaround it as invalid.render— A NumberField is made of an outer box and the input inside it, and neither can be replaced without breaking the other, so there is no single element forrenderto swap.- The browser's number input — The native number input can't format or read numbers in the user's language, and it draws its own spinner buttons, so NumberField uses a text input with its own buttons.
- Dragging or scrolling to change the value — Changing the value by dragging or by scrolling needs its own pointer design, which doesn't exist yet. Use the buttons or the arrow keys for now.