Molecules · Quantity stepper
Quantity stepper
A whole number between a lowest and a highest value, with a button on each side. It follows the spinbutton pattern (the standard recipe for a number you nudge up and down), so the keyboard can do everything from inside the field.
- Best for
- A whole number between real limits, such as a quantity in a cart.
- Not for
- A value that isn't a whole number: use Input. A range of hundreds: use Slider or Input.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
Default
Code
tsx
<QuantityStepper label="Speed loader quantity" defaultValue={1} />Bounded: with a lowest and a highest allowed number
Only four left
Code
tsx
<div className="flex flex-col gap-2"> <QuantityStepper label="Bump helmet quantity" defaultValue={1} min={1} max={4} /> <span className="font-mono text-xs text-[var(--color-text-secondary)]">Only four left</span></div>Stepping by five
Code
tsx
<QuantityStepper label="BB bottle quantity" defaultValue={5} min={0} max={50} step={5} />In a cart line
Speed loader
€76
Code
tsx
function CartLine() { const price = 38 const [qty, setQty] = useState(2) return ( <div className="flex items-center gap-4 w-full"> <span className="flex-1 font-body text-sm text-[var(--color-text-primary)]">Speed loader</span> <QuantityStepper label="Speed loader quantity" defaultValue={2} min={1} max={9} onValueChange={setQty} /> <span className="w-16 text-right font-display font-semibold tabular-nums text-[var(--color-text-primary)]"> €{price * qty} </span> </div> )}<CartLine />Anatomy
| Part | What it is |
|---|---|
| Field | Has role=spinbutton, plus its value and limits, so screen readers know what it is. It's a text field that brings up a number keypad on phones, not the browser's own number input. |
| Decrease / Increase | Secondary IconButtons named after the field. As the pattern says, Tab skips them. They're disabled at the lowest and highest value. |
Props
- labelstring
- Names the field and both buttons ("Decrease Speed loader quantity"). Leave it out inside a FormField, which sets it from its own label. Outside one it is required, and `division-ds check` flags a missing one.
- valuenumber
- Controlled value (your page holds it).
- defaultValuenumberDefault min
- Starting value when the component holds the value itself (uncontrolled).
- onValueChange(value: number) => void
- Fires with the final value, pulled inside the limits: after a button click, an arrow key, Home/End, or when typing is finished by leaving the field or pressing Enter.
- minnumberDefault 0
- Lowest allowed value.
- maxnumberDefault 99
- Highest allowed value.
- stepnumberDefault 1
- The size of each step the value snaps to, counted from min.
- disabledbooleanDefault false
- Disables the field and both buttons.
- inputSize'sm' | 'md'Default 'md'
- Size of the field and buttons.
- classNamestring
- Combined with the root classes. Where Tailwind utility classes clash, the later one wins.
- refRef<HTMLInputElement>
- Passed through to the field.
- idstring
- The field's id, so a FormField's label and error text can point at it. FormField sets it.
- aria-describedbystring
- The ids of the help and error text, set on the field so screen readers read them out. FormField sets it.
- invalidboolean
- Marks the field as invalid: aria-invalid and the invalid border. FormField sets it when there is an error.
- requiredboolean
- Marks the field as required. FormField sets it.
Keyboard
| Keys | Result |
|---|---|
| ↑↓ | One step. |
| Page UpPage Down | Ten steps. |
| HomeEnd | Jumps to the minimum or maximum. |
| Enter | Confirms what you typed. |
| Tab | Leaves the field and confirms what you typed. Tab doesn't stop on the buttons. |
Do and don't
Do
- Set a real maximum, like the stock level or an order limit, not an arbitrary 99.
- Name the field after the item: 'Speed loader quantity'.
- Show the line total beside it, updating as the value changes.
Don't
- Don't use it for values that aren't whole numbers; that's an Input.
- Don't use it for a range of hundreds; that's a Slider or an Input.
- Don't pull the value back inside the limits while someone's still typing.