# OTP Input

Capture one-time passwords with a series of individual input fields.

```tsx
import { Otp } from '@fluxui-dev/react';

<Otp length={6} />
```

## Example usage

Display a form for verifying one-time passwords.

```tsx
import { Button, Card, Field, Heading, Otp, Text } from '@fluxui-dev/react';
import { useState } from 'react';

const [code, setCode] = useState('');
const [error, setError] = useState<string>();

<Card>
    <form
        className="space-y-8"
        onSubmit={(event) => {
            event.preventDefault();
            setError(code.length === 6 ? undefined : 'The code field must be 6 characters.');
        }}
    >
        <div className="mx-auto max-w-64 space-y-2">
            <Heading size="lg" className="text-center">Verify your account</Heading>
            <Text className="text-center">Please enter a one-time password from the authenticator app.</Text>
        </div>

        <Field error={error}>
            <Field.Label srOnly>OTP Code</Field.Label>
            <Otp value={code} onValueChange={setCode} length={6} className="mx-auto" />
            <Field.Error icon={null} className="text-center" />
        </Field>

        <div className="space-y-4">
            <Button variant="primary" type="submit" className="w-full">Verify</Button>
            <Button className="w-full">Resend code</Button>
        </div>
    </form>
</Card>
```

## Autosubmit

Use the `submit="auto"` prop to automatically submit the form once all input fields are filled.

```tsx
const [code, setCode] = useState('');

<form
    className="space-y-8"
    onSubmit={(event) => {
        event.preventDefault();
        setCode('');
    }}
>
    <div className="mx-auto max-w-64 space-y-2">
        <Heading size="lg" className="text-center">Verify your account</Heading>
        <Text className="text-center">Please enter a one-time password from the authenticator app.</Text>
    </div>

    <div className="space-y-6">
        <Otp value={code} onValueChange={setCode} length={6} submit="auto" className="mx-auto" />
    </div>
</form>
```

## Alphanumeric

Allow non-numeric characters by setting the `mode` to `alphanumeric` or `alpha`.

```tsx
<Otp length={10} mode="alphanumeric" label="License key" descriptionTrailing="Enter the license key printed on the installation disc" />
```

## Private

To mask sensitive input values, use the `private` prop.

```tsx
<Otp length={4} private label="PIN Code" />
```

## Separator

Use children to render input fields with a separator in between them.

```tsx
<Otp>
    <Otp.Input />
    <Otp.Input />
    <Otp.Input />
    <Otp.Separator />
    <Otp.Input />
    <Otp.Input />
    <Otp.Input />
</Otp>
```

## Group

Group all input fields together using the `Otp.Group` component.

```tsx
<Otp>
    <Otp.Group>
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
    </Otp.Group>
</Otp>
```

## Group separator

Add a separator between groups of input fields using the `Otp.Separator` component.

```tsx
<Otp>
    <Otp.Group>
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
    </Otp.Group>

    <Otp.Separator />

    <Otp.Group>
        <Otp.Input />
        <Otp.Input />
        <Otp.Input />
    </Otp.Group>
</Otp>
```

## Reference

### Otp

**Prop:**

- `value` - The current value as a string. Pair with `onValueChange`.
- `defaultValue` - Initial value of the code when uncontrolled.
- `onValueChange` - Callback fired with the new code string whenever it changes.
- `onValueComplete` - Callback fired with the code once every cell is filled.
- `length` - The number of input fields to display.
- `mode` - The input mode. Can be `numeric` (default), `alphanumeric`, or `alpha`. Letters are uppercased.
- `private` - Masks the input values, as a password input does.
- `submit` - Keyboard behavior for form submission. Options: (default), `auto` to submit when all fields are filled.
- `name` - Name under which the code is submitted with the enclosing form.
- `autoComplete` - The `autocomplete` attribute for the code. Use `off` to disable browser autofill. Default: `one-time-code`.
- `autoFocus` - Focuses the first empty cell when the component mounts.
- `invalid` - Styles every cell as invalid and sets `aria-invalid`. Passing an `error` does the same.
- `disabled` - If `true`, prevents interaction with every cell.
- `readOnly` - If `true`, the code can be focused but not changed.
- `label` - Label text shown above the cells, wrapping `Otp` in a `Field`.
- `description` - Description text shown between the label and the cells.
- `descriptionTrailing` - Description text shown below the cells.
- `error` - Error message shown below the cells, marking them invalid and defaulting to the validation error for the Otp's `name`.
- `children` - Custom input fields and separators. When used, the `length` prop is ignored.
- `className` - CSS classes applied to the row of cells. With `label` set, they stay on the row, below the label; to space the whole field, [write the `Field` yourself](https://fluxui.dev/react/components/field#spacing-a-field) and pass it `className`.

**Attribute:**

- `data-flux-otp` - Applied to the element that holds the cells.

### Otp.Input

**CSS:**

- `className` - CSS classes applied to the cell's wrapper element.
- `inputClassName` - CSS classes applied to the cell's `input` element.

**Attribute:**

- `data-flux-otp-input` - Applied to each cell's `input` element.

### Otp.Separator

**Prop:**

- `children` - Content shown in place of the default em dash.

### Otp.Group

**Slot:**

- `children` - The input fields to include in the group.
