Skip to content
Klarhimmel UI

Number field

guests
npx shadcn add @klarhimmel/number-field @klarhimmel/input

Examples

  • Basic

  • Decimal

  • States

DetailsAbout, usage, props and more

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

Up and Down add one, Page Up and Page Down add ten.

2 guests

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

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.

Props

PropTypeDefaultDescription
valuenumber | null-Controlled or uncontrolled. null means empty.
defaultValuenumber | nullnullControlled or uncontrolled. null means empty.
onValueChange(value: number | null) => void-Controlled or uncontrolled. null means empty.
minnumber-Limits for the keys, the buttons and typed text.
maxnumber-Limits for the keys, the buttons and typed text.
stepnumber1What the arrows and the buttons add. Default 1. Its decimals set how many the field shows (0.25 shows two).
largeStepnumber-What Page Up and Page Down add. Default ten steps.
unitstring-A unit after the number ("kg", "nights"). Said as part of the value to assistive tech.
namestring-Submits the number with a form (a hidden input).
idstring-
disabledboolean-Error styling; switches it off.
invalidboolean-Error styling; switches it off.
aria-labelstring-Needed when no Field or <label htmlFor> names it.
aria-labelledbystring-Needed when no Field or <label htmlFor> names it.
aria-describedbystring-
decreaseLabelstring'Decrease'The two buttons' names; pass them translated.
increaseLabelstring'Increase'The two buttons' names, and the language numbers are written in. Pass them translated.
localestring'en'The language numbers are written in (decimal comma or point).
classNamestring-Extra classes, merged after the component's own.

Accessibility

KeyDoes
Up / DownAdd or subtract the step; from empty, Up goes to the minimum
Page Up / Page DownAdd or subtract ten steps (or largeStep)
Home / EndJump to the minimum or the maximum, when there is one
Enter / leaving the fieldReads what was typed, inside the limits; a non-number is put back

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.

Accessibility