NumberInput
Una caja para un número, con dos botoncitos para subir y bajar. Escribe el número como lo hace el idioma de la persona (1,500.5 o 1.500,5) y puede mostrar una moneda, un porcentaje o una unidad.
import { NumberInput } from '@sypnia/ui/react'
Un número con límites
Entre 1 y 90 días. Prueba las flechas del teclado.
A backup made today is deleted in 7 days. From 1 to 90.
Código
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>
);
} Moneda, porcentaje y unidades
El mismo control escribe dinero, porcentajes y tamaños. Pasa la página a español y las comas y los puntos cambian de lugar.
We write to you at 80 % of it.
The share of requests that are recorded.
Código
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>
);
} Tamaños y estados
Pequeño, con un error que dice cómo arreglarlo, y desactivado.
This plan allows up to 60 connections. Lower the number or change the plan.
Replicas come with the Pro plan.
Código
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>
);
} Cuándo usarlo
Úsalo cuando
- Importa un número exacto: filas por página, días que se guarda una copia, un precio, un límite.
- El número tiene un piso, un techo o un paso:
min,max,step. - Lleva una unidad: pasa
formatOptionscomo enIntl.NumberFormat.
No lo uses cuando
- La cifra es aproximada y lo que importa es «más o menos»: un Slider.
- Es un código hecho de dígitos (un teléfono, una tarjeta, un código postal): eso es texto, usa Input.
- La opción está entre tres o cuatro cantidades fijas: RadioGroup o SegmentedControl.
Para todos
- Las flechas arriba y abajo suman y restan un paso; Re Pág y Av Pág diez; Inicio y Fin van a los límites. La rueda del ratón lo cambia solo mientras tiene el foco.
- Los botones de subir y bajar no son paradas de Tab: el teclado ya tiene las flechas. En pantallas táctiles miden 44 px.
- Lo escrito se revisa cuando la persona sale de la caja: lo que está fuera de rango va al límite más cercano, y un texto que no es número vuelve al último valor.
- Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un
aria-label.
Props
| NumberInput | Tipo | Por defecto |
|---|---|---|
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 |