register.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <script setup lang="ts">
  2. import type { VbenFormSchema } from '@vben-core/form-ui';
  3. import type { RegisterEmits } from './types';
  4. import { computed, reactive } from 'vue';
  5. import { useRouter } from 'vue-router';
  6. import { $t } from '@vben/locales';
  7. import { useVbenForm } from '@vben-core/form-ui';
  8. import { VbenButton } from '@vben-core/shadcn-ui';
  9. import Title from './auth-title.vue';
  10. interface Props {
  11. formSchema: VbenFormSchema[];
  12. /**
  13. * @zh_CN 是否处于加载处理状态
  14. */
  15. loading?: boolean;
  16. /**
  17. * @zh_CN 登陆路径
  18. */
  19. loginPath?: string;
  20. /**
  21. * @zh_CN 标题
  22. */
  23. title?: string;
  24. /**
  25. * @zh_CN 描述
  26. */
  27. subTitle?: string;
  28. /**
  29. * @zh_CN 按钮文本
  30. */
  31. submitButtonText?: string;
  32. }
  33. defineOptions({
  34. name: 'RegisterForm',
  35. });
  36. const props = withDefaults(defineProps<Props>(), {
  37. formSchema: () => [],
  38. loading: false,
  39. loginPath: '/auth/login',
  40. submitButtonText: '',
  41. subTitle: '',
  42. title: '',
  43. });
  44. const emit = defineEmits<{
  45. submit: RegisterEmits['submit'];
  46. }>();
  47. const [Form, { validate }] = useVbenForm(
  48. reactive({
  49. commonConfig: {
  50. hideLabel: true,
  51. hideRequiredMark: true,
  52. },
  53. schema: computed(() => props.formSchema),
  54. showDefaultActions: false,
  55. }),
  56. );
  57. const router = useRouter();
  58. async function handleSubmit() {
  59. const { valid, values } = await validate();
  60. if (valid) {
  61. emit('submit', values as { password: string; username: string });
  62. }
  63. }
  64. function goToLogin() {
  65. router.push(props.loginPath);
  66. }
  67. </script>
  68. <template>
  69. <div>
  70. <Title>
  71. <slot name="title">
  72. {{ title || $t('authentication.createAnAccount') }} 🚀
  73. </slot>
  74. <template #desc>
  75. <slot name="subTitle">
  76. {{ subTitle || $t('authentication.signUpSubtitle') }}
  77. </slot>
  78. </template>
  79. </Title>
  80. <Form />
  81. <VbenButton
  82. :class="{
  83. 'cursor-wait': loading,
  84. }"
  85. :loading="loading"
  86. aria-label="register"
  87. class="mt-2 w-full"
  88. @click="handleSubmit"
  89. >
  90. <slot name="submitButtonText">
  91. {{ submitButtonText || $t('authentication.signUp') }}
  92. </slot>
  93. </VbenButton>
  94. <div class="mt-4 text-center text-sm">
  95. {{ $t('authentication.alreadyHaveAccount') }}
  96. <span
  97. class="text-primary hover:text-primary-hover cursor-pointer text-sm font-normal"
  98. @click="goToLogin()"
  99. >
  100. {{ $t('authentication.goToLogin') }}
  101. </span>
  102. </div>
  103. </div>
  104. </template>