Skip to content

Commit 58b6fe5

Browse files
committed
feat: improve patchOptions and allow to pass Vfm instance until modalInstance.open({ context: vfm })
1 parent 81d2cd3 commit 58b6fe5

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

packages/vue-final-modal/src/useApi.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
7777
if (options.modelValue === true)
7878
options.context?.dynamicModals.push(options)
7979

80-
function open(): Promise<string> {
80+
function open(opt?: { context: Vfm }): Promise<string> {
81+
if (opt?.context)
82+
options.context = opt.context
8183
if (!options?.context)
8284
return Promise.resolve('[Vue Final Modal] options.context is not exist.')
8385
if (options.modelValue)
@@ -106,9 +108,16 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
106108
})
107109
}
108110

109-
function patchOptions(_options: Partial<Omit<UseModalOptions<P>, 'defaultModelValue' | 'context'>>) {
111+
function patchOptions(_options: Partial<UseModalOptions<P>>) {
110112
const { slots, ...rest } = withMarkRaw(_options, options.component)
111113

114+
if (_options.defaultModelValue !== undefined)
115+
options.defaultModelValue = _options.defaultModelValue
116+
if (_options.keepAlive !== undefined)
117+
options.keepAlive = _options.keepAlive
118+
if (_options.context)
119+
options.context = _options.context
120+
112121
// patch options.component and options.attrs
113122
patchComponentOptions(options, rest)
114123

@@ -126,6 +135,25 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
126135
}
127136
}
128137

138+
function patchComponentOptions<P>(
139+
options: UseModalOptions<P> | ModalSlotOptions,
140+
newOptions: Partial<UseModalOptions<P>> | ModalSlotOptions,
141+
) {
142+
if (newOptions.component)
143+
options.component = newOptions.component
144+
145+
if (newOptions.attrs)
146+
patchAttrs(options.attrs!, newOptions.attrs)
147+
}
148+
149+
function patchAttrs<T extends Record<string, any>>(attrs: T, newAttrs: Partial<T>): T {
150+
Object.entries(newAttrs).forEach(([key, value]) => {
151+
attrs[key as keyof T] = value
152+
})
153+
154+
return attrs
155+
}
156+
129157
function destroy(): void {
130158
if (!options.context)
131159
return
@@ -150,25 +178,6 @@ export function useModalSlot<P>(options: {
150178
return options
151179
}
152180

153-
function patchAttrs<T extends Record<string, any>>(attrs: T, newAttrs: Partial<T>): T {
154-
Object.entries(newAttrs).forEach(([key, value]) => {
155-
attrs[key as keyof T] = value
156-
})
157-
158-
return attrs
159-
}
160-
161-
function patchComponentOptions<P>(
162-
options: Omit<UseModalOptions<P>, 'defaultModelValue' | 'context'> | ModalSlotOptions,
163-
newOptions: Partial<Omit<UseModalOptions<P>, 'defaultModelValue' | 'context'>> | ModalSlotOptions,
164-
) {
165-
if (newOptions.component)
166-
options.component = newOptions.component
167-
168-
if (newOptions.attrs)
169-
patchAttrs(options.attrs!, newOptions.attrs)
170-
}
171-
172181
function isModalSlotOptions(value: any): value is ModalSlotOptions {
173182
return 'component' in value || 'attrs' in value
174183
}

0 commit comments

Comments
 (0)