石墨仪设备 前端仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

6 months ago
  1. <template>
  2. <div class="bg-white text-xl text-text px-5 py-7 rounded">
  3. <h1 class="font-medium">新增用户</h1>
  4. <section>
  5. <div class="grid grid-cols-[5rem_1fr] gap-x-8 gap-y-4 min-w-[30rem] mt-4 mb-7 px-4">
  6. <span class="text-right">用户名</span>
  7. <input type="text" class="border border-[#eee] rounded-sm px-2" v-model="user.username" />
  8. <span class="text-right">昵称</span>
  9. <input type="text" class="border border-[#eee] rounded-sm px-2" v-model.trim="user.nickname" />
  10. <span class="text-right">密码</span>
  11. <input type="text" class="border border-[#eee] rounded-sm px-2" v-model.trim="user.password" />
  12. </div>
  13. </section>
  14. <footer class="flex justify-center gap-x-6">
  15. <button class="btn-dark px-6 py-1" @click="onConfirm">确定</button>
  16. <button class="btn-light px-6 py-1" @click="$emit('cancel')">取消</button>
  17. </footer>
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { showToast } from "vant";
  22. import { reactive, ref, watch } from "vue";
  23. interface UserDto {
  24. username: string;
  25. nickname: string;
  26. password: string;
  27. }
  28. const emit = defineEmits<{
  29. (e: "confirm", user: UserDto): void;
  30. (e: "cancel"): void;
  31. }>();
  32. const user = reactive<UserDto>({ username: "", nickname: "", password: "" });
  33. watch(
  34. () => user.username,
  35. newVal => {
  36. const reg = /^\S+$/;
  37. if (!reg.test(newVal)) {
  38. user.username = user.username.trim();
  39. }
  40. }
  41. );
  42. function onConfirm() {
  43. if (user.username === "") {
  44. showToast("用户名不能为空")
  45. return
  46. }
  47. if (user.nickname === "") {
  48. showToast("昵称不能为空")
  49. return
  50. }
  51. if (user.password === "") {
  52. showToast("密码不能为空")
  53. return
  54. }
  55. emit("confirm", user);
  56. }
  57. </script>