Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Number Input for React Aria

A field for one number, with step buttons, limits it stays inside, and currency or percent formatting. For quantities and prices in React on React Aria.

On this page9 sections

A number input is a text field for one number, with buttons that step it up and down and limits it won't leave. Reach for it when the number has a minimum, a maximum or a natural step: a cart quantity, a price, a percentage.

A postal code or a card number is a string of digits, not a quantity, so it belongs in an Input. When people pick a value by feel rather than type an exact one, a Slider is quicker.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Number Input anatomy: label, scrubber, group, decrement, input, increment

Usage

import {
  NumberInput, NumberInputDecrement, NumberInputGroup,
  NumberInputIncrement, NumberInputInput,
} from "@/components/ui/number-input";
<NumberInput className="w-full max-w-48" defaultValue="1">
  <NumberInputGroup>
    <NumberInputDecrement />
    <NumberInputInput />
    <NumberInputIncrement />
  </NumberInputGroup>
</NumberInput>

Accessibility

The input is a text field that a screen reader announces as a number field, and the keys below step it. The step buttons are labeled and left out of the tab order. React Aria needs a name for the field: put the FieldLabel inside NumberInput, where it is connected, as With field does, or pass aria-label to NumberInput. A label beside it in a Field isn't connected.

Arrow Up
Raises the value by one step.
Arrow Down
Lowers the value by one step.
Page Up
Raises the value by one step, as ArrowUp does.
Page Down
Lowers the value by one step, as ArrowDown does.
Home
When min is set, sets the value to it.
End
When max is set, sets the value to it.
Enter
Commits the typed number, clamped to the range and rounded to step when one is set, and lets the form submit.

Demos

Controlled

The number lives in your own state, starting at 1, and the line under the field changes from "Not 3 yet." to "That's 3." once the value is 3. Control it when another part of the screen reads the number, such as a cart total or a price summary. Leave it uncontrolled when only the form needs it.

Preview width

Disabled

With disabled on NumberInput, the field and both buttons fade and stop responding, and the value is left out when the form submits. Use it for a number this person can't change right now, such as seats fixed by their plan, with a line nearby that says why. When the value must still reach the server, set readOnly instead.

Preview width

Field only

The group holds only NumberInputInput, starting at 0, with no step buttons. People type the number or step it with the arrow keys. Use it in a table cell or a dense toolbar, where two buttons per field would crowd the row. Letters still can't get in.

Preview width

Form

A Quantity field from 1 to 10 sits in a form under the name quantity, and Add to cart reads the form data the way a server would and prints what it got, such as quantity=3. The name goes on NumberInput. Use a plain form like this when the number goes out with other fields, such as a cart line.

Preview width

Formatted

formatOptions set to US dollars shows the starting value, 19, as $19.00, and every step keeps the currency format. Use it for prices and other amounts where the format is part of what people read. When you only want a symbol beside a plain number, the With input group demo puts one there instead.

Preview width

Invalid

invalid on NumberInput turns the border red around a value of 32 and tells screen readers the input is invalid. It doesn't stop typing or stepping, and the field can't say what is wrong, so pair it with a message such as "Only 20 left in stock". Use it for rules that min and max can't express.

Preview width

Mouse wheel

The field starts at 10 with allowMouseWheel set, so turning the wheel over it steps the value. That only happens while the input has focus and the pointer is over it, so scrolling the page past the field leaves it alone. Turn it on in an editor panel where the pointer already rests on the field.

Preview width

Range

min is 0, max is 10 and the field starts at 5. The buttons stop at either end, and a typed number past them is pulled back into range. Set both for a rating, a percentage or a small count with a fixed band, and only min when the one rule is no negatives.

Preview width

Scrub

The Quantity label above the field is a NumberInputScrubber: press on it and drag sideways, and the value follows, the way design tools let you nudge a size. The buttons and typing still work. Add it where people adjust numbers by eye, such as dimensions in an editor, and leave it off forms people fill in on a phone.

Preview width

Size lg

size set to lg raises the input and its buttons from 32 to 36 pixels. Use it when the number is the main control on the screen, such as the quantity on a product page, or when it sits in a row of other large inputs and buttons.

Preview width

Size md

md is the default size, written out here so you can compare it with the other two: the input and its buttons are 32 pixels tall, the height of the default Input. Leave size out in most forms and the field lines up with its neighbors.

Preview width

Size sm

size set to sm brings the input and its buttons down to 28 pixels, for a table row, a toolbar or a crowded side panel. On a touch screen each button keeps a 44-pixel tap area at every size, so the small one costs no reach. Keep a visible label, since a short box is easy to overlook.

Preview width

Step

Two fields: one moves by 5 from 0, the other by 0.1 from 0.1. The step sets how far one press of a button moves the value. Pick one people can predict from the label, whole units for stock counts and tenths for a measurement such as a weight in kilograms.

Preview width

With field

The input sits in a Field under the label Quantity, with the hint "0 - 100 only" below it and min and max set to match. Use a Field when the number needs a visible label, a hint and later an error message. The FieldLabel sits inside NumberInput, where React Aria connects it to the input.

Preview width

With input group

The number sits in an Input Group between a euro sign and the letters EUR, with no step buttons. Use it when the unit is decoration around a plain number and a currency format would change how the number reads. The aria-label on NumberInput names the input, since the symbols can't.

Preview width

FAQ

How do I control the Number Input value in React?

Hold the value in state, pass it as value and update it in onValueChange, which receives { value } with the number as a string, empty once the field is cleared. React Aria commits a typed number when the field loses focus or on Enter, so the callback fires then and on every step, not on each keystroke.

The Controlled demo holds the value this way and compares it with 3.

How do I set a minimum, maximum and step on a Number Input?

Set min, max and step on NumberInput. The buttons move by step and stop at either end, as the Range and Step demos show.

React Aria clamps a typed number into the range when it commits, on blur or Enter. When step is set it also rounds the number to the nearest step, so 7 becomes 5 with a step of 5.

How do I format a Number Input as currency or a percentage?

Pass formatOptions, which takes the options of Intl.NumberFormat. { style: "currency", currency: "USD" } shows 19 as $19.00, as in the Formatted demo. With { style: "percent" } the value is a fraction, so 0.25 shows as 25%.

With the percent style, React Aria steps by 0.01, one percent, unless you set step yourself.

How do I submit a Number Input value with a form?

Give NumberInput a name. React Aria renders a hidden input under that name holding the bare number, even when the field shows currency, and an empty field sends an empty string. A disabled field sends nothing, so use readOnly for a value that must be submitted but not changed.

The Form demo sends a quantity and shows what the form received.

Can a Number Input change value with the mouse wheel?

Yes, once you set allowMouseWheel on NumberInput, which turns on React Aria's wheel handling: the wheel steps the value while the input has focus and the pointer is over it, and the page doesn't scroll underneath. React Aria raises the value on a turn that would scroll the page down and lowers it on one that would scroll up. Without the prop, the wheel leaves the field alone.

How do I let people drag to change a Number Input value?

Put a NumberInputScrubber above the group with the label text inside it, as the Scrub demo does with Quantity. It renders the label and makes it a handle: press on it and drag sideways to move the value.

This build moves the value one step for every 8 pixels, keeps it inside min and max, and reports each step through onValueChange.

Is the React Aria Number Input accessible?

Yes, once it has a name. The arrow keys and Page Up and Page Down step the value, Home and End jump to min and max when they're set, and a screen reader announces a number field. Put the FieldLabel inside NumberInput, as With field does, or pass aria-label, because a label beside it in a Field isn't connected.

The Accessibility section lists each key.

When should I use a Number Input instead of an input with type number?

Use the Number Input when the number needs a currency or percent format, step buttons that look the same in every browser, or a min and max it is pulled back inside. A native number input draws the browser's own spinners, can't format the value, accepts the letter e for exponents, and only reports an out-of-range number through form validation.

For digits that aren't a quantity, such as a postal code or a one-time code, use an Input with inputMode set to numeric, or an Input OTP. Step buttons mean nothing on those.

API Reference

NumberInput

PropType
value?number | string

The value. Pair with onValueChange to control it.

Type
number | string
defaultValue?number | string

The value it starts with, when you don't control value.

Type
number | string
onValueChange?(details: { value: string }) → void

Called when the value changes.

Type
(details: { value: string }) → void
Parameters

details – { value: string }

Returns
void
allowMouseWheel?boolean

Whether to allow mouse wheel to change the value.

Default
false
disabled?boolean

Whether it's disabled.

formatOptions?Intl.NumberFormatOptions

The options to pass to the Intl.NumberFormat constructor.

id?string

The element's id.

invalid?boolean

Whether it's invalid.

Default
false
max?number

The highest value.

Default
Number.MAX_SAFE_INTEGER
min?number

The lowest value.

Default
Number.MIN_SAFE_INTEGER
name?string

The name it submits with its form.

readOnly?boolean

Whether the number input is readonly.

size?"sm" | "md" | "lg"

The size.

Type
"sm" | "md" | "lg"
Default
"md"
step?number

How much each step changes the value.

Default
1
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-focus?string

Present when focused is true.

data-invalid?string

Present when invalid is true.

data-scrubbing?string

Present when scrubbing is true.

NumberInputGroup

data-slot="number-field-group"

PropType
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
isDisabled?boolean

Whether the group is disabled.

isInvalid?boolean

Whether the group is invalid.

isReadOnly?boolean

Whether the group is read only.

role?"group" | "region" | "presentation"

An accessibility role for the group. By default, this is set to group. Use region when the contents of the group is important enough to be included in the page table of contents. Use presentation if the group is visual only and does not represent a semantic grouping of controls.

Type
"group" | "region" | "presentation"
Default
"group"
slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
Default
number-field-group
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-focus?string

Present when focused is true.

data-invalid?string

Present when invalid is true.

data-scrubbing?string

Present when scrubbing is true.

NumberInputDecrement

data-slot="number-field-decrement"

PropType
children?ReactNode

The content to display in the button.

clickEffect?boolean

Apply a click effect to the button.

Default
true
download?LinkPrimitiveProps["download"]

With href, downloads the linked file instead of opening it.

href?string

When set, the button renders as a link.

isDisabled?boolean

Whether the button is disabled.

isLoading?boolean

Show a loading indicator.

Default
false
pill?boolean

Fully rounded corners.

Default
false
rel?string

With href, the link's relationship to the page (noreferrer, …).

size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
target?string

With href, where the linked page opens (_blank, …).

variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

Ark default: default.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
ghost
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-focus?string

Present when focused is true.

data-invalid?string

Present when invalid is true.

data-scrubbing?string

Present when scrubbing is true.

NumberInputIncrement

data-slot="number-field-increment"

PropType
children?ReactNode

The content to display in the button.

clickEffect?boolean

Apply a click effect to the button.

Default
true
download?LinkPrimitiveProps["download"]

With href, downloads the linked file instead of opening it.

href?string

When set, the button renders as a link.

isDisabled?boolean

Whether the button is disabled.

isLoading?boolean

Show a loading indicator.

Default
false
pill?boolean

Fully rounded corners.

Default
false
rel?string

With href, the link's relationship to the page (noreferrer, …).

size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
target?string

With href, where the linked page opens (_blank, …).

variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

Ark default: default.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
ghost
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-focus?string

Present when focused is true.

data-invalid?string

Present when invalid is true.

data-scrubbing?string

Present when scrubbing is true.

NumberInputInput

data-slot="number-field-input"

PropType
children?ReactNode

The children of the component.

enterKeyHint?"enter" | "done" | "go" | "next" | "previous" | "search" | "send"

An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint).

Type
"enter" | "done" | "go" | "next" | "previous" | "search" | "send"
style?object

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element.

Type
{ prop?: string]: string | number }
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-focus?string

Present when focused is true.

data-invalid?string

Present when invalid is true.

data-scrubbing?string

Present when scrubbing is true.

NumberInputScrubber

<span> data-slot="number-field-scrubber"

PropType
className?string

Merged with the part's classes.