Fulldev UI

A number input with decrement and increment buttons.

Installation

npx shadcn@latest add @fulldev/number-field

Usage

import {
  NumberField,
  NumberFieldDecrement,
  NumberFieldIncrement,
  NumberFieldInput,
} from "@/components/ui/number-field"
<NumberField>
  <NumberFieldDecrement />
  <NumberFieldInput name="quantity" value="1" min="1" aria-label="Quantity" />
  <NumberFieldIncrement />
</NumberField>

NumberField is an input group around a native <input type="number">, as data-slot leaves a number field to the browser. Set name, value, min, max and step on NumberFieldInput; it submits with its form and works without JavaScript. The buttons step the input as its arrow keys do and fire input and change, so listen to the input only. As in Base UI, a button is disabled when a step would not change the value, also with step="any", so give the input a numeric step or none. A script that sets the input’s value property fires input or change so the buttons follow. They are named “Decrease” and “Increase”; pass an aria-label in your language. Each click steps once; holding a button does not repeat, as it does in React.

Composition

NumberField
├── NumberFieldDecrement
├── NumberFieldInput
└── NumberFieldIncrement

API Reference

NumberFieldInput takes the attributes of InputGroupInput. NumberFieldDecrement and NumberFieldIncrement take the props of InputGroupButton, with size "icon-xs" by default, and render a minus or plus icon unless you pass children.

Source: src/components/ui/number-field