NumberInput
A box for a number, with two small buttons to go up and down. It writes the number the way the person’s language does (1,500.5 or 1.500,5) and can show a currency, a percent or a unit.
import { NumberInput } from '@sypnia/ui/react'
A number with limits
Between 1 and 90 days. Try the arrows of the keyboard.
A backup made today is deleted in 7 days. From 1 to 90.
Code
import { useState } from 'react';
import { Field, FormGrid, NumberInput } from '@sypnia/ui/react';
export default function Basic() {
const [days, setDays] = useState<number | null>(7);
return (
<FormGrid>
<Field label="Keep backups for" hint={days ? `A backup made today is deleted in ${days} days. From 1 to 90.` : 'From 1 to 90 days.'}>
<NumberInput value={days} onChange={setDays} min={1} max={90} formatOptions={{ style: 'unit', unit: 'day', unitDisplay: 'long' }} />
</Field>
</FormGrid>
);
} Currency, percent and units
The same control writes money, percentages and sizes. Switch the page to Spanish and the commas and points change places.
We write to you at 80 % of it.
The share of requests that are recorded.
Code
import { Field, FormGrid, NumberInput } from '@sypnia/ui/react';
export default function Formats() {
return (
<FormGrid>
<Field label="Monthly budget" hint="We write to you at 80 % of it.">
<NumberInput defaultValue={1250.5} min={0} step={0.5} formatOptions={{ style: 'currency', currency: 'USD' }} />
</Field>
<Field label="Sample rate" hint="The share of requests that are recorded.">
<NumberInput defaultValue={0.25} min={0} max={1} step={0.05} formatOptions={{ style: 'percent' }} />
</Field>
<Field label="Storage limit">
<NumberInput defaultValue={20} min={1} max={500} formatOptions={{ style: 'unit', unit: 'gigabyte' }} />
</Field>
<Field label="Rows per page">
<NumberInput defaultValue={1500} min={10} max={5000} step={10} />
</Field>
</FormGrid>
);
} Sizes and states
Small, with an error that says how to fix it, and disabled.
This plan allows up to 60 connections. Lower the number or change the plan.
Replicas come with the Pro plan.
Code
import { Field, FormGrid, NumberInput } from '@sypnia/ui/react';
export default function States() {
return (
<FormGrid>
<Field label="Small, for toolbars">
<NumberInput size="sm" defaultValue={50} min={10} max={500} step={10} />
</Field>
<Field label="Connections" error="This plan allows up to 60 connections. Lower the number or change the plan.">
<NumberInput defaultValue={120} />
</Field>
<Field label="Replicas" hint="Replicas come with the Pro plan.">
<NumberInput disabled defaultValue={0} />
</Field>
</FormGrid>
);
} When to use it
Use it when
- An exact number matters: rows per page, days a backup is kept, a price, a limit.
- The number has a floor, a ceiling or a step:
min,max,step. - It carries a unit: pass
formatOptionsas inIntl.NumberFormat.
Do not use it when
- The figure is approximate and «more or less» is what matters: a Slider.
- It is a code made of digits (a phone, a card, a postal code): that is text, use Input.
- The choice is among three or four fixed amounts: RadioGroup or SegmentedControl.
For everyone
- Up and down arrows add and take one step; Page Up and Page Down ten; Home and End go to the limits. The mouse wheel changes it only while it has the focus.
- The steppers are not tab stops: the keyboard already has the arrows. On touch screens they are 44 px.
- What is typed is checked when the person leaves the box: out of range goes to the nearest limit, and text that is not a number goes back to the last value.
- Inside a Field it takes the label, the hint and the error by itself; alone, give it an
aria-label.
Props
| NumberInput | Type | Default |
|---|---|---|
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
defaultValue | number | |
disabled | boolean | |
formatOptionsHow the number is written, as in Intl.NumberFormat: `{ style: 'currency', currency: 'USD' }`, `{ style: 'percent' }`, `{ style: 'unit', unit: 'gigabyte' }`. | NumberFormatOptions | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
max | number | |
min | number | |
name | string | |
onChange | ((value: number | null) => void) | |
placeholder | string | |
size | "sm" | "md" | "lg" | |
stepHow much each press of a stepper or an arrow key adds. | number | |
valueThe number, or null while the box is empty. | number | null |