Number Field

A number typed, stepped or scrubbed by a person or agent.

Source
$npx @dinachi/cli@latest add number-field
min
max
step
NumberFieldScrubArea
disabled

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>
  );
}

API

valuenumber | null
Controlled value.
defaultValuenumber | null = null
Starting value when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
onValueCommitted
agent
(value, details) => void
Runs when a person commits, such as on blur, and after each accepted agent change.
min
agent
number
Lowest value. Agent changes are clamped to it.
max
agent
number
Highest value. Agent changes are clamped to it.
step
agent
number | "any" = 1
How far one step moves. With "any" the agent steps by 1.
smallStepnumber = 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
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Shows the value, refuses any change.
namestring
Submits the value with a form.
requiredboolean = false
Must have a value before the form submits.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as number.

Reads

label

The input's label or aria-label.

value

The current number, or nothing while empty.

min / max

The range, when set.

step

The step size. 1 when step is "any".

disabled

True 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.