useDialog.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-21 17:23:32
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-10-14 17:00:57
  6. * @FilePath: \Game-Backstage-Management-System\src\hooks\useDialog.ts
  7. * @Description:
  8. *
  9. */
  10. import type { DialogSetting } from '@/types/table'
  11. import { nextTick } from 'vue'
  12. import Form from '@/components/form/CustomForm.vue'
  13. import 'element-plus/theme-chalk/el-message.css'
  14. import 'element-plus/theme-chalk/el-message-box.css'
  15. export function useDialog() {
  16. /**
  17. * @description: 对话框关闭
  18. * @param {InstanceType} formEl 表单对象
  19. * @param {DialogSetting} dialogConfig 对话框配置
  20. */
  21. const dialogClose = (
  22. formEl: InstanceType<typeof Form> | undefined,
  23. dialogConfig: DialogSetting
  24. ) => {
  25. if (!formEl) return
  26. dialogConfig.dialogVisible = false
  27. formEl.resetForm()
  28. }
  29. /**
  30. * @description: 点击修改按钮,填充数据
  31. * @param {any} row 行数据
  32. * @param {any} formData 表单数据
  33. * @param {DialogSetting} dialogConfig 对话框配置
  34. */
  35. const handleEdit = (row: any, formData: any, dialogConfig: DialogSetting) => {
  36. dialogConfig.type = 1
  37. dialogConfig.dialogVisible = true
  38. // 这里放到nextTick,因为resetFields原理是将表单重置到dom刚渲染时的数据
  39. // 而这个表单,如果第一次使用就点击编辑的话,会将初始值直接设置为这行的数据,导致无法重置
  40. void nextTick(() => {
  41. for (const key in row) {
  42. if (key in formData) {
  43. formData[key] = row[key]
  44. } else {
  45. console.log(key)
  46. }
  47. }
  48. })
  49. }
  50. return {
  51. dialogClose,
  52. handleEdit
  53. }
  54. }