| 1 | <template> |
| 2 | <div> |
| 3 | <n-form ref="formRef" :model :rules> |
| 4 | <n-form-item path="email" label="Email" first> |
| 5 | <n-input |
| 6 | v-model:value="model.email" |
| 7 | placeholder="Input your email" |
| 8 | size="large" |
| 9 | @keydown.enter="forgotPassword" |
| 10 | /> |
| 11 | </n-form-item> |
| 12 | <div class="flex flex-col items-end gap-6"> |
| 13 | <div class="w-full"> |
| 14 | <n-button type="primary" class="w-full!" size="large" :disabled="!isValid" @click="forgotPassword"> |
| 15 | Send Reset Link |
| 16 | </n-button> |
| 17 | </div> |
| 18 | </div> |
| 19 | </n-form> |
| 20 | </div> |
| 21 | </template> |
| 22 | |
| 23 | <script lang="ts" setup> |
| 24 | import type { FormInst, FormItemRule, FormRules, FormValidationError } from "naive-ui" |
| 25 | import { NButton, NForm, NFormItem, NInput, useMessage } from "naive-ui" |
| 26 | import isEmail from "validator/es/lib/isEmail" |
| 27 | import { computed, ref, watch } from "vue" |
| 28 | |
| 29 | interface ModelType { |
| 30 | email: string | null |
| 31 | } |
| 32 | |
| 33 | const formRef = ref<FormInst | null>(null) |
| 34 | const message = useMessage() |
| 35 | const model = ref<ModelType>({ |
| 36 | email: null |
| 37 | }) |
| 38 | |
| 39 | const rules: FormRules = { |
| 40 | email: [ |
| 41 | { |
| 42 | required: true, |
| 43 | trigger: ["blur"], |
| 44 | message: "The email is mandatory" |
| 45 | }, |
| 46 | { |
| 47 | validator: (_rule: FormItemRule, value: string): boolean => { |
| 48 | return isEmail(value) |
| 49 | }, |
| 50 | message: "The email is not formatted correctly", |
| 51 | trigger: ["blur"] |
| 52 | } |
| 53 | ] |
| 54 | } |
| 55 | |
| 56 | const isValid = computed(() => { |
| 57 | return isEmail(model.value.email || "") |
| 58 | }) |
| 59 | |
| 60 | function forgotPassword(e: Event) { |
| 61 | e.preventDefault() |
| 62 | formRef.value?.validate((errors: Array<FormValidationError> | undefined) => { |
| 63 | if (!errors) { |
| 64 | message.success("Reset Link sent") |
| 65 | } |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | watch(isValid, val => { |
| 70 | if (val) { |
| 71 | formRef.value?.validate() |
| 72 | } |
| 73 | }) |
| 74 | </script> |