0.04K

Accessible one-time password component with copy paste functionality.

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from '@/components/ui/input-otp';

<template>
  <InputOTP @maxLength={{6}}>
    <InputOTPGroup>
      <InputOTPSlot @index={{0}} />
      <InputOTPSlot @index={{1}} />
      <InputOTPSlot @index={{2}} />
    </InputOTPGroup>
    <InputOTPSeparator />
    <InputOTPGroup>
      <InputOTPSlot @index={{3}} />
      <InputOTPSlot @index={{4}} />
      <InputOTPSlot @index={{5}} />
    </InputOTPGroup>
  </InputOTP>
</template>

Installation

pnpm dlx shadcn-ember@latest add input-otp

Usage

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from '@/components/ui/input-otp';
<InputOTP @maxLength={{6}}>
  <InputOTPGroup>
    <InputOTPSlot @index={{0}} />
    <InputOTPSlot @index={{1}} />
    <InputOTPSlot @index={{2}} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot @index={{3}} />
    <InputOTPSlot @index={{4}} />
    <InputOTPSlot @index={{5}} />
  </InputOTPGroup>
</InputOTP>

Examples

Pattern

Use a custom pattern to validate the OTP input.

The component uses a built-in pattern that only allows alphanumeric characters (letters and numbers).

Separator

You can use the <InputOTPSeparator /> component to add a separator between the input groups.

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from '@/components/ui/input-otp';

<template>
  <InputOTP @maxLength={{4}}>
    <InputOTPGroup>
      <InputOTPSlot @index={{0}} />
      <InputOTPSlot @index={{1}} />
    </InputOTPGroup>
    <InputOTPSeparator />
    <InputOTPGroup>
      <InputOTPSlot @index={{2}} />
      <InputOTPSlot @index={{3}} />
    </InputOTPGroup>
  </InputOTP>
</template>

Controlled

You can use the @value and @onValueChange arguments to control the input value.

Enter your one-time password.
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSlot,
} from '@/components/ui/input-otp';

export default class InputOtpControlled extends Component {
  @tracked value = '';

  handleValueChange = (value: string) => {
    this.value = value;
  };

  <template>
    <div class="space-y-2">
      <InputOTP
        @maxLength={{6}}
        @onValueChange={{this.handleValueChange}}
        @value={{this.value}}
      >
        <InputOTPGroup>
          <InputOTPSlot @index={{0}} />
          <InputOTPSlot @index={{1}} />
          <InputOTPSlot @index={{2}} />
          <InputOTPSlot @index={{3}} />
          <InputOTPSlot @index={{4}} />
          <InputOTPSlot @index={{5}} />
        </InputOTPGroup>
      </InputOTP>
      <div class="text-center text-sm">
        {{#if this.value}}
          You entered:
          {{this.value}}
        {{else}}
          Enter your one-time password.
        {{/if}}
      </div>
    </div>
  </template>
}

Form

import { on } from '@ember/modifier';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import { Button } from '@/components/ui/button';
import {
  Field,
  FieldContent,
  FieldError,
  FieldLabel,
} from '@/components/ui/field';
import {
  InputOTP,
  InputOTPGroup,
  InputOTPSlot,
} from '@/components/ui/input-otp';

export default class InputOtpFormDemo extends Component {
  @tracked pin = '';
  @tracked errorMessage = '';

  handleSubmit = (event: Event) => {
    event.preventDefault();

    if (this.pin.length !== 6) {
      this.errorMessage = 'Please enter a 6-digit one-time password';
      return;
    }

    this.errorMessage = '';
    alert(`Submitted PIN: ${this.pin}`);
  };

  handleValueChange = (value: string) => {
    this.pin = value;
    if (this.errorMessage && value.length === 6) {
      this.errorMessage = '';
    }
  };

  <template>
    <form {{on "submit" this.handleSubmit}} class="w-full max-w-sm space-y-4">
      <Field @invalid={{if this.errorMessage true false}}>
        <FieldLabel>One-Time Password</FieldLabel>
        <FieldContent>
          <InputOTP
            @maxLength={{6}}
            @onValueChange={{this.handleValueChange}}
            @value={{this.pin}}
          >
            <InputOTPGroup>
              <InputOTPSlot @index={{0}} />
              <InputOTPSlot @index={{1}} />
              <InputOTPSlot @index={{2}} />
              <InputOTPSlot @index={{3}} />
              <InputOTPSlot @index={{4}} />
              <InputOTPSlot @index={{5}} />
            </InputOTPGroup>
          </InputOTP>
          {{#if this.errorMessage}}
            <FieldError>{{this.errorMessage}}</FieldError>
          {{/if}}
        </FieldContent>
      </Field>
      <Button type="submit">Submit</Button>
    </form>
  </template>
}