1
1
<template >
2
2
<TransitionRoot
3
3
as =" template"
4
- :show =" open "
4
+ :show =" isOpen "
5
5
@after-leave =" $emit('after-leave')"
6
6
>
7
7
<HDialog
116
116
</slot >
117
117
<div
118
118
class =" px-4 pb-7 pt-4 sm:px-6"
119
- v-if =" dialogActions .length || $slots.actions"
119
+ v-if =" actions .length || $slots.actions"
120
120
>
121
121
<slot name =" actions" v-bind =" { close }" >
122
122
<div class =" space-y-2" >
123
123
<Button
124
124
class =" w-full"
125
- v-for =" action in dialogActions "
125
+ v-for =" action in actions "
126
126
:key =" action.label"
127
127
v-bind =" action"
128
128
>
@@ -147,7 +147,7 @@ import {
147
147
TransitionChild ,
148
148
TransitionRoot ,
149
149
} from ' @headlessui/vue'
150
- import { computed , ref , watch } from ' vue'
150
+ import { computed , reactive } from ' vue'
151
151
import { Button , ButtonProps } from ' ./Button'
152
152
import FeatherIcon from ' ./FeatherIcon.vue'
153
153
@@ -178,8 +178,11 @@ type DialogOptions = {
178
178
position ?: ' top' | ' center'
179
179
}
180
180
181
+ type DialogActionContext = {
182
+ close: () => void
183
+ }
181
184
type DialogAction = ButtonProps & {
182
- onClick? : (close : ( ) => void ) => Promise <void > | void
185
+ onClick? : (context : DialogActionContext ) => void | Promise <void >
183
186
}
184
187
185
188
interface DialogProps {
@@ -199,37 +202,33 @@ const emit = defineEmits<{
199
202
(event : ' after-leave' ): void
200
203
}>()
201
204
202
- const dialogActions = ref <Array <DialogAction >>([])
203
- watch (
204
- () => props .options .actions ,
205
- (actions ) => {
206
- if (! actions ?.length ) return
205
+ const actions = computed (() => {
206
+ let actions = props .options .actions
207
+ if (! actions ?.length ) return []
207
208
208
- dialogActions .value = actions .map ((action ) => {
209
- let loading = ref (false )
210
- return {
211
- ... action ,
212
- loading ,
213
- onClick: ! action .onClick
214
- ? close
215
- : async () => {
216
- loading .value = true
217
- try {
218
- if (action .onClick ) {
219
- // pass close function to action
220
- await action .onClick (close )
221
- }
222
- } finally {
223
- loading .value = false
209
+ return actions .map ((action ) => {
210
+ let _action = reactive ({
211
+ ... action ,
212
+ loading: false ,
213
+ onClick: ! action .onClick
214
+ ? close
215
+ : async () => {
216
+ _action .loading = true
217
+ try {
218
+ if (action .onClick ) {
219
+ let context: DialogActionContext = { close }
220
+ await action .onClick (context )
224
221
}
225
- },
226
- }
222
+ } finally {
223
+ _action .loading = false
224
+ }
225
+ },
227
226
})
228
- },
229
- { immediate: true },
230
- )
227
+ return _action
228
+ })
229
+ } )
231
230
232
- const open = computed ({
231
+ const isOpen = computed ({
233
232
get() {
234
233
return props .modelValue
235
234
},
@@ -242,7 +241,7 @@ const open = computed({
242
241
})
243
242
244
243
function close() {
245
- open .value = false
244
+ isOpen .value = false
246
245
}
247
246
248
247
const icon = computed (() => {
0 commit comments