# Input OTP

> Enters a one-time code in six boxes.

An OTP input takes a one-time code, such as an email verification or a sign-in code, in six separate boxes.

## When to use

Use it for every one-time code in every Sosialoka product, with these rules:

- **Six separate boxes**, never one text field.
- **Digits only.** Pasting the whole code from the email fills every box.
- **Send it when it is complete.** When the sixth digit is in, submit the code without a click. Keep the submit button too, disabled until the code is complete, and guard the submit so it runs once.
- **Clear it when the backend refuses the code**, so the caret goes back to the first box; show the backend's message below it.
- **Focus the first box** when the screen or step appears.
- Put the label above the boxes as a `p` element, centered with its hint.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/input-otp/_

## Anatomy

1. Filled box
2. Focused box
3. Empty box

Six 48px boxes (`size-12`) on `radius-md` with an `input` border, 8px apart, in `text-lg`. The active box shows the `ring`; a refused code turns every border `destructive`. One real input sits behind the boxes, which is what makes typing, deleting and pasting work.

## Variants

There is one style, for six digits.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `maxLength` | `maxlength` | `maxLength` | number | none | The number of digits: `6`. |
| `pattern={REGEXP_ONLY_DIGITS}` | `:pattern="REGEXP_ONLY_DIGITS"` | `FilteringTextInputFormatter.digitsOnly` | pattern | any character | Accepts digits only. |
| `value` / `onChange` | `v-model` | `onChanged` | text | `""` | The code typed so far. |
| `onComplete` | `@complete` | check the length in `onChanged` | function | none | Runs when the last box is filled: submit here. |
| `aria-invalid` | `aria-invalid` | an error below | boolean | `false` | Marks a refused code. |

## Code

**React**
```tsx
import { useId } from "react"
import { REGEXP_ONLY_DIGITS } from "input-otp"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp"

export default function InputOtpDemo() {
  const id = useId()
  return (
    <div className="grid justify-items-center gap-2 text-center">
      {/* One real input sits behind the six boxes, so the label points at it through aria-labelledby. */}
      <p id={`${id}-label`} className="text-sm font-medium">Kode verifikasi</p>
      <InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS} autoFocus aria-labelledby={`${id}-label`} aria-describedby={`${id}-hint`}>
        <InputOTPGroup>
          {[0, 1, 2, 3, 4, 5].map((index) => (
            <InputOTPSlot key={index} index={index} />
          ))}
        </InputOTPGroup>
      </InputOTP>
      <p id={`${id}-hint`} className="text-sm text-muted-foreground">Kami kirim kodenya ke email kamu.</p>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { useId } from "vue"
import { REGEXP_ONLY_DIGITS } from "vue-input-otp"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp"

const id = useId()
</script>

<template>
  <div class="grid justify-items-center gap-2 text-center">
    <!-- One real input sits behind the six boxes, so the label points at it through aria-labelledby. -->
    <p :id="`${id}-label`" class="text-sm font-medium">Kode verifikasi</p>
    <InputOTP :maxlength="6" :pattern="REGEXP_ONLY_DIGITS" autofocus :aria-labelledby="`${id}-label`" :aria-describedby="`${id}-hint`">
      <InputOTPGroup>
        <InputOTPSlot v-for="index in 6" :key="index" :index="index - 1" />
      </InputOTPGroup>
    </InputOTP>
    <p :id="`${id}-hint`" class="text-sm text-muted-foreground">Kami kirim kodenya ke email kamu.</p>
  </div>
</template>
```

**Flutter**
```dart
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class InputOtpDemo extends StatelessWidget {
  const InputOtpDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return Column(
      mainAxisSize: MainAxisSize.min,
      spacing: 8,
      children: [
        Text('Kode verifikasi', style: theme.textTheme.small),
        // Six separate 48px boxes: one slot per group, 8px apart.
        ShadInputOTP(
          maxLength: 6,
          gap: 8,
          keyboardType: TextInputType.number,
          inputFormatters: [FilteringTextInputFormatter.digitsOnly],
          onChanged: (code) {},
          children: [
            for (var i = 0; i < 6; i++)
              const ShadInputOTPGroup(children: [ShadInputOTPSlot(width: 48, height: 48)]),
          ],
        ),
        Text('Kami kirim kodenya ke email kamu.', style: theme.textTheme.muted),
      ],
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/input-otp.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/input-otp.json
```

## Accessibility

- The real input behind the boxes is named by the label with `aria-labelledby` and tied to the hint with `aria-describedby`.
- It uses `autocomplete="one-time-code"`, so phones can offer the code from a message.
- Backspace clears the previous box; the arrow keys move between boxes.
