fix(projects): 修复useModal组件主题色失效问题

This commit is contained in:
Yanbowen 2021-12-24 15:23:24 +08:00
parent d9ccce69df
commit 70ea2ef8f2
3 changed files with 44 additions and 12 deletions

View File

@ -1,20 +1,27 @@
<template> <template>
<NModal <app-provider>
<n-modal
v-model:show="show" v-model:show="show"
v-bind="getModalProps" v-bind="getModalProps"
:loading="loading" :loading="loading"
@positive-click="positiveClick" @positive-click="positiveClick"
@negative-click="negativeClick" @negative-click="negativeClick"
> >
{{ isFunction(props.content) ? props.content() : props.content }} <template v-if="!isVNode(SlotContent)">
</NModal> {{ SlotContent }}
</template>
<component :is="SlotContent" v-else />
</n-modal>
</app-provider>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref } from 'vue'; import { computed, ref, isVNode } from 'vue';
import type { VNode } from 'vue';
import { NModal } from 'naive-ui'; import { NModal } from 'naive-ui';
import type { ModalProps } from 'naive-ui'; import type { ModalProps } from 'naive-ui';
import { omit, isFunction } from 'lodash-es'; import { omit, isFunction } from 'lodash-es';
import AppProvider from '@/AppProvider.vue';
export interface HookModalProps extends Partial<ModalProps> { export interface HookModalProps extends Partial<ModalProps> {
closeModal?: () => void; closeModal?: () => void;
@ -26,6 +33,12 @@ const props = withDefaults(defineProps<HookModalProps>(), {
/** 重组NModal的props */ /** 重组NModal的props */
const getModalProps = computed(() => omit(props, ['onPositiveClick', 'onNegativeClick'])); const getModalProps = computed(() => omit(props, ['onPositiveClick', 'onNegativeClick']));
const SlotContent = computed(() => {
if (isFunction(props.content)) {
return isVNode(props.content()) ? (props.content() as VNode) : (props.content() as any);
}
return props.content as string;
});
const loading = ref(false); const loading = ref(false);
const show = computed({ const show = computed({

View File

@ -11,7 +11,7 @@ export default function useModal() {
if (modalInstance) { if (modalInstance) {
return modalInstance; return modalInstance;
} }
const container = document.createElement('template'); const container = document.createElement('div');
const VNode = createVNode(Modal); const VNode = createVNode(Modal);
VNode.appContext = appContext; VNode.appContext = appContext;
render(VNode, container); render(VNode, container);

View File

@ -18,13 +18,19 @@
<n-button @click="handleShow">来吧</n-button> <n-button @click="handleShow">来吧</n-button>
</n-card> </n-card>
</n-grid-item> </n-grid-item>
<n-grid-item>
<n-card title="渲染一个组件" class="min-h-180px">
<n-button @click="handleRender">来吧</n-button>
</n-card>
</n-grid-item>
</n-grid> </n-grid>
</n-card> </n-card>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from 'vue'; import { ref, h } from 'vue';
import { NCard, NGrid, NGridItem, NModal, NButton } from 'naive-ui'; import { NCard, NGrid, NGridItem, NModal, NButton } from 'naive-ui';
import { ComponentTable } from '@/views';
import { useModal } from '@/hooks'; import { useModal } from '@/hooks';
const visible = ref(false); const visible = ref(false);
@ -49,5 +55,18 @@ function handleShow() {
negativeText: '算了' negativeText: '算了'
}); });
} }
function handleRender() {
showModal({
title: '渲染个组件试试',
content: () => h(ComponentTable),
maskClosable: true,
preset: 'card',
bordered: false,
overlayStyle: {
width: '600px'
}
});
}
</script> </script>
<style lang="scss" scoped></style> <style lang="scss" scoped></style>