# NumberInput (/components/number-input)

<ReferenceLinks
  links={[
    { label: "Source", href: meta.links.source, iconName: "code" },
    { label: "Stories", href: meta.links.stories },
  ]}
/>

<Stage code={"<NumberInput\n  value={8}\n  min={1}\n  max={64}\n  width=\"xs\"\n  aria-label=\"vCPU limit\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={0.75}\n  step={0.05}\n  width=\"xs\"\n  aria-label=\"Sampling temperature\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={1024}\n  hideStepper\n  width=\"xs\"\n  aria-label=\"Max tokens\"\n  onChange={() => {}}\n/>\n<NumberInput\n  variant=\"subtle\"\n  value={12}\n  width=\"fitContent\"\n  aria-label=\"Page size\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={8}
      min={1}
      max={64}
      width="xs"
      aria-label="vCPU limit"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={0.75}
      step={0.05}
      width="xs"
      aria-label="Sampling temperature"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={1024}
      hideStepper
      width="xs"
      aria-label="Max tokens"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      variant="subtle"
      value={12}
      width="fitContent"
      aria-label="Page size"
      onChange={noop}
    />
  </DemoState>
</Stage>

## Installation [#installation]

1. Install the package.

```sh
npm install @hashintel/ds-components
```

2. Import the component.

```tsx
import { NumberInput } from "@hashintel/ds-components";

export const RetryLimitInput = ({
  value,
  onChange,
}: {
  value: number | null;
  onChange: (value: number | null) => void;
}) => (
  <NumberInput
    value={value}
    onChange={onChange}
    min={0}
    max={10}
    width="xs"
    aria-label="Retry limit"
  />
);
```

### When to use [#when-to-use]

<DoList
  items={[
    "Use NumberInput when the stored value is a number: counts, limits, timeouts, numeric entity properties.",
    <Fragment key="0">
      Set <code>min</code>, <code>max</code>, <code>step</code> and <code>inputMode</code> to match the
      value's constraints — stepping stays in range and mobile devices get the
      matching keyboard.
    </Fragment>,
    <Fragment key="1">
      Wrap it in <code>Form.Field</code> for a label, description, and error
      messages — the label connects to the input automatically.
    </Fragment>,
  ]}
/>

### When not to use [#when-not-to-use]

<DontList
  items={[
    <Fragment key="0">
      Don't use it for numeric codes — IDs, phone numbers, postal codes — where
      leading zeros matter and stepping makes no sense. Use TextInput with{" "}
      <code>inputMode="numeric"</code>.
    </Fragment>,
    "Don't use it when the rough position in a bounded range matters more than the exact figure — use Slider.",
    <Fragment key="1">
      Don't expect formatted text while typing (thousands separators, currency
      symbols) — the field holds a plain number. Use <code>styledValue</code>{" "}
      for a formatted unfocused display and a prefix or suffix for symbols.
    </Fragment>,
  ]}
/>

## Examples [#examples]

### Basic [#basic]

NumberInput is controlled: pass `value` and take the parsed number from `onChange`, which reports `null` when the field is empty.

<Stage code={"<NumberInput\n  value={32}\n  min={1}\n  max={512}\n  width=\"xs\"\n  aria-label=\"Batch size\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={32}
      min={1}
      max={512}
      width="xs"
      aria-label="Batch size"
      onChange={noop}
    />
  </DemoState>
</Stage>

```tsx
import { useState } from "react";

import { NumberInput } from "@hashintel/ds-components";

export const BatchSizeInput = () => {
  const [batchSize, setBatchSize] = useState<number | null>(32);

  return (
    <NumberInput
      value={batchSize}
      onChange={(value) => setBatchSize(value)}
      min={1}
      max={512}
      width="xs"
      aria-label="Batch size"
    />
  );
};
```

### Decimal values [#decimal-values]

`step` defaults to `1`, which makes an integer input: decimal-separator keys are rejected and parsed values are truncated. A fractional step — or `"any"` — switches to decimal mode and the decimal mobile keyboard.

<Stage code={"<NumberInput\n  value={0.25}\n  step={0.01}\n  width=\"xs\"\n  aria-label=\"Confidence threshold\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={3.14159}\n  step=\"any\"\n  width=\"xs\"\n  aria-label=\"Property value\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={0.25}
      step={0.01}
      width="xs"
      aria-label="Confidence threshold"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={3.14159}
      step="any"
      width="xs"
      aria-label="Property value"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Minimum and maximum [#minimum-and-maximum]

`min` defaults to `0` — pass a negative `min` to allow negative values. The bounds constrain stepping only: users can still type outside the range, so validate the value and set `invalid` yourself.

<Stage code={"<NumberInput\n  value={5}\n  min={1}\n  max={10}\n  width=\"xs\"\n  aria-label=\"Crawl depth\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={5}
      min={1}
      max={10}
      width="xs"
      aria-label="Crawl depth"
      onChange={noop}
    />
  </DemoState>
</Stage>

### States [#states]

Disabled, invalid, and loading, in that order. `invalid` is presentational — an out-of-range typed value doesn't set it automatically.

<Stage code={"<NumberInput\n  value={16}\n  disabled\n  width=\"xs\"\n  aria-label=\"Instance count\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={2048}\n  max={1024}\n  invalid\n  width=\"xs\"\n  aria-label=\"Max tokens\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={42}\n  loading\n  width=\"xs\"\n  aria-label=\"Random seed\"\n  onChange={() => {}}\n/>"}>
  <NumberInput
    value={16}
    disabled
    width="xs"
    aria-label="Instance count"
    onChange={noop}
  />
  <DemoState>
    <NumberInput
      value={2048}
      max={1024}
      invalid
      width="xs"
      aria-label="Max tokens"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={42}
      loading
      width="xs"
      aria-label="Random seed"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Read-only [#read-only]

`readonly` renders the value as plain text — no border, stepper, or focus ring — for view modes that later switch to editing. Unlike `disabled`, it reads as content rather than a blocked control.

<Stage code={"<NumberInput\n  value={1250}\n  width=\"xs\"\n  aria-label=\"Entity count\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={1250}\n  readonly\n  aria-label=\"Entity count\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={1250}
      width="xs"
      aria-label="Entity count"
      onChange={noop}
    />
  </DemoState>
  <NumberInput
    value={1250}
    readonly
    aria-label="Entity count"
    onChange={noop}
  />
</Stage>

### Subtle variant [#subtle-variant]

`subtle` drops the border and background so the field reads as inline text until hovered or focused. Use it for in-place editing in dense surfaces; keep `default` in forms.

<Stage code={"<NumberInput\n  variant=\"default\"\n  value={3}\n  width=\"xs\"\n  aria-label=\"Retry limit\"\n  onChange={() => {}}\n/>\n<NumberInput\n  variant=\"subtle\"\n  value={3}\n  width=\"fitContent\"\n  aria-label=\"Retry limit\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      variant="default"
      value={3}
      width="xs"
      aria-label="Retry limit"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      variant="subtle"
      value={3}
      width="fitContent"
      aria-label="Retry limit"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Sizes [#sizes]

`md` is the default. Match the size of neighbouring inputs and buttons; use `xxs`–`sm` in dense chrome such as table rows and toolbars.

<Stage code={"<NumberInput\n  size=\"xxs\"\n  placeholder=\"xxs\"\n  value={null}\n  width=\"xs\"\n  aria-label=\"xxs size\"\n  onChange={() => {}}\n/>\n<NumberInput\n  size=\"xs\"\n  placeholder=\"xs\"\n  value={null}\n  width=\"xs\"\n  aria-label=\"xs size\"\n  onChange={() => {}}\n/>\n<NumberInput\n  size=\"sm\"\n  placeholder=\"sm\"\n  value={null}\n  width=\"xs\"\n  aria-label=\"sm size\"\n  onChange={() => {}}\n/>\n<NumberInput\n  size=\"md\"\n  placeholder=\"md\"\n  value={null}\n  width=\"xs\"\n  aria-label=\"md size\"\n  onChange={() => {}}\n/>\n<NumberInput\n  size=\"lg\"\n  placeholder=\"lg\"\n  value={null}\n  width=\"xs\"\n  aria-label=\"lg size\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      size="xxs"
      placeholder="xxs"
      value={null}
      width="xs"
      aria-label="xxs size"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      size="xs"
      placeholder="xs"
      value={null}
      width="xs"
      aria-label="xs size"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      size="sm"
      placeholder="sm"
      value={null}
      width="xs"
      aria-label="sm size"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      size="md"
      placeholder="md"
      value={null}
      width="xs"
      aria-label="md size"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      size="lg"
      placeholder="lg"
      value={null}
      width="xs"
      aria-label="lg size"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Widths [#widths]

Width presets align fields across form rows: `xs`, `sm`, `md`, `lg`, plus `fullWidth` (the default, stretching to the container) and `fitContent`, which hugs the current value. Set a custom width with `className` when alignment doesn't matter.

<Stage code={"<NumberInput\n  width=\"xs\"\n  placeholder=\"xs\"\n  value={null}\n  aria-label=\"xs width\"\n  onChange={() => {}}\n/>\n<NumberInput\n  width=\"sm\"\n  placeholder=\"sm\"\n  value={null}\n  aria-label=\"sm width\"\n  onChange={() => {}}\n/>\n<NumberInput\n  width=\"md\"\n  placeholder=\"md\"\n  value={null}\n  aria-label=\"md width\"\n  onChange={() => {}}\n/>\n<NumberInput\n  width=\"fitContent\"\n  value={1234}\n  aria-label=\"fitContent width\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      width="xs"
      placeholder="xs"
      value={null}
      aria-label="xs width"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      width="sm"
      placeholder="sm"
      value={null}
      aria-label="sm width"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      width="md"
      placeholder="md"
      value={null}
      aria-label="md width"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      width="fitContent"
      value={1234}
      aria-label="fitContent width"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Sizing to the maximum value [#sizing-to-the-maximum-value]

`width="maxNumber"` sizes the field to exactly fit `max` at `step`'s precision. It assumes the stepper is hidden, so pair it with `hideStepper`, and it needs a finite `max`.

<Stage code={"<NumberInput\n  width=\"maxNumber\"\n  hideStepper\n  value={999}\n  max={999}\n  aria-label=\"Max retries\"\n  onChange={() => {}}\n/>\n<NumberInput\n  width=\"maxNumber\"\n  hideStepper\n  value={98.425}\n  max={99}\n  step={0.001}\n  aria-label=\"Threshold\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      width="maxNumber"
      hideStepper
      value={999}
      max={999}
      aria-label="Max retries"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      width="maxNumber"
      hideStepper
      value={98.425}
      max={99}
      step={0.001}
      aria-label="Threshold"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Hiding the stepper [#hiding-the-stepper]

The spin buttons show only while the field is focused but reserve their space; `hideStepper` removes them and reclaims it. Hide the stepper when values are typed rather than nudged.

<Stage code={"<NumberInput\n  value={1024}\n  width=\"xs\"\n  aria-label=\"With stepper\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={1024}\n  hideStepper\n  width=\"xs\"\n  aria-label=\"Without stepper\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={1024}
      width="xs"
      aria-label="With stepper"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={1024}
      hideStepper
      width="xs"
      aria-label="Without stepper"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Clearable [#clearable]

`clearable` shows a clear button on hover or focus; clearing reports `null` to `onChange` and refocuses the input. Pass `{ onClear }` to run your own reset instead.

<Stage code={"<NumberInput\n  value={250}\n  clearable\n  width=\"xs\"\n  aria-label=\"Row limit\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={250}
      clearable
      width="xs"
      aria-label="Row limit"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Units with prefix and suffix [#units-with-prefix-and-suffix]

Put the unit in an adornment so users enter only the number. Text adornments take `{ text }`, icons `{ iconName }`, and either becomes a button when given `onClick`.

<Stage code={"<NumberInput\n  value={512}\n  suffix={{ text: \"MB\" }}\n  width=\"xs\"\n  aria-label=\"Memory limit\"\n  onChange={() => {}}\n/>\n<NumberInput\n  value={20}\n  step={0.01}\n  prefix={{ text: \"$\" }}\n  width=\"xs\"\n  aria-label=\"Monthly budget\"\n  onChange={() => {}}\n/>"}>
  <DemoState>
    <NumberInput
      value={512}
      suffix={{ text: "MB" }}
      width="xs"
      aria-label="Memory limit"
      onChange={noop}
    />
  </DemoState>
  <DemoState>
    <NumberInput
      value={20}
      step={0.01}
      prefix={{ text: "$" }}
      width="xs"
      aria-label="Monthly budget"
      onChange={noop}
    />
  </DemoState>
</Stage>

### Formatted display [#formatted-display]

`styledValue` overlays a custom rendering while the field is unfocused — thousands separators, highlights — and reveals the raw number for editing on focus.

<Stage code={formattedDisplayCode}>
  <FormattedDisplayDemo />
</Stage>

### Connected inputs [#connected-inputs]

Set `connectToRightInput` on the left input and `connectToLeftInput` on the
right to join a pair into one visual control. Only the `default` variant
connects — `subtle` and `readonly` inputs render separately.

<Stage code={"<div style={{ display: \"flex\", alignItems: \"center\" }}>\n  <NumberInput\n    connectToRightInput\n    value={1}\n    width=\"xs\"\n    aria-label=\"Minimum instances\"\n    onChange={() => {}}\n  />\n  <NumberInput\n    connectToLeftInput\n    value={8}\n    width=\"xs\"\n    aria-label=\"Maximum instances\"\n    onChange={() => {}}\n  />\n</div>"}>
  <div style={{ display: "flex", alignItems: "center" }}>
    <DemoState>
      <NumberInput
        connectToRightInput
        value={1}
        width="xs"
        aria-label="Minimum instances"
        onChange={noop}
      />
    </DemoState>
    <DemoState>
      <NumberInput
        connectToLeftInput
        value={8}
        width="xs"
        aria-label="Maximum instances"
        onChange={noop}
      />
    </DemoState>
  </div>
</Stage>

### In a form [#in-a-form]

Inside `Form.Field` the label connects to the input automatically. Keep `invalid` and the `errors` list in step so the message explains the red border.

```tsx
import { Form, NumberInput } from "@hashintel/ds-components";

export const MaxTokensField = ({
  value,
  error,
  onChange,
}: {
  value: number | null;
  error?: string;
  onChange: (value: number | null) => void;
}) => (
  <Form.Field
    label="Max tokens"
    description="Upper limit for each flow step"
    errors={error ? [error] : undefined}
  >
    <NumberInput
      value={value}
      onChange={onChange}
      min={1}
      max={128000}
      width="sm"
      invalid={!!error}
    />
  </Form.Field>
);
```

## Behavior [#behavior]

- An integer `step` makes an integer input: decimal-separator and exponent keys are blocked with a brief grey flash, and parsed values are truncated. A fractional step (or `"any"`) accepts every locale's decimal separator.
- Typed values are never clamped: arrows and spin buttons stay within `min`/`max`, but typing can leave the range. Validate the value and set `invalid`.
- Scrolling over a focused field doesn't change the value — the wheel is disabled on focus to prevent accidental edits.
- Spin buttons appear on focus and reserve their space while hidden, so the layout never shifts; Firefox never shows them.
- The minimum width fits two digits plus padding — much narrower than other inputs — so number fields sit comfortably in table rows.

## Content [#content]

- Name the quantity in the label and put the unit in a suffix adornment: "Memory limit" plus "MB", not "Memory limit in megabytes".
- Use the placeholder for an example value ("e.g. 512"), never as the label — it disappears as soon as users type.
- Put the valid range in the field description, and template errors as "[Field] must be between [min] and [max]": "Batch size must be between 1 and 512."

## Accessibility [#accessibility]

NumberInput renders a native `<input type="number">`, which implements the [WAI-ARIA spinbutton pattern](https://www.w3.org/WAI/ARIA/apg/patterns/spinbutton/) — arrow-key stepping and numeric semantics come from the browser.

### What you must do [#what-you-must-do]

- Always label the input — via `Form.Field`, your own label with `htmlForId`, or `aria-label`.
- Pair `invalid` with a visible message (for example `Form.Field` `errors`) — color alone doesn't explain the problem.
- Surface the valid range in the label or description — rejected keystrokes and stepping limits only flash the field, they don't announce why.

## API reference [#api-reference]

### NumberInput [#numberinput]

<PropsTable
  of={NumberInput}
  defaults={{
    min: "0",
    max: "Number.MAX_SAFE_INTEGER",
    step: "1",
    variant: '"default"',
    size: '"md"',
    align: '"left"',
    width: '"fullWidth"',
    autocomplete: "false",
  }}
/>

### PrefixOrSuffix [#prefixorsuffix]

The object shape `prefix` and `suffix` take.

<PropsTable of={PrefixOrSuffixDoc} defaults={{ variant: '"default"' }} />
