modal.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { ModalName } from '@/common/constants/modalName';
  2. import { inject, provide, ref, SetupContext, watchEffect } from 'vue';
  3. /**
  4. * 控制关闭弹窗
  5. * @param modelName 相对应的弹窗组件名
  6. * @param context 上次ui上下文,用于关闭弹窗时候需要刷新数据
  7. * @returns
  8. */
  9. export function closeModal(modelName: keyof ModalName, context?: SetupContext) {
  10. // 控制弹窗 flag
  11. const visible = ref<boolean>(false);
  12. const show = inject(modelName, ref<boolean>(false))
  13. watchEffect(() => {
  14. visible.value = show.value
  15. })
  16. /**
  17. * 取消弹窗
  18. */
  19. function cancel() {
  20. visible.value = false;
  21. show.value = false
  22. }
  23. // 组件关闭弹窗需要刷新列表数据
  24. function refresh() {
  25. context?.emit('refresh')
  26. cancel()
  27. }
  28. return { visible, cancel, refresh }
  29. }
  30. /**
  31. * 控制打开弹窗
  32. * @param modelName 相对应的弹窗组件名
  33. * @returns openAction 打开弹窗方法
  34. */
  35. export function openModal(modelName: keyof ModalName, data?: any) {
  36. const name = ref<boolean>(false)
  37. provide(modelName, name);
  38. /**
  39. * 打开弹窗
  40. * @param value 弹窗名
  41. */
  42. function openAction() {
  43. console.log('弹窗组件名', modelName);
  44. name.value = true;
  45. }
  46. return { openAction }
  47. }
  48. export type { ModalName };
  49. type emitFn = 'cancel' | 'update'
  50. export function _closeModal(context: SetupContext<emitFn[]>) {
  51. const visible = ref<boolean>(true);
  52. /**
  53. * 关闭弹窗
  54. * @param isRefresh 是否需要刷新页面: tur => 刷新;false => 不刷新
  55. */
  56. function cancel(isRefresh = false) {
  57. visible.value = false;
  58. context.emit('cancel', isRefresh)
  59. }
  60. return { visible, cancel }
  61. }