$npx @dinachi/cli@latest add number-field
minmaxstepNumberFieldScrubAreadisabledquantity.tsx
import { NumberField, NumberFieldDecrement, NumberFieldGroup, NumberFieldIncrement, NumberFieldInput, NumberFieldScrubArea } from "@/components/ui/number-field"
export function Quantity() {
return (
<NumberField defaultValue={5} min={0} max={10}>
<NumberFieldScrubArea>
<label htmlFor="quantity">Quantity</label>
</NumberFieldScrubArea>
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput id="quantity" />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
)
}Usage
import {
NumberField,
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
} from "@/components/ui/number-field"
export function Quantity() {
return (
<NumberField defaultValue={1} min={1} max={10}>
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput aria-label="Quantity" />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
)
}Examples
An input between decrement and increment buttons.
import { NumberField, NumberFieldGroup, NumberFieldInput, NumberFieldIncrement, NumberFieldDecrement } from '@/components/ui/number-field';
export function Example() {
return (
<NumberField defaultValue={5} className="w-[180px]">
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
);
}The value stays between 1 and 10.
import { NumberField, NumberFieldGroup, NumberFieldInput, NumberFieldIncrement, NumberFieldDecrement } from '@/components/ui/number-field';
export function Example() {
return (
<div className="space-y-2">
<label className="text-sm font-medium">Quantity (1-10)</label>
<NumberField defaultValue={1} min={1} max={10} className="w-[180px]">
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
</div>
);
}A price that steps by 0.01.
import { NumberField, NumberFieldGroup, NumberFieldInput, NumberFieldIncrement, NumberFieldDecrement } from '@/components/ui/number-field';
export function Example() {
return (
<div className="space-y-2">
<label className="text-sm font-medium">Price ($)</label>
<NumberField defaultValue={9.99} step={0.01} min={0} className="w-[180px]">
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
</div>
);
}API
valuenumber | null
Controlled value.defaultValuenumber | null = null
Starting value when uncontrolled.onValueChange
Runs for people and agents. agent
(value, details) => voiddetails.cancel() refuses the change.onValueCommitted
Runs when a person commits, such as on blur, and after each accepted agent change.agent
(value, details) => voidmin
Lowest value. Agent changes are clamped to it.agent
numbermax
Highest value. Agent changes are clamped to it.agent
numberstep
How far one step moves. With "any" the agent steps by 1.agent
number | "any" = 1smallStepnumber = 0.1
The step while Alt is held.largeStepnumber = 10
The step while Shift is held.formatIntl.NumberFormatOptions
Formats the value in the input, for example as currency.localeIntl.LocalesArgument
The locale used to format and parse the value.allowWheelScrubboolean = false
Steps the value with the mouse wheel while the input is focused.snapOnStepboolean = false
Snaps stepped values to the nearest multiple of the step.disabled
Blocks people and agents.agent
boolean = falsereadOnly
Shows the value, refuses any change.agent
boolean = falsenamestring
Submits the value with a form.requiredboolean = false
Must have a value before the form submits.Takes the props of the element it renders.
aria-label
Names the field without a label element. The agent reads it too.agent
stringchildrenReactNode = <Minus />
Replaces the icon.childrenReactNode = <Plus />
Replaces the icon.direction"horizontal" | "vertical" = "horizontal"
Which way the pointer moves to scrub.pixelSensitivitynumber = 2
Pixels the pointer moves per step.childrenReactNode
The cursor icon.Agent
Inside an AgentProvider, it appears in agent.snapshot() as number.
Reads
labelThe input's label or aria-label.
valueThe current number, or nothing while empty.
min / maxThe range, when set.
stepThe step size. 1 when step is "any".
disabledTrue when the field is blocked.
Actions
setValue(42)Sets the value, clamped to min and max. A string that is not a number fails.
increment(3)Steps up by that many steps, 1 if omitted. An empty field steps from min, or 0.
decrement(3)Steps down the same way.
An agent's change runs through onValueChange like anyone's. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told the app refused. An accepted change also runs onValueCommitted. A disabled or readOnly field refuses on its own.
await agent.act({
steps: [{ ref: "number", action: "increment", args: [3] }],
})Accessibility
- The input is a text field described as a "Number field", with a numeric inputmode.
- Name every field with a label element or aria-label on
NumberFieldInput. The agent reads it too. - The step buttons are hidden from the agent, which steps through the field.
Keyboard
↑↓Steps the value up or down
Shift↑Steps by
largeStep, with ↓ to go downAlt↑Steps by
smallStep, with ↓ to go downHomeSets the value to min, when set
EndSets the value to max, when set
↑↓Steps the value up or down
Shift↑Steps by
largeStep, with ↓ to go downAlt↑Steps by
smallStep, with ↓ to go downHomeSets the value to min, when set
EndSets the value to max, when set