Files
heroui-vue/apps/docs/demos/input-otp-on-complete.vue
T

78 lines
1.8 KiB
Vue

<script setup lang="ts">
import { onBeforeUnmount, ref } from "vue";
import {
Button,
Form,
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
Label,
Spinner,
} from "@heroui-vue/components";
const value = ref("");
const isComplete = ref(false);
const isSubmitting = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
onBeforeUnmount(() => clearTimeout(timer));
function handleComplete(code: string) {
isComplete.value = true;
// Demo-only: no network request.
console.log("Code complete:", code);
}
function handleSubmit(event: Event) {
event.preventDefault();
isSubmitting.value = true;
timer = setTimeout(() => {
isSubmitting.value = false;
value.value = "";
isComplete.value = false;
}, 2000);
}
import { useId } from "vue";
const otpLabelId = useId();
</script>
<template>
<Form class="flex w-[280px] flex-col gap-2" @submit="handleSubmit">
<Label :id="otpLabelId">验证账户</Label>
<InputOTP
:aria-labelledby="otpLabelId"
v-model="value"
:max-length="6"
@complete="handleComplete"
@update:model-value="isComplete = false"
>
<InputOTPGroup>
<InputOTPSlot :index="0" />
<InputOTPSlot :index="1" />
<InputOTPSlot :index="2" />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot :index="3" />
<InputOTPSlot :index="4" />
<InputOTPSlot :index="5" />
</InputOTPGroup>
</InputOTP>
<Button
class="mt-2 w-full"
:is-disabled="!isComplete"
:is-pending="isSubmitting"
type="submit"
variant="primary"
>
<template v-if="isSubmitting">
<Spinner color="current" size="sm" />
验证中…
</template>
<template v-else>验证验证码</template>
</Button>
</Form>
</template>