瀏覽代碼

feat: 函数式弹框组件添加 `beforeCancel`和`beforeSure`回调

xiaoxian521 1 年之前
父節點
當前提交
47f951312e
共有 2 個文件被更改,包括 18 次插入2 次删除
  1. 14 2
      src/components/ReDialog/index.vue
  2. 4 0
      src/components/ReDialog/type.ts

+ 14 - 2
src/components/ReDialog/index.vue

@@ -19,7 +19,13 @@ const footerButtons = computed(() => {
             text: true,
             bg: true,
             btnClick: ({ dialog: { options, index } }) => {
-              closeDialog(options, index, { command: "cancel" });
+              const done = () =>
+                closeDialog(options, index, { command: "cancel" });
+              if (options?.beforeCancel && isFunction(options?.beforeCancel)) {
+                options.beforeCancel(done);
+              } else {
+                done();
+              }
             }
           },
           {
@@ -28,7 +34,13 @@ const footerButtons = computed(() => {
             text: true,
             bg: true,
             btnClick: ({ dialog: { options, index } }) => {
-              closeDialog(options, index, { command: "sure" });
+              const done = () =>
+                closeDialog(options, index, { command: "sure" });
+              if (options?.beforeSure && isFunction(options?.beforeSure)) {
+                options.beforeSure(done);
+              } else {
+                done();
+              }
             }
           }
         ] as Array<ButtonProps>);

+ 4 - 0
src/components/ReDialog/type.ts

@@ -189,6 +189,10 @@ interface DialogOptions extends DialogProps {
     options: DialogOptions;
     index: number;
   }) => void;
+  /** 点击底部取消按钮的回调,会暂停 `Dialog` 的关闭. 回调函数内执行 `done` 参数方法的时候才是真正关闭对话框的时候 */
+  beforeCancel?: (done: Function) => void;
+  /** 点击底部确定按钮的回调,会暂停 `Dialog` 的关闭. 回调函数内执行 `done` 参数方法的时候才是真正关闭对话框的时候 */
+  beforeSure?: (done: Function) => void;
 }
 
 export type { EventType, ArgsType, DialogProps, ButtonProps, DialogOptions };