Forms
Number Field
Numeric input with −/+ steppers, arrow keys, min/max and locale-aware currency or percent formatting.
react-aria-componentslucide-react
Preview
Your plan includes up to 2 seats.
"use client";
import { NumberField } from "@/components/ui/number-field";
export default function Example() {
return (
<div className="grid w-full max-w-md gap-4 sm:grid-cols-2">
<NumberField label="Daily email limit" defaultValue={120} minValue={0} maxValue={500} step={10} />
<NumberField label="Deal size" defaultValue={24000} formatOptions={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }} step={1000} />
<NumberField label="Reply rate target" defaultValue={0.12} formatOptions={{ style: "percent" }} step={0.01} />
<NumberField label="Seats" defaultValue={3} minValue={1} errorMessage="Your plan includes up to 2 seats." />
</div>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-components lucide-reactCreate a
number-field.tsxfile and paste the following code into it.src/components/ui/number-field.tsx"use client"; import { Minus, Plus } from "lucide-react"; import { Button as AriaButton, Group, Input, NumberField as AriaNumberField, type NumberFieldProps as AriaNumberFieldProps } from "react-aria-components"; import { cx } from "@/utils/cx"; import { Description, ErrorMessage, Label, fieldBase } from "./field"; export interface NumberFieldProps extends Omit<AriaNumberFieldProps, "className" | "children"> { label?: string; description?: string; errorMessage?: string; className?: string; } const stepper = "inline-flex h-full w-10 shrink-0 cursor-default items-center justify-center text-fg-secondary outline-none transition-colors data-hovered:bg-subtle data-hovered:text-fg data-pressed:bg-active data-disabled:text-fg-disabled data-focus-visible:shadow-[inset_0_0_0_2px_var(--color-accent)] [&_svg]:size-3.5"; /** Numeric input with −/+ steppers, keyboard arrows, min/max and locale formatting. */ export function NumberField({ label, description, errorMessage, className, ...props }: NumberFieldProps) { return ( <AriaNumberField {...props} isInvalid={props.isInvalid ?? Boolean(errorMessage)} className={cx("flex flex-col gap-2", className)}> {label && <Label>{label}</Label>} <Group className={cx(fieldBase, "flex h-10 items-center overflow-hidden")}> <AriaButton slot="decrement" className={cx(stepper, "border-r border-line")}> <Minus /> </AriaButton> <Input className="h-full min-w-0 flex-1 bg-transparent text-center tabular outline-none" /> <AriaButton slot="increment" className={cx(stepper, "border-l border-line")}> <Plus /> </AriaButton> </Group> {description && !errorMessage && <Description>{description}</Description>} <ErrorMessage>{errorMessage}</ErrorMessage> </AriaNumberField> ); }Create a
field.tsxfile and paste the following code into it.src/components/ui/field.tsx"use client"; import type { ReactNode } from "react"; import { FieldError, Label as AriaLabel, Text } from "react-aria-components"; import { cx } from "@/utils/cx"; /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */ export const fieldBase = "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " + "placeholder:text-fg-muted data-hovered:border-fg-disabled " + "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " + "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " + "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled"; export function Label({ className, children }: { className?: string; children: ReactNode }) { return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>; } export function Description({ children }: { children: ReactNode }) { return ( <Text slot="description" className="text-sm text-fg-muted"> {children} </Text> ); } export function ErrorMessage({ children }: { children?: ReactNode }) { return <FieldError className="text-sm text-error-text">{children}</FieldError>; }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| formatOptions | Intl.NumberFormatOptions | — | Currency, percent, units… |
| minValue / maxValue / step | number | — | Range and increment. |