Trên trang này

Input OTP

Ví dụ trực tiếp của Input OTP, kèm đúng file Vue hiển thị từng ví dụ.

Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào ứng dụng đã cài registry, hoặc mở trong StackBlitz.

Sáu chữ số

Dán được: cả mã điền vào một lần.

Chúng tôi đã gửi mã 6 chữ số tới [email protected].

InputOTPBasic.vue
<script setup lang="ts">
import { ref } from 'vue'
import { REGEXP_ONLY_DIGITS } from 'vue-input-otp'
import { InputOTP, InputOTPGroup, InputOTPSlot } from '@/components/ui/input-otp'

const code = ref('')
</script>

<template>
  <div class="grid gap-(--bh-space-100)">
    <label for="sign-in-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã đăng nhập</label>
    <InputOTP
      id="sign-in-code"
      v-slot="{ slots }"
      v-model="code"
      :maxlength="6"
      :pattern="REGEXP_ONLY_DIGITS"
      autocomplete="one-time-code"
      aria-describedby="sign-in-code-hint"
    >
      <InputOTPGroup>
        <InputOTPSlot v-for="(_, index) in slots" :key="index" :index="index" />
      </InputOTPGroup>
    </InputOTP>
    <p id="sign-in-code-hint" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Chúng tôi đã gửi mã 6 chữ số tới [email protected].</p>
  </div>
</template>

Theo nhóm

Dấu ngăn chia mã thành các nhóm dễ đọc hơn.

InputOTPGroups.vue
<script setup lang="ts">
import { ref } from 'vue'
import { REGEXP_ONLY_DIGITS_AND_CHARS } from 'vue-input-otp'
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from '@/components/ui/input-otp'

const code = ref('')
</script>

<template>
  <div class="grid gap-(--bh-space-100)">
    <label for="recovery-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã khôi phục</label>
    <InputOTP
      id="recovery-code"
      v-model="code"
      :maxlength="6"
      :pattern="REGEXP_ONLY_DIGITS_AND_CHARS"
    >
      <InputOTPGroup>
        <InputOTPSlot :index="0" />
        <InputOTPSlot :index="1" />
        <InputOTPSlot :index="2" />
      </InputOTPGroup>
      <InputOTPSeparator />
      <InputOTPGroup>
        <InputOTPSlot :index="3" />
        <InputOTPSlot :index="4" />
        <InputOTPSlot :index="5" />
      </InputOTPGroup>
    </InputOTP>
  </div>
</template>

Kiểm tra mã

Mã được kiểm tra khi đủ các ô; mã sai hiện lỗi.

1
2
3
4
5
6

Mã chưa đúng. Hãy xem email mới nhất rồi thử lại.

Nhập được sau khi ERP gửi mã.

InputOTPStates.vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { REGEXP_ONLY_DIGITS } from 'vue-input-otp'
import { InputOTP, InputOTPGroup, InputOTPSlot } from '@/components/ui/input-otp'

// Mã được kiểm tra khi đủ 6 chữ số. Nhập mã khác để xóa lỗi.
const code = ref('123456')
const wrong = computed(() => code.value === '123456')
</script>

<template>
  <div class="grid gap-(--bh-space-300)">
    <div class="grid gap-(--bh-space-100)">
      <label for="checked-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã xác minh</label>
      <InputOTP
        id="checked-code"
        v-model="code"
        :maxlength="6"
        :pattern="REGEXP_ONLY_DIGITS"
        :aria-invalid="wrong"
        aria-describedby="checked-code-error"
      >
        <InputOTPGroup>
          <InputOTPSlot v-for="index in 6" :key="index" :index="index - 1" />
        </InputOTPGroup>
      </InputOTP>
      <p v-if="wrong" id="checked-code-error" class="m-0 text-(--bh-color-text-danger) [font:var(--bh-font-body-small)]">Mã chưa đúng. Hãy xem email mới nhất rồi thử lại.</p>
    </div>

    <div class="grid gap-(--bh-space-100)">
      <label for="waiting-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã từ ERP</label>
      <InputOTP id="waiting-code" :maxlength="6" disabled aria-describedby="waiting-code-hint">
        <InputOTPGroup>
          <InputOTPSlot v-for="index in 6" :key="index" :index="index - 1" />
        </InputOTPGroup>
      </InputOTP>
      <p id="waiting-code-hint" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Nhập được sau khi ERP gửi mã.</p>
    </div>
  </div>
</template>

Cỡ lớn

size="lg" làm mỗi ô cao 40 px (size.control.large), cho trang đăng nhập và onboarding.

Chúng tôi đã gửi mã 6 chữ số tới [email protected].

InputOTPLarge.vue
<script setup lang="ts">
import { ref } from 'vue'
import { REGEXP_ONLY_DIGITS } from 'vue-input-otp'
import { InputOTP, InputOTPGroup, InputOTPSlot } from '@/components/ui/input-otp'

const code = ref('')
</script>

<template>
  <div class="grid gap-(--bh-space-100)">
    <label for="large-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã đăng nhập</label>
    <InputOTP
      id="large-code"
      v-slot="{ slots }"
      v-model="code"
      :maxlength="6"
      size="lg"
      :pattern="REGEXP_ONLY_DIGITS"
      autocomplete="one-time-code"
      aria-describedby="large-code-hint"
    >
      <InputOTPGroup>
        <InputOTPSlot v-for="(_, index) in slots" :key="index" :index="index" />
      </InputOTPGroup>
    </InputOTP>
    <p id="large-code-hint" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Chúng tôi đã gửi mã 6 chữ số tới [email protected].</p>
  </div>
</template>