# Number Field

> A numeric input with stepper buttons, units, bounds and locale formatting.

```tsx title="hero.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
} from '@langgenius/dify-ui/number-field'

export function NumberFieldHeroDemo() {
  return (
    <Field name="topK" className="w-full max-w-80">
      <FieldLabel>Top K</FieldLabel>
      <NumberField min={1} max={10} smallStep={1}>
        <NumberFieldGroup>
          <NumberFieldInput placeholder="e.g. 3" />
          <NumberFieldControls>
            <NumberFieldIncrement />
            <NumberFieldDecrement />
          </NumberFieldControls>
        </NumberFieldGroup>
      </NumberField>
      <FieldDescription>Chunks returned for each query, from 1 to 10.</FieldDescription>
    </Field>
  )
}
```

## Usage guidelines

- Use `NumberField` for a precise number the user types or nudges, such as Top K, a timeout or a token limit. Use `Slider` when an approximate value within a range is enough, and `Input` for numeric identifiers such as codes or phone numbers.
- Put the number field in a [`Field`](/components/field.md) with a `FieldLabel`; the label names the input. Without a `Field`, give `NumberFieldInput` an accessible name of its own.
- Set `min` and `max` whenever the value has bounds. Stepping stops at them, and an out-of-range typed value is clamped when the input loses focus.
- For whole numbers such as counts and limits, set `smallStep` to the `step` value. The default `smallStep` of 0.1 lets `Alt` + arrow keys produce decimals.
- Show the unit with `NumberFieldUnit` instead of putting it in the label or the value. The unit is visual only and is not part of the input's name or description, so state the unit and the bounds in a `FieldDescription` as well. Use `format` for locale formatting such as decimals, percentages or currency.
- Pass the same `size` to `NumberFieldGroup`, `NumberFieldInput`, `NumberFieldUnit` and the stepper buttons. `medium` is the default; `large` matches large inputs.
- The stepper buttons are named "Increment value" and "Decrement value" by default. Pass a localized `aria-label` or `aria-labelledby` in translated products.
- Number fields that save together belong in a real form boundary: [`Form`](/components/form.md) or a native `<form>`, with a `Button` of `type="submit"`.

## Anatomy

```tsx
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
  NumberFieldUnit,
} from '@langgenius/dify-ui/number-field'
```

```tsx
<Field>
  <FieldLabel />
  <NumberField>
    <NumberFieldGroup>
      <NumberFieldInput />
      <NumberFieldUnit />
      <NumberFieldControls>
        <NumberFieldIncrement />
        <NumberFieldDecrement />
      </NumberFieldControls>
    </NumberFieldGroup>
  </NumberField>
  <FieldDescription />
  <FieldError />
</Field>
```

`NumberFieldGroup` draws the input surface. `NumberFieldUnit` and `NumberFieldControls` are optional.

## Examples

### Sizes and units

`NumberFieldUnit` sits between the value and the steppers. Each `FieldDescription` repeats the unit and range for assistive technology. The large size uses more padding and a larger value text.

```tsx title="sizes.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
  NumberFieldUnit,
} from '@langgenius/dify-ui/number-field'

export function NumberFieldSizesDemo() {
  return (
    <div className="grid w-full max-w-80 gap-3">
      <Field name="timeout">
        <div className="flex items-baseline justify-between gap-2">
          <FieldLabel>Request timeout</FieldLabel>
          <span aria-hidden="true" className="system-xs-regular text-text-tertiary">
            medium
          </span>
        </div>
        <NumberField defaultValue={30} min={1} max={600} smallStep={1}>
          <NumberFieldGroup>
            <NumberFieldInput placeholder="e.g. 30" />
            <NumberFieldUnit>seconds</NumberFieldUnit>
            <NumberFieldControls>
              <NumberFieldIncrement />
              <NumberFieldDecrement />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>From 1 to 600 seconds.</FieldDescription>
      </Field>
      <Field name="maxTokens">
        <div className="flex items-baseline justify-between gap-2">
          <FieldLabel>Max tokens</FieldLabel>
          <span aria-hidden="true" className="system-xs-regular text-text-tertiary">
            large
          </span>
        </div>
        <NumberField defaultValue={512} min={1} max={4096} step={32} smallStep={32}>
          <NumberFieldGroup size="large">
            <NumberFieldInput size="large" placeholder="e.g. 512" />
            <NumberFieldUnit size="large">tokens</NumberFieldUnit>
            <NumberFieldControls>
              <NumberFieldIncrement size="large" />
              <NumberFieldDecrement size="large" />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>Up to 4,096 tokens per response.</FieldDescription>
      </Field>
    </div>
  )
}
```

### States

Set `invalid` on the `Field` when application code owns the validity, and render `<FieldError match>` while it is invalid; bringing the value back into range clears the error. Set `disabled` on `Field` to disable the label, input and steppers together, and `readOnly` on `NumberField` so the value stays focusable while the steppers stop.

```tsx title="states.tsx"
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
  NumberFieldUnit,
} from '@langgenius/dify-ui/number-field'
import * as React from 'react'

export function NumberFieldStatesDemo() {
  const [threshold, setThreshold] = React.useState<number | null>(120)
  const invalidThreshold = threshold === null || threshold < 0 || threshold > 100

  return (
    <div className="grid w-full max-w-80 gap-3">
      <Field name="scoreThreshold" invalid={invalidThreshold}>
        <FieldLabel>Score threshold</FieldLabel>
        <NumberField value={threshold} onValueChange={setThreshold} min={0} max={100} smallStep={1}>
          <NumberFieldGroup>
            <NumberFieldInput placeholder="e.g. 80" />
            <NumberFieldUnit>%</NumberFieldUnit>
            <NumberFieldControls>
              <NumberFieldIncrement />
              <NumberFieldDecrement />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>Minimum match percentage, from 0 to 100%.</FieldDescription>
        {invalidThreshold && <FieldError match>Use a percentage from 0 to 100.</FieldError>}
      </Field>
      <Field name="rateLimit">
        <FieldLabel>Rate limit</FieldLabel>
        <NumberField defaultValue={60} smallStep={1} readOnly>
          <NumberFieldGroup>
            <NumberFieldInput />
            <NumberFieldUnit>requests/min</NumberFieldUnit>
            <NumberFieldControls>
              <NumberFieldIncrement />
              <NumberFieldDecrement />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>Workspace quota in requests per minute (read-only).</FieldDescription>
      </Field>
      <Field name="maxIterations" disabled>
        <FieldLabel>Max iterations</FieldLabel>
        <NumberField defaultValue={5} min={1} max={10} smallStep={1}>
          <NumberFieldGroup>
            <NumberFieldInput />
            <NumberFieldControls>
              <NumberFieldIncrement />
              <NumberFieldDecrement />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>Managed by the workspace owner.</FieldDescription>
      </Field>
    </div>
  )
}
```

### Step sizes and formatting

`step` sets the amount for the buttons and arrow keys. `smallStep` and `largeStep` set the amounts used with modifier keys. `format` takes `Intl.NumberFormat` options. The mouse wheel does not change the value unless `allowWheelScrub` is set, and then only while the input has focus.

```tsx title="steps.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
} from '@langgenius/dify-ui/number-field'

export function NumberFieldStepsDemo() {
  return (
    <Field name="temperature" className="w-full max-w-80">
      <FieldLabel>Temperature</FieldLabel>
      <NumberField
        defaultValue={0.7}
        min={0}
        max={2}
        step={0.1}
        smallStep={0.01}
        largeStep={0.5}
        allowWheelScrub
        format={{ maximumFractionDigits: 2 }}
      >
        <NumberFieldGroup>
          <NumberFieldInput placeholder="e.g. 0.7" />
          <NumberFieldControls>
            <NumberFieldIncrement />
            <NumberFieldDecrement />
          </NumberFieldControls>
        </NumberFieldGroup>
      </NumberField>
      <FieldDescription>From 0 to 2. Higher values make responses more varied.</FieldDescription>
    </Field>
  )
}
```

### Validation in a form

A `required` number field shows its `valueMissing` message when the form is submitted empty. A typed value outside `min` and `max` is clamped when the input loses focus, so the field needs no range messages.

```tsx title="validation.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
} from '@langgenius/dify-ui/number-field'
import * as React from 'react'

type RetrievalSettingsValues = {
  chunkSize: number
}

export function NumberFieldValidationDemo() {
  const [savedChunkSize, setSavedChunkSize] = React.useState<number | null>(null)

  return (
    <Form<RetrievalSettingsValues>
      aria-label="Retrieval settings"
      className="grid w-full max-w-80 gap-4"
      onChange={() => {
        if (savedChunkSize !== null) setSavedChunkSize(null)
      }}
      onFormSubmit={(values) => setSavedChunkSize(values.chunkSize)}
    >
      <Field name="chunkSize">
        <FieldLabel>Maximum chunk length</FieldLabel>
        <NumberField
          required
          min={100}
          max={4000}
          smallStep={1}
          onValueChange={() => {
            if (savedChunkSize !== null) setSavedChunkSize(null)
          }}
        >
          <NumberFieldGroup>
            <NumberFieldInput placeholder="e.g. 1024" />
            <NumberFieldControls>
              <NumberFieldIncrement />
              <NumberFieldDecrement />
            </NumberFieldControls>
          </NumberFieldGroup>
        </NumberField>
        <FieldDescription>Characters per chunk, from 100 to 4000.</FieldDescription>
        <FieldError match="valueMissing">Maximum chunk length is required.</FieldError>
      </Field>
      <div className="flex items-center justify-between gap-3">
        <span
          role="status"
          className="min-w-0 truncate system-xs-regular text-text-success tabular-nums"
        >
          {savedChunkSize !== null ? `Saved (${savedChunkSize} characters)` : ''}
        </span>
        <Button type="submit" variant="primary" className="shrink-0">
          Save
        </Button>
      </div>
    </Form>
  )
}
```

## Keyboard

| Key                                        | Action                                                                                                                       |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| `ArrowUp`, `ArrowDown`                     | Increases or decreases the value by `step`, stopping at `min` and `max`.                                                     |
| `Shift` + `ArrowUp`, `Shift` + `ArrowDown` | Steps by `largeStep` (10 by default).                                                                                        |
| `Alt` + `ArrowUp`, `Alt` + `ArrowDown`     | Steps by `smallStep` (0.1 by default).                                                                                       |
| `Home`, `End`                              | Sets the value to `min` or `max`, when that bound is set.                                                                    |
| `Tab`                                      | Moves focus out of the input and commits a typed value, clamped to the bounds. The stepper buttons are not in the tab order. |

## API reference

### NumberField

Groups all parts of a numeric input with increment and decrement controls. Renders a `<div>` element.

Inherits `id`, `min`, `max`, `allowOutOfRange`, `smallStep`, `step`, `largeStep`, `required`, `disabled`, `readOnly`, `name`, `form`, `value`, `defaultValue`, `allowWheelScrub`, `snapOnStep`, `format`, `onValueChange`, `onValueCommitted`, `locale`, `inputRef`, `className`, `render`, `style` from [Base UI NumberField.Root](https://base-ui.com/react/components/number-field#root).

**Data attributes**

| Attribute        | Description                                                                        |
| ---------------- | ---------------------------------------------------------------------------------- |
| `data-scrubbing` | Present while scrubbing.                                                           |
| `data-disabled`  | Present when the number field is disabled.                                         |
| `data-readonly`  | Present when the number field is readonly.                                         |
| `data-required`  | Present when the number field is required.                                         |
| `data-valid`     | Present when the number field is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the number field is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the number field has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the number field's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the number field is filled (when wrapped in Field.Root).              |
| `data-focused`   | Present when the number field is focused (when wrapped in Field.Root).             |

### NumberFieldControls

Accepts the props of the element it renders.

### NumberFieldDecrement

A stepper button that decreases the field value when clicked. Renders a `<button>` element.

**Props**

| Prop    | Type                  | Default    | Description                                                      |
| ------- | --------------------- | ---------- | ---------------------------------------------------------------- |
| `size?` | `'large' \| 'medium'` | `'medium'` | The size of the button. Match the size of the surrounding group. |

Inherits `className`, `render`, `style`, `nativeButton` from [Base UI NumberField.Decrement](https://base-ui.com/react/components/number-field#decrement).

**Data attributes**

| Attribute        | Description                                                                        |
| ---------------- | ---------------------------------------------------------------------------------- |
| `data-scrubbing` | Present while scrubbing.                                                           |
| `data-disabled`  | Present when the number field is disabled.                                         |
| `data-readonly`  | Present when the number field is readonly.                                         |
| `data-required`  | Present when the number field is required.                                         |
| `data-valid`     | Present when the number field is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the number field is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the number field has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the number field's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the number field is filled (when wrapped in Field.Root).              |
| `data-focused`   | Present when the number field is focused (when wrapped in Field.Root).             |

### NumberFieldGroup

Groups the input with the increment and decrement buttons. Renders a `<div>` element.

**Props**

| Prop    | Type                  | Default    | Description                                                                |
| ------- | --------------------- | ---------- | -------------------------------------------------------------------------- |
| `size?` | `'large' \| 'medium'` | `'medium'` | The corner radius of the group. Pass the same size to the parts inside it. |

Inherits `className`, `render`, `style` from [Base UI NumberField.Group](https://base-ui.com/react/components/number-field#group).

**Data attributes**

| Attribute        | Description                                                                        |
| ---------------- | ---------------------------------------------------------------------------------- |
| `data-scrubbing` | Present while scrubbing.                                                           |
| `data-disabled`  | Present when the number field is disabled.                                         |
| `data-readonly`  | Present when the number field is readonly.                                         |
| `data-required`  | Present when the number field is required.                                         |
| `data-valid`     | Present when the number field is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the number field is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the number field has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the number field's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the number field is filled (when wrapped in Field.Root).              |
| `data-focused`   | Present when the number field is focused (when wrapped in Field.Root).             |

### NumberFieldIncrement

A stepper button that increases the field value when clicked. Renders a `<button>` element.

**Props**

| Prop    | Type                  | Default    | Description                                                      |
| ------- | --------------------- | ---------- | ---------------------------------------------------------------- |
| `size?` | `'large' \| 'medium'` | `'medium'` | The size of the button. Match the size of the surrounding group. |

Inherits `className`, `render`, `style`, `nativeButton` from [Base UI NumberField.Increment](https://base-ui.com/react/components/number-field#increment).

**Data attributes**

| Attribute        | Description                                                                        |
| ---------------- | ---------------------------------------------------------------------------------- |
| `data-scrubbing` | Present while scrubbing.                                                           |
| `data-disabled`  | Present when the number field is disabled.                                         |
| `data-readonly`  | Present when the number field is readonly.                                         |
| `data-required`  | Present when the number field is required.                                         |
| `data-valid`     | Present when the number field is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the number field is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the number field has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the number field's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the number field is filled (when wrapped in Field.Root).              |
| `data-focused`   | Present when the number field is focused (when wrapped in Field.Root).             |

### NumberFieldInput

The native input control in the number field. Renders an `<input>` element.

**Props**

| Prop    | Type                  | Default    | Description                                                                       |
| ------- | --------------------- | ---------- | --------------------------------------------------------------------------------- |
| `size?` | `'large' \| 'medium'` | `'medium'` | The padding and type scale of the input. Match the size of the surrounding group. |

Inherits `className`, `render`, `style`, `aria-roledescription` from [Base UI NumberField.Input](https://base-ui.com/react/components/number-field#input).

**Data attributes**

| Attribute        | Description                                                                        |
| ---------------- | ---------------------------------------------------------------------------------- |
| `data-scrubbing` | Present while scrubbing.                                                           |
| `data-disabled`  | Present when the number field is disabled.                                         |
| `data-readonly`  | Present when the number field is readonly.                                         |
| `data-required`  | Present when the number field is required.                                         |
| `data-valid`     | Present when the number field is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`   | Present when the number field is in an invalid state (when wrapped in Field.Root). |
| `data-touched`   | Present when the number field has been touched (when wrapped in Field.Root).       |
| `data-dirty`     | Present when the number field's value has changed (when wrapped in Field.Root).    |
| `data-filled`    | Present when the number field is filled (when wrapped in Field.Root).              |
| `data-focused`   | Present when the number field is focused (when wrapped in Field.Root).             |

### NumberFieldUnit

**Props**

| Prop    | Type                  | Default    | Description                                                           |
| ------- | --------------------- | ---------- | --------------------------------------------------------------------- |
| `size?` | `'large' \| 'medium'` | `'medium'` | The end padding of the unit. Match the size of the surrounding group. |
