# OtpField

> One-time code input from @salmonui/inputs/otp-field - one native input drawn as cells, with SMS autofill, paste, masking, separators and onComplete.

Canonical: https://salmonui.dev/docs/inputs/otp-field

`OtpField` is a one-time code input. It is one native
`<input autocomplete="one-time-code">` with `maxLength={length}` laid over
cells drawn from the value and the caret, so SMS autofill, password
managers, paste and screen readers all see a single field.

```tsx
import { OtpField } from '@salmonui/inputs/otp-field';
import '@salmonui/inputs/otp-field/styles.css';

<OtpField name="code" label="Verification code" description="6 digits from the SMS" length={6} separator="-" groupSize={3} onComplete={verify} />
```

### Live demo: OtpField basics

Live demo: https://salmonui.dev/demos/inputs/otp-field-basics

A six-digit code with a separator and onComplete, and a masked PIN.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { OtpField } from '@salmonui/inputs/otp-field';

// A six-digit code in two groups with onComplete, and a masked four-digit PIN.
export default function OtpFieldBasicsDemo() {
  const [status, setStatus] = useState('Waiting for the code');
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <OtpField label="Verification code" description="6 digits from the SMS" length={6} separator="-" groupSize={3} onValueChange={() => setStatus('Typing')} onComplete={(code) => setStatus(`Verifying ${code}`)} />
      <output>{status}</output>
      <OtpField label="PIN" mask="●" placeholder="○" />
    </div>
  );
}
```

## Value

`value` / `defaultValue` / `onValueChange(value, details)` with a string;
`details.reason` is `'input'` (typing or autofill), `'paste'` or
`'reset'`. `onComplete(value)` fires when an edit fills every cell.
`length` (default 4) is the number of cells. Controlled with
`value`, uncontrolled with `defaultValue` (restored by a form reset); with
`name` the native input submits the code.

## Characters

- `type: 'numeric' | 'alphanumeric' | 'text'` (default `'numeric'`) sets
  `inputmode` and `pattern` and drops other characters. Numeric
  codes are laid out left to right even in right-to-left content.
- `transform: 'uppercase' | 'lowercase' | 'none'` normalizes letters in the
  value.
- `mask` hides characters: the input becomes `type="password"` and the cells
  show `•` (or the given character).

## Cells

- `placeholder`: one character repeated in every empty cell, or one
  character per cell.
- `separator` (`ReactNode`) between groups of `groupSize` cells (default 1
  when a separator is given).
- The active cell follows the caret: arrow keys, Home and End move it, and a
  click on a filled cell selects its character so typing replaces it.

## Paste and autofill

A pasted code replaces the whole value after filtering (spaces and dashes in
"123 456" are dropped) and fills every cell; one-time-code autofill writes
the input directly. Both fire `onComplete` when the code is full.

## Validation

A partly filled code is invalid with `localeText.incomplete` ("Enter all {0}
characters"), shown after blur like every field; `required` and the
[Form](https://salmonui.dev/docs/inputs/form) rules apply as usual. `autoFocus` is native.

### Live demo: One-time code options

Live demo: https://salmonui.dev/demos/inputs/otp-field-options

OtpField with a controlled value the owner fills or clears, per-cell placeholders, an uppercase, lowercase or unchanged transform, and autoFocus when the code step appears.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { OtpField } from '@salmonui/inputs/otp-field';
import { Select } from '@salmonui/dropdowns/select';

type Transform = 'uppercase' | 'lowercase' | 'none';

// A controlled code the owner can fill or clear, cell placeholders, a case
// transform for alphanumeric codes, and autoFocus when the step appears.
export default function OtpFieldOptionsDemo() {
  const [code, setCode] = useState('');
  const [transform, setTransform] = useState<Transform>('uppercase');
  const [voucher, setVoucher] = useState('');
  const [sent, setSent] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <OtpField label="Login code" length={6} placeholder="000000" value={code} onValueChange={setCode} description='placeholder="000000" fills the empty cells' />
      <div className="demo-toolbar" style={{ marginBlockEnd: 0 }}>
        <Button type="button" variant="outline" onClick={() => setCode('482915')}>
          Fill from SMS
        </Button>
        <Button type="button" variant="outline" onClick={() => setCode('')}>
          Clear
        </Button>
        <output>Value: {JSON.stringify(code)}</output>
      </div>
      <div className="demo-toolbar" style={{ marginBlockEnd: 0 }}>
        <label>
          Transform
          <Select aria-label="Transform" data={['uppercase', 'lowercase', 'none']} value={transform} onValueChange={(value) => value != null && setTransform(value as Transform)} />
        </label>
      </div>
      <OtpField label="Voucher" type="alphanumeric" length={5} transform={transform} value={voucher} onValueChange={setVoucher} description="Type letters: they are stored in the chosen case" />
      <output>Value: {JSON.stringify(voucher)}</output>
      {sent ? (
        <OtpField label="Confirmation code" length={4} autoFocus description="Mounted with autoFocus: start typing right away" />
      ) : (
        <Button type="button" variant="outline" onClick={() => setSent(true)} style={{ justifySelf: 'start' }}>
          Send confirmation code
        </Button>
      )}
    </div>
  );
}
```

## Keyboard

| Key | Action |
| --- | --- |
| Characters | Insert at the caret, up to `length`; with a selected character (click its cell), replace it |
| Backspace / Delete | Remove the character before / after the caret |
| Arrow Left / Arrow Right, Home / End | Move the active cell |
| Paste (Ctrl+V / Cmd+V) | Replaces the whole code |

Screen readers hear one textbox named by the label and described by the
description. Try it in the
[keyboard demo](https://salmonui.dev/demos/inputs/inputs-keyboard).

## SSR

The cells are drawn from the value, so the server markup shows the
`defaultValue` and placeholders; the native input works (and SMS autofill
fills it) before hydration.

## Design notes

- the cells have one outlined look: the theme keeps one look per
  component, so there is no styling variant.
- there are no per-cell labels: the field is one native input with
  one accessible name, so per-cell labels would describe elements that do
  not exist. Put the instructions in one `label` and `description`
  ("6-digit code from the SMS").

## API

| Prop | Type | Default |
| --- | --- | --- |
| `value`, `defaultValue` | `string` | `''` |
| `onValueChange` | `(value, { source, reason }) => void` | |
| `onComplete` | `(value) => void` | |
| `length` | `number` | `4` |
| `type` | `'numeric' \| 'alphanumeric' \| 'text'` | `'numeric'` |
| `mask` | `boolean \| string` | `false` |
| `placeholder` | `string` | |
| `separator` | `ReactNode` | |
| `groupSize` | `number` | `1` with a separator |
| `transform` | `'uppercase' \| 'lowercase' \| 'none'` | `'none'` |
| `label`, `description`, `errorMessage`, `invalid`, `validation`, `validateOn` | see [Field](https://salmonui.dev/docs/inputs/field) | |
| `localeText` | `{ incomplete, validation* }` | English |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` |
| `classNames` | `{ cells, cell, input, label, description, error }` | |
| `ref` | `HTMLInputElement` | |

`normalizeOtp(raw, length, type?, transform?)` is exported for server-side
checks of submitted codes. Full signatures:
[`@salmonui/inputs/otp-field` API reference](https://salmonui.dev/docs/reference/inputs/otp-field).
