index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { ref, shallowRef } from "vue";
  2. import XEUtils from "xe-utils";
  3. interface DialogProps {
  4. title: string;
  5. fullscreen?: boolean;
  6. width?: number | string;
  7. top?: string;
  8. }
  9. type CloseValue = "close" | "confirm" | "cancel";
  10. export interface DialogExpose {
  11. // 如果报错了会阻止关闭弹窗
  12. confirm: () => any | Promise<any>;
  13. // 如果报错了会阻止关闭弹窗
  14. cancel: () => any | Promise<any>;
  15. }
  16. // eslint-disable-next-line @typescript-eslint/no-namespace
  17. export namespace UseDialog {
  18. export interface Expose extends DialogExpose {}
  19. export interface Emits {
  20. (e: "cyDialogConfirm" | "cyDialogCancel", value?: any): void;
  21. }
  22. }
  23. export interface DialogOptions {
  24. dialogProps: DialogProps;
  25. showCancel?: boolean;
  26. showConfirm?: boolean;
  27. cancelText?: string;
  28. confirmText?: string;
  29. visible?: boolean;
  30. component?: any;
  31. componentRef?: DialogExpose;
  32. propsData?: {
  33. [key: string]: any;
  34. };
  35. }
  36. export interface DialogState extends DialogOptions {
  37. resolve: (val: { value: CloseValue; data: any }) => void;
  38. reject: (val: { value: CloseValue; data: any }) => void;
  39. dialogKey?: number;
  40. closeValue?: CloseValue;
  41. }
  42. export const dialogStore = ref<DialogState[]>([]);
  43. export const dialogKey = ref(1);
  44. export function useDialog(component: any, props: DialogOptions) {
  45. return new Promise<any>((resolve, reject) => {
  46. props = {
  47. visible: true,
  48. component: shallowRef(component),
  49. showCancel: true,
  50. showConfirm: true,
  51. ...props
  52. };
  53. dialogKey.value++;
  54. dialogStore.value.push({
  55. ...props,
  56. resolve: XEUtils.once(value => {
  57. resolve(value);
  58. }),
  59. reject: XEUtils.once(value => {
  60. reject(value);
  61. }),
  62. dialogKey: dialogKey.value,
  63. closeValue: "close"
  64. });
  65. });
  66. }