Number input
A number field with stepper buttons, arrow keys, limits and a form value.
Demo
Usage
import { Field, NumberInput } from '@tuttn/slant'
// Controlled: the value is a number, or null when the field is empty.
const [agents, setAgents] = useState<number | null>(4)
<NumberInput value={agents} onValueChange={setAgents} min={1} max={16} aria-label="Parallel agents" />
// Uncontrolled, formatted while blurred, submitted with a form as a plain number.
<form>
<NumberInput name="budget" defaultValue={1200} min={0} step={50}
formatOptions={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }} aria-label="Budget" />
</form>
// In a Field: the label, description, error, disabled and required reach the input.
<Field invalid={agents === null} required>
<Field.Label>Parallel agents</Field.Label>
<NumberInput value={agents} onValueChange={setAgents} min={1} max={16} />
<Field.Error>Choose how many agents run at once.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | — | The number, or null when the field is empty. Pass it to control the field; leave it out to let the field keep its own. |
defaultValue | number | null | null | The number an uncontrolled field starts with. |
onValueChange | (value: number | null) => void | — | Called with the committed number (or null) after Enter, blur, an arrow key or a stepper button. Typing alone doesn't call it. |
min | number | — | The lowest value. Commits clamp to it, and Home jumps to it. |
max | number | — | The highest value. Commits clamp to it, and End jumps to it. |
step | number | 1 | What the arrow keys and the stepper buttons add or take away. Committed values snap to multiples of it, counted from `min` (or 0). |
largeStep | number | step × 10 | What Page Up and Page Down add or take away. |
formatOptions | Intl.NumberFormatOptions | — | How the number reads while the field isn't focused (currency, percent, units, fraction digits), and its `aria-valuetext`. While focused, the field shows the plain number. |
locale | string | the browser's | The locale the number is formatted and parsed in. |
size | 'sm' | 'md' | 'lg' | 'md' | Height on the control scale: 28, 34 or 40px at regular density. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this. |
incrementLabel | string | 'Increase' | The increment button's accessible name. From the root's `messages.increment`. |
decrementLabel | string | 'Decrease' | The decrement button's accessible name. From the root's `messages.decrement`. |
…rest | InputHTMLAttributes<HTMLInputElement> | — | The ref and every other attribute (id, placeholder, aria-*, handlers) reach the visible input; className and style go on the group. `name` and `form` go on the hidden input that carries the number. |
Keyboard
- ArrowUp and ArrowDown add or take away
step; Page Up and Page Down add or take awaylargeStep(ten steps unless set). - Home and End jump to
minandmaxwhen they are set, and move the caret when they aren't. - Enter commits what was typed, and still submits a form around the field.
Committing
What the reader types is a draft, and onValueChange hears nothing until Enter or blur commits it. A commit parses the draft in the locale (group separators are ignored), clamps it to min and max, and snaps it to the nearest multiple of step counted from min (or 0). An empty draft commits null; one that isn't a number puts the last value back. Steps round away float noise, so 0.1 and 0.2 make 0.3.
Accessibility
The input has role="spinbutton" with aria-valuenow, aria-valuemin and aria-valuemax, and with formatOptions an aria-valuetext that reads the formatted number. It is a text input with a decimal keypad on phones, not type="number", so the wheel never changes it by accident. The stepper buttons are out of the tab order, since the arrow keys do the same; they are named by messages.increment and messages.decrement, and disable at the limits.
See Input group for the field it is drawn in, and Slider for a number picked by dragging.