Skip to content

Commit

Permalink
feat: added support for slots in dynamic modal (euvl#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaguya3222 authored Nov 15, 2021
1 parent 7d37291 commit 814b1ac
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/PluginCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PluginCore = (Vue, options = {}) => {
const showDynamicModal = (
component,
componentProps,
componentSlots,
modalProps = {},
modalEvents
) => {
Expand All @@ -26,6 +27,7 @@ const PluginCore = (Vue, options = {}) => {
container?.add(
component,
componentProps,
componentSlots,
{ ...defaults, ...modalProps },
modalEvents
)
Expand Down
21 changes: 18 additions & 3 deletions src/components/ModalsContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
v-bind="modal.componentAttrs"
v-on="$listeners"
@close="$modal.hide(modal.modalAttrs.name, $event)"
/>
>
<template v-for="(slot, key) in modal.componentSlots" #[key]="scope">
<VNode :node="slot" :key="key" :scope="scope" />
</template>
</component>
</modal>
</div>
</template>
<script>
import { generateId } from '../utils'
import VNode from './VNode.vue'
const PREFIX = 'dynamic_modal_'
Expand All @@ -27,6 +32,9 @@ export default {
modals: []
}
},
components: {
VNode
},
created() {
/**
* Register ModalContainer so that it was availiable inside the plugin
Expand All @@ -39,7 +47,13 @@ export default {
})
},
methods: {
add(component, componentAttrs = {}, modalAttrs = {}, modalListeners = {}) {
add(
component,
componentAttrs = {},
componentSlots = {},
modalAttrs = {},
modalListeners = {}
) {
const id = generateId()
const name = modalAttrs.name || PREFIX + id
Expand All @@ -48,7 +62,8 @@ export default {
modalAttrs: { ...modalAttrs, name },
modalListeners,
component,
componentAttrs
componentAttrs,
componentSlots
})
this.$nextTick(() => {
Expand Down
22 changes: 22 additions & 0 deletions src/components/VNode.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { isFunction } from '../utils/types'
export default {
name: 'VNode',
props: {
node: {
type: [Object, Function],
required: true
},
scope: {
type: Object,
default: () => ({})
}
},
render() {
if (isFunction(this.node)) return this.node(this.scope)
return this.node
}
}
</script>
4 changes: 4 additions & 0 deletions src/utils/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export const isObject = value => {
export const isFn = value => {
return typeof value === 'function'
}

export const isFunction = value => {
return typeof value === 'function'
}
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare interface VModal {
show(
component: typeof Vue | ComponentOptions<Vue> | AsyncComponent,
componentProps?: object,
componentSlots?: object,
modalProps?: object,
modalEvents?: object
): void
Expand Down

0 comments on commit 814b1ac

Please sign in to comment.