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.

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a number-field.tsx file 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>
      );
    }
  3. Create a field.tsx file 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>;
    }
  4. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.

Props

PropTypeDefaultDescription
formatOptionsIntl.NumberFormatOptions—Currency, percent, units…
minValue / maxValue / stepnumber—Range and increment.