# OTP Field

A segmented one-time-password input with paste and keyboard support.

A segmented one-time-password input with paste and keyboard support.

<p className="zenos-base-ui-source">
  Backed by <a href="https://base-ui.com/react/components/otp-field">Base UI OTP Field</a> for
  upstream behavior and configuration. Zenos owns the public API, styling, and composition shown on
  this page.
</p>

```tsx
import { Field, OTPField } from "@zentara-co/zenos-ui";

function OTPFieldPreviewExample() {
  return (
    <Field.Root className="w-full max-w-sm">
      <Field.Label>Verification code</Field.Label>
      <OTPField length={6} />
    </Field.Root>
  );
}
```

```tsx
import { OTPField } from "@zentara-co/zenos-ui";
```

## Variants

Each slot uses the same surfaces as Input: `default` (standard), `filled` (subtle), and `none` (borderless). Set `variant` on the root or on individual `OTPField.Input` nodes when composing groups.

```tsx
import { Field, OTPField, Stack } from "@zentara-co/zenos-ui";

type CatalogVariantProps = { limit?: number };

const limited = <T,>(items: readonly T[], limit?: number) =>
  limit === undefined ? items : items.slice(0, limit);

const formControlSurfaceVariants = ["default", "filled", "none"] as const;

function OTPFieldVariantsExample({ limit }: CatalogVariantProps = {}) {
  if (limit !== undefined) {
    return (
      <Stack gap="4" className="w-full max-w-sm">
        {limited(formControlSurfaceVariants, limit).map((variant) => (
          <Field.Root key={variant}>
            <Field.Label className="capitalize">{variant}</Field.Label>
            <OTPField length={4} variant={variant} defaultValue="12" />
          </Field.Root>
        ))}
      </Stack>
    );
  }

  return (
    <Stack gap="4" className="w-full max-w-sm">
      {formControlSurfaceVariants.map((variant) => (
        <Field.Root key={variant}>
          <Field.Label className="capitalize">{variant}</Field.Label>
          <OTPField length={4} variant={variant} defaultValue="1234" />
        </Field.Root>
      ))}
    </Stack>
  );
}
```

## States

The root forwards `disabled`, `required`, and Field invalid semantics to every slot. Invalid styling matches Input.

```tsx
import { Field, OTPField, Stack } from "@zentara-co/zenos-ui";

function OTPFieldStatesExample() {
  return (
    <Stack gap="4" className="w-full max-w-sm">
      <Field.Root>
        <Field.Label>Empty</Field.Label>
        <OTPField length={4} />
      </Field.Root>
      <Field.Root>
        <Field.Label>Filled</Field.Label>
        <OTPField length={4} defaultValue="1234" />
      </Field.Root>
      <Field.Root invalid>
        <Field.Label>Invalid</Field.Label>
        <OTPField length={4} defaultValue="0000" />
        <Field.Error>The verification code has expired.</Field.Error>
      </Field.Root>
      <Field.Root disabled>
        <Field.Label>Disabled</Field.Label>
        <OTPField length={4} defaultValue="1234" />
      </Field.Root>
    </Stack>
  );
}
```

## Length and grouping

Use the shorthand root when every slot is identical. Compose `Group`, `Input`, and `Separator` when the code has meaningful sections.

```tsx
import { Field, OTPField, Stack } from "@zentara-co/zenos-ui";

function OTPFieldPatternsExample() {
  return (
    <Stack gap="5" className="w-full max-w-lg">
      <Field.Root>
        <Field.Label>Four-digit PIN</Field.Label>
        <OTPField length={4} />
      </Field.Root>
      <Field.Root>
        <Field.Label>Recovery code</Field.Label>
        <OTPField.Root length={6} variant="filled">
          <OTPField.Group>
            <OTPField.Input variant="filled" />
            <OTPField.Input variant="filled" />
            <OTPField.Input variant="filled" />
          </OTPField.Group>
          <OTPField.Separator />
          <OTPField.Group>
            <OTPField.Input variant="filled" />
            <OTPField.Input variant="filled" />
            <OTPField.Input variant="filled" />
          </OTPField.Group>
        </OTPField.Root>
      </Field.Root>
    </Stack>
  );
}
```

## Controlled value

```tsx
import { Field, OTPField } from "@zentara-co/zenos-ui";
import { useState } from "react";

function OTPFieldControlledExample() {
  const [code, setCode] = useState("");
  return (
    <Field.Root className="w-full max-w-sm" invalid={code.length > 0 && code.length < 6}>
      <Field.Label>Verification code</Field.Label>
      <OTPField length={6} value={code} onValueChange={setCode} />
      <Field.Description>{code.length} of 6 characters entered.</Field.Description>
      {code.length > 0 && code.length < 6 ? (
        <Field.Error>Enter all six characters.</Field.Error>
      ) : null}
    </Field.Root>
  );
}
```

## Variants

default, filled, none

## Anatomy

```text
OTPField.Root
OTPField.Input
OTPField.Group
OTPField.Separator
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | string | — | Controlled OTP value. |
| defaultValue | string | — | Initial uncontrolled value. |
| onValueChange | (value: string) => void | — | Runs when digits change. |
| length | number | — | Number of generated inputs. |
| disabled | boolean | false | Disables entry. |
| required | boolean | false | Marks the code as required. |
| variant | default \| filled \| none | default | Surface treatment per slot (`default` = standard, `filled` = subtle, `none` = borderless). |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

OTPField preserves Base UI paste, focus, and keyboard behavior. Label the root through `Field`, set the expected `length`, and avoid implementing per-slot focus movement yourself. Separators are presentation-only and are hidden from assistive technology.

Source: https://zenos-ui.pages.dev/components/otp-field/