Skip to content

Commit e6bc3dc

Browse files
authored
Merge pull request #339 from devforth/AdminForth/810
Admin forth/810
2 parents 58ca992 + 0718c6a commit e6bc3dc

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

adminforth/documentation/docs/tutorial/03-Customization/15-afcl.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,41 @@ const closeDialog = () => {
595595
}
596596
```
597597

598+
### Before open/close dialog handlers
599+
If you want to run custom logic before the dialog opens or closes by passing callback props:
600+
601+
```ts
602+
<Dialog
603+
class="w-96"
604+
:beforeCloseFunction="onBeforeOpen"
605+
:beforeOpenFunction="onBeforeClose"
606+
>
607+
<template #trigger>
608+
<Button>Dialog Toggle</Button>
609+
</template>
610+
611+
<div class="space-y-4">
612+
<p>This is the first paragraph of dialog content.</p>
613+
<p>And this is the second paragraph.</p>
614+
</div>
615+
</Dialog>
616+
```
617+
Now you can pass before open/close functions:
618+
```ts
619+
const counter = ref(0);
620+
621+
function onBeforeOpen() {
622+
counter.value++;
623+
console.log(`custom open function called ${counter.value}`);
624+
}
625+
626+
function onBeforeClose() {
627+
counter.value++;
628+
console.log(`custom close function called ${counter.value}`);
629+
}
630+
```
631+
632+
598633
## Dropzone
599634

600635
```ts

adminforth/spa/src/afcl/Dialog.vue

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,31 @@ import { Modal } from 'flowbite';
6363
const modalEl = ref(null);
6464
const modal: Ref<Modal|null> = ref(null);
6565
66-
const props = defineProps({
67-
header: {
68-
type: String,
69-
default: '',
70-
},
71-
headerCloseButton: {
72-
type: Boolean,
73-
default: true,
74-
},
75-
buttons: {
76-
type: Array,
77-
default: () => [{ label: 'Close', onclick: (dialog) => dialog.hide(), type: '' }],
78-
},
79-
clickToCloseOutside: {
80-
type: Boolean,
81-
default: true,
82-
},
83-
});
66+
interface DialogButton {
67+
label: string
68+
onclick: (dialog: any) => void
69+
type?: string
70+
}
71+
72+
interface DialogProps {
73+
header?: string
74+
headerCloseButton?: boolean
75+
buttons?: DialogButton[]
76+
clickToCloseOutside?: boolean
77+
beforeCloseFunction?: (() => void | Promise<void>) | null
78+
beforeOpenFunction?: (() => void | Promise<void>) | null
79+
}
80+
81+
const props = withDefaults(defineProps<DialogProps>(), {
82+
header: '',
83+
headerCloseButton: true,
84+
buttons: () => [
85+
{ label: 'Close', onclick: (dialog: any) => dialog.hide(), type: '' },
86+
],
87+
clickToCloseOutside: true,
88+
beforeCloseFunction: null,
89+
beforeOpenFunction: null,
90+
})
8491
8592
onMounted(async () => {
8693
//await one tick when all is mounted
@@ -89,7 +96,17 @@ onMounted(async () => {
8996
modalEl.value,
9097
{
9198
backdrop: props.clickToCloseOutside ? 'dynamic' : 'static',
92-
},
99+
onHide: async () => {
100+
if (props.beforeCloseFunction) {
101+
await props.beforeCloseFunction();
102+
}
103+
},
104+
onShow: async () => {
105+
if (props.beforeOpenFunction) {
106+
await props.beforeOpenFunction();
107+
}
108+
},
109+
}
93110
);
94111
})
95112

0 commit comments

Comments
 (0)