Files
heroui-vue/apps/docs/demos/input-otp-form-example.vue
T

95 lines
2.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onBeforeUnmount, ref, useId } from "vue";
import {
Button,
Description,
FieldError,
Form,
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
Label,
Link,
Spinner,
} from "@heroui-vue/components";
const value = ref("");
const error = ref("");
const isSubmitting = ref(false);
const errorId = useId();
let timer: ReturnType<typeof setTimeout> | undefined;
onBeforeUnmount(() => clearTimeout(timer));
function handleSubmit(event: Event) {
event.preventDefault();
error.value = "";
if (value.value.length !== 6) {
error.value = "请输入全部 6 位数字";
return;
}
isSubmitting.value = true;
// Demo-only simulated verification; no external service is called.
timer = setTimeout(() => {
if (value.value === "123456") {
value.value = "";
} else {
error.value = "验证码无效,请重试。";
}
isSubmitting.value = false;
}, 1500);
}
function handleChange() {
error.value = "";
}
</script>
<template>
<Form class="flex w-[280px] flex-col gap-4" @submit="handleSubmit">
<InputOTP
v-model="value"
:is-invalid="!!error"
:max-length="6"
:validation-errors="error ? [error] : []"
style="flex-direction: column; align-items: stretch; gap: 0.5rem"
@update:model-value="handleChange"
>
<Label>双重身份验证</Label>
<Description>请输入身份验证器应用中的 6 位验证码</Description>
<div class="flex items-center gap-2">
<InputOTPGroup>
<InputOTPSlot :index="0" />
<InputOTPSlot :index="1" />
<InputOTPSlot :index="2" />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot :index="3" />
<InputOTPSlot :index="4" />
<InputOTPSlot :index="5" />
</InputOTPGroup>
</div>
<FieldError :id="errorId" />
</InputOTP>
<Button
class="w-full"
:is-disabled="value.length !== 6"
:is-pending="isSubmitting"
type="submit"
variant="primary"
>
<template v-if="isSubmitting">
<Spinner color="current" size="sm" />
验证中…
</template>
<template v-else>验证</template>
</Button>
<div class="flex items-center justify-center gap-1">
<p class="text-sm text-muted">遇到问题?</p>
<Link class="text-sm text-foreground underline" href="#">使用备用码</Link>
</div>
</Form>
</template>