Number field
A number input with steppers on Base UI NumberField.
With a label
"use client"
import { useState } from "react"
import { Label } from "@/components/ui/label"
import { NumberField } from "@/components/ui/number-field"
export default function NumberFieldWithALabel() {
const [seats, setSeats] = useState<number | null>(5)
return (
<div className="grid w-40 gap-2">
<Label htmlFor="number-field-seats">Seats</Label>
<NumberField id="number-field-seats" value={seats} onValueChange={setSeats} />
</div>
)
}
Minimum and maximum
Between 1 and 4.
"use client"
import { useState } from "react"
import { FieldDescription } from "@/components/ui/field"
import { Label } from "@/components/ui/label"
import { NumberField } from "@/components/ui/number-field"
export default function NumberFieldMinimumAndMaximum() {
const [reviewers, setReviewers] = useState<number | null>(2)
return (
<div className="grid w-40 gap-2">
<Label htmlFor="number-field-reviewers">Reviewers</Label>
<NumberField
id="number-field-reviewers"
value={reviewers}
onValueChange={setReviewers}
min={1}
max={4}
aria-describedby="number-field-reviewers-description"
/>
<FieldDescription id="number-field-reviewers-description">Between 1 and 4.</FieldDescription>
</div>
)
}
Step and format
"use client"
import { useState } from "react"
import { Label } from "@/components/ui/label"
import { NumberField } from "@/components/ui/number-field"
const percent: Intl.NumberFormatOptions = { style: "percent" }
export default function NumberFieldStepAndFormat() {
const [discount, setDiscount] = useState<number | null>(0.15)
return (
<div className="grid w-40 gap-2">
<Label htmlFor="number-field-discount">Discount</Label>
<NumberField
id="number-field-discount"
value={discount}
onValueChange={setDiscount}
min={0}
max={0.5}
step={0.05}
format={percent}
/>
</div>
)
}
Disabled
"use client"
import { useState } from "react"
import { Label } from "@/components/ui/label"
import { NumberField } from "@/components/ui/number-field"
export default function NumberFieldDisabled() {
const [copies, setCopies] = useState<number | null>(3)
return (
<div className="group grid w-40 gap-2" data-disabled="true">
<Label htmlFor="number-field-copies">Copies</Label>
<NumberField id="number-field-copies" value={copies} onValueChange={setCopies} disabled />
</div>
)
}
Installation
npx shadcn@latest add @khaeli/number-fieldbunx --bun shadcn@latest add @khaeli/number-fieldInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
ArrowUpArrowDown- Raise and lower the value by one
step. Shift + ArrowUpShift + ArrowDown- Raise and lower it by ten steps.
Alt + ArrowUpAlt + ArrowDown- Raise and lower it by a tenth of a step.
HomeEnd- Jump to
minand tomax, when they are set. Tab- Moves focus to the input and then past the field: the two steppers are for pointers and are not in the tab order.
Accessible name
idgoes to the input, so a @khaeli/label withhtmlFornames it. Without a visible label, passaria-label.- The steppers are named Decrease and Increase.
- A hint such as the allowed range is tied to the input with
aria-describedby. - At a limit a polite live region says where the value stopped ("4, maximum"). A typed number outside the range is corrected when focus leaves, and the region says what it became ("Set to 4, the maximum").
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/number-field.json