# Number field

- **[Atoms](/components/)**

A number you can type or step, with minus and plus buttons: the WAI-ARIA spin button.

## When to use

- **Use for**: A quantity people may know or want to nudge: guests, nights, quantity.
- **Not for**: A rough value on a range. Use Slider. A choice among named options. Use Select.
- **Pairs with**: Field, a unit, a status line.

## Preview

number-field.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { Field } from '@/components/klarhimmel/ui/input';
import { NumberField } from '@/components/klarhimmel/ui/number-field';

export default function Example() {
  return (
    <Theme theme="brand">
      <Field label="Guests">
        {(p) => <NumberField {...p} defaultValue={2} min={1} max={20} />}
      </Field>
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/number-field @klarhimmel/input
  ```
- **Import**

  ```
  import { NumberField } from '@/components/klarhimmel/ui/number-field';
  ```

- **Files**: number-field.tsx, input.tsx, in components/klarhimmel/ui/. With it: button, editing, input-group, theme, utils. npm: lucide-react.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.

## Examples

- **Decimal**

  ```
  import { Field } from '@/components/klarhimmel/ui/input';
  import { NumberField } from '@/components/klarhimmel/ui/number-field';
  
  export default function NumberFieldDecimal() {
    return (
      <div className="flex w-[min(24rem,calc(100cqw-3rem))] flex-col gap-5">
        <Field label="Luggage weight" hint="Steps of a quarter kilo. Type 12,5 or 12.5.">
          {(p) => <NumberField {...p} defaultValue={12.5} step={0.25} min={0} max={32} unit="kg" />}
        </Field>
        <Field label="Nights">
          {(p) => <NumberField {...p} defaultValue={3} min={1} max={30} unit="nights" decreaseLabel="One night fewer" increaseLabel="One night more" />}
        </Field>
      </div>
    );
  }
  ```
- **States**

  ```
  import { Field } from '@/components/klarhimmel/ui/input';
  import { NumberField } from '@/components/klarhimmel/ui/number-field';
  
  export default function NumberFieldStates() {
    return (
      <div className="flex w-[min(24rem,calc(100cqw-3rem))] flex-col gap-5">
        <Field label="Rooms (empty)" hint="The first Up goes to the minimum.">
          {(p) => <NumberField {...p} min={1} max={10} />}
        </Field>
        <Field label="Extra beds" error="At most two extra beds per room.">
          {(p) => <NumberField {...p} defaultValue={3} max={3} invalid />}
        </Field>
        <Field label="Children (set by the room)">
          {(p) => <NumberField {...p} defaultValue={2} disabled />}
        </Field>
      </div>
    );
  }
  ```

## Props

- **value**: Controlled or uncontrolled. null means empty.
- **defaultValue**: Controlled or uncontrolled. null means empty.
- **onValueChange**: Controlled or uncontrolled. null means empty.
- **min**: Limits for the keys, the buttons and typed text.
- **max**: Limits for the keys, the buttons and typed text.
- **step**: What the arrows and the buttons add. Default 1. Its decimals set how many the field shows (0.25 shows two).
- **largeStep**: What Page Up and Page Down add. Default ten steps.
- **unit**: A unit after the number ("kg", "nights"). Said as part of the value to assistive tech.
- **name**: Submits the number with a form (a hidden input).
- **id**
- **disabled**: Error styling; switches it off.
- **invalid**: Error styling; switches it off.
- **aria-label**: Needed when no Field or <label htmlFor> names it.
- **aria-labelledby**: Needed when no Field or <label htmlFor> names it.
- **aria-describedby**
- **decreaseLabel**: The two buttons' names; pass them translated.
- **increaseLabel**: The two buttons' names, and the language numbers are written in. Pass them translated.
- **locale**: The language numbers are written in (decimal comma or point).
- **className**: Extra classes, merged after the component's own.

## Accessibility

- Up / Down · Add or subtract the step; from empty, Up goes to the minimum
- Page Up / Page Down · Add or subtract ten steps (or largeStep)
- Home / End · Jump to the minimum or the maximum, when there is one
- Enter / leaving the field · Reads what was typed, inside the limits; a non-number is put back

[Accessibility](/get-started/accessibility)

The WAI-ARIA spin button. The buttons are for the pointer and are not tab stops: the keyboard has the arrow keys. A decimal comma is accepted.

[Previous](/components/input-otp)

[Next](/components/agent-chat)
