perf(drawer): perf drawer

This commit is contained in:
vben 2020-10-31 00:41:33 +08:00
parent 840332abf7
commit 28f7f7bf7f
7 changed files with 106 additions and 44 deletions

View File

@ -5,9 +5,14 @@
- 表单组件现在支持直接传入 model 直接进行 set 操作,参考**组件->弹窗扩展->打开弹窗并传递数据** - 表单组件现在支持直接传入 model 直接进行 set 操作,参考**组件->弹窗扩展->打开弹窗并传递数据**
- modal 的 useModalInner 现在支持传入回调函数,用于接收外部`transferModalData`传进来的值, - modal 的 useModalInner 现在支持传入回调函数,用于接收外部`transferModalData`传进来的值,
- 用于处理打开弹窗对表单等组件的设置值。参考**组件->弹窗扩展->打开弹窗并传递数据** - 用于处理打开弹窗对表单等组件的设置值。参考**组件->弹窗扩展->打开弹窗并传递数据**
- `receiveModalDataRef`这个值暂时保留。尽量少用。后续可能会删除。 - `receiveModalDataRef`这个值暂时保留。尽量少用。后续可能会删除。
- drawer 的 useDrawerInner 现在支持传入回调函数,用于接收外部`transferModalData`传进来的值,
- 用于处理打开抽屉对表单等组件的设置值。参考**组件->抽屉扩展->打开抽屉并传递数据**
- `receiveModalDataRef`这个值暂时保留。尽量少用。后续可能会删除。
### ✨ Refactor ### ✨ Refactor
- 表单代码优化重构 - 表单代码优化重构

View File

@ -1,17 +1,19 @@
import { Drawer, Row, Col, Button } from 'ant-design-vue'; import type { DrawerInstance, DrawerProps } from './types';
import { defineComponent, ref, computed, watchEffect, watch, unref, nextTick, toRaw } from 'vue'; import { defineComponent, ref, computed, watchEffect, watch, unref, nextTick, toRaw } from 'vue';
import { Drawer, Row, Col, Button } from 'ant-design-vue';
import { BasicTitle } from '/@/components/Basic'; import { BasicTitle } from '/@/components/Basic';
import { FullLoading } from '/@/components/Loading/index'; import { FullLoading } from '/@/components/Loading/index';
import { LeftOutlined } from '@ant-design/icons-vue';
import { getSlot } from '/@/utils/helper/tsxHelper';
import { DrawerInstance, DrawerProps } from './types';
import { basicProps } from './props'; import { basicProps } from './props';
import { getSlot } from '/@/utils/helper/tsxHelper';
import { isFunction, isNumber } from '/@/utils/is'; import { isFunction, isNumber } from '/@/utils/is';
import { LeftOutlined } from '@ant-design/icons-vue';
import { buildUUID } from '/@/utils/uuid'; import { buildUUID } from '/@/utils/uuid';
import { deepMerge } from '/@/utils'; import { deepMerge } from '/@/utils';
import './index.less'; import './index.less';
const prefixCls = 'basic-drawer'; const prefixCls = 'basic-drawer';
@ -31,7 +33,6 @@ export default defineComponent({
const getProps = computed(() => { const getProps = computed(() => {
const opt: any = { const opt: any = {
// @ts-ignore
placement: 'right', placement: 'right',
...attrs, ...attrs,
...props, ...props,
@ -47,7 +48,6 @@ export default defineComponent({
opt.wrapClassName = opt.wrapClassName opt.wrapClassName = opt.wrapClassName
? `${opt.wrapClassName} ${prefixCls}__detail` ? `${opt.wrapClassName} ${prefixCls}__detail`
: `${prefixCls}__detail`; : `${prefixCls}__detail`;
// opt.maskClosable = false;
if (!opt.getContainer) { if (!opt.getContainer) {
opt.getContainer = `.default-layout__main`; opt.getContainer = `.default-layout__main`;
} }
@ -128,11 +128,11 @@ export default defineComponent({
{showOkBtn && ( {showOkBtn && (
<Button <Button
type={okType} type={okType}
{...okButtonProps}
loading={confirmLoading}
onClick={() => { onClick={() => {
emit('ok'); emit('ok');
}} }}
{...okButtonProps}
loading={confirmLoading}
> >
{() => okText} {() => okText}
</Button> </Button>
@ -152,13 +152,9 @@ export default defineComponent({
{() => ( {() => (
<> <>
{props.showDetailBack && ( {props.showDetailBack && (
<Col class="mx-2"> <Button size="small" type="link" onClick={onClose}>
{() => ( {() => <LeftOutlined />}
<Button size="small" type="link" onClick={onClose}> </Button>
{() => <LeftOutlined />}
</Button>
)}
</Col>
)} )}
{title && ( {title && (

View File

@ -5,10 +5,12 @@ import type { ScrollContainerOptions } from '/@/components/Container/index';
export interface DrawerInstance { export interface DrawerInstance {
setDrawerProps: (props: Partial<DrawerProps> | boolean) => void; setDrawerProps: (props: Partial<DrawerProps> | boolean) => void;
} }
export interface ReturnMethods extends DrawerInstance { export interface ReturnMethods extends DrawerInstance {
openDrawer: (visible?: boolean) => void; openDrawer: (visible?: boolean) => void;
transferDrawerData: (data: any) => void; transferDrawerData: (data: any) => void;
} }
export type RegisterFn = (drawerInstance: DrawerInstance, uuid?: string) => void; export type RegisterFn = (drawerInstance: DrawerInstance, uuid?: string) => void;
export interface ReturnInnerMethods extends DrawerInstance { export interface ReturnInnerMethods extends DrawerInstance {
@ -19,6 +21,7 @@ export interface ReturnInnerMethods extends DrawerInstance {
} }
export type UseDrawerReturnType = [RegisterFn, ReturnMethods]; export type UseDrawerReturnType = [RegisterFn, ReturnMethods];
export type UseDrawerInnerReturnType = [RegisterFn, ReturnInnerMethods]; export type UseDrawerInnerReturnType = [RegisterFn, ReturnInnerMethods];
export interface DrawerFooterProps { export interface DrawerFooterProps {

View File

@ -5,8 +5,11 @@ import type {
DrawerProps, DrawerProps,
UseDrawerInnerReturnType, UseDrawerInnerReturnType,
} from './types'; } from './types';
import { ref, getCurrentInstance, onUnmounted, unref, reactive, computed } from 'vue';
import { ref, getCurrentInstance, onUnmounted, unref, reactive, computed, watchEffect } from 'vue';
import { isProdMode } from '/@/utils/env'; import { isProdMode } from '/@/utils/env';
import { isFunction } from '/@/utils/is';
const dataTransferRef = reactive<any>({}); const dataTransferRef = reactive<any>({});
/** /**
@ -34,6 +37,7 @@ export function useDrawer(): UseDrawerReturnType {
drawerRef.value = drawerInstance; drawerRef.value = drawerInstance;
loadedRef.value = true; loadedRef.value = true;
} }
const getInstance = () => { const getInstance = () => {
const instance = unref(drawerRef); const instance = unref(drawerRef);
if (!instance) { if (!instance) {
@ -41,15 +45,18 @@ export function useDrawer(): UseDrawerReturnType {
} }
return instance; return instance;
}; };
const methods: ReturnMethods = { const methods: ReturnMethods = {
setDrawerProps: (props: Partial<DrawerProps>): void => { setDrawerProps: (props: Partial<DrawerProps>): void => {
getInstance().setDrawerProps(props); getInstance().setDrawerProps(props);
}, },
openDrawer: (visible = true): void => { openDrawer: (visible = true): void => {
getInstance().setDrawerProps({ getInstance().setDrawerProps({
visible: visible, visible: visible,
}); });
}, },
transferDrawerData(val: any) { transferDrawerData(val: any) {
dataTransferRef[unref(uidRef)] = val; dataTransferRef[unref(uidRef)] = val;
}, },
@ -57,7 +64,7 @@ export function useDrawer(): UseDrawerReturnType {
return [getDrawer, methods]; return [getDrawer, methods];
} }
export const useDrawerInner = (): UseDrawerInnerReturnType => { export const useDrawerInner = (callbackFn?: Fn): UseDrawerInnerReturnType => {
const drawerInstanceRef = ref<DrawerInstance | null>(null); const drawerInstanceRef = ref<DrawerInstance | null>(null);
const currentInstall = getCurrentInstance(); const currentInstall = getCurrentInstance();
const uidRef = ref<string>(''); const uidRef = ref<string>('');
@ -65,6 +72,7 @@ export const useDrawerInner = (): UseDrawerInnerReturnType => {
if (!currentInstall) { if (!currentInstall) {
throw new Error('instance is undefined!'); throw new Error('instance is undefined!');
} }
const getInstance = () => { const getInstance = () => {
const instance = unref(drawerInstanceRef); const instance = unref(drawerInstanceRef);
if (!instance) { if (!instance) {
@ -72,26 +80,39 @@ export const useDrawerInner = (): UseDrawerInnerReturnType => {
} }
return instance; return instance;
}; };
const register = (modalInstance: DrawerInstance, uuid: string) => { const register = (modalInstance: DrawerInstance, uuid: string) => {
uidRef.value = uuid; uidRef.value = uuid;
drawerInstanceRef.value = modalInstance; drawerInstanceRef.value = modalInstance;
currentInstall.emit('register', modalInstance); currentInstall.emit('register', modalInstance);
}; };
watchEffect(() => {
const data = dataTransferRef[unref(uidRef)];
if (!data) return;
if (!callbackFn || !isFunction(callbackFn)) return;
callbackFn(data);
});
return [ return [
register, register,
{ {
receiveDrawerDataRef: computed(() => { receiveDrawerDataRef: computed(() => {
return dataTransferRef[unref(uidRef)]; return dataTransferRef[unref(uidRef)];
}), }),
changeLoading: (loading = true) => { changeLoading: (loading = true) => {
getInstance().setDrawerProps({ loading }); getInstance().setDrawerProps({ loading });
}, },
changeOkLoading: (loading = true) => { changeOkLoading: (loading = true) => {
getInstance().setDrawerProps({ confirmLoading: loading }); getInstance().setDrawerProps({ confirmLoading: loading });
}, },
closeDrawer: () => { closeDrawer: () => {
getInstance().setDrawerProps({ visible: false }); getInstance().setDrawerProps({ visible: false });
}, },
setDrawerProps: (props: Partial<DrawerProps>) => { setDrawerProps: (props: Partial<DrawerProps>) => {
getInstance().setDrawerProps(props); getInstance().setDrawerProps(props);
}, },

View File

@ -1,16 +1,56 @@
<template> <template>
<BasicDrawer v-bind="$attrs" @register="register" title="Drawer Title" width="50%"> <BasicDrawer v-bind="$attrs" @register="register" title="Drawer Title" width="50%">
<p class="h-20">外部传递数据: {{ receiveDrawerDataRef }}</p> <div :style="{ background: '#fff' }">
<p class="h-20">外部传递数据: {{ receiveDrawerDataRef }}</p>
<BasicForm @register="registerForm" />
</div>
</BasicDrawer> </BasicDrawer>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { defineComponent, nextTick } from 'vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer'; import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
const schemas: FormSchema[] = [
{
field: 'field1',
component: 'Input',
label: '字段1',
colProps: {
span: 12,
},
defaultValue: '111',
},
{
field: 'field2',
component: 'Input',
label: '字段2',
colProps: {
span: 12,
},
},
];
export default defineComponent({ export default defineComponent({
components: { BasicDrawer }, components: { BasicDrawer, BasicForm },
setup() { setup() {
const [register, { receiveDrawerDataRef }] = useDrawerInner(); const [registerForm, { setFieldsValue }] = useForm({
return { register, receiveDrawerDataRef }; labelWidth: 120,
schemas,
showActionButtonGroup: false,
actionColOptions: {
span: 24,
},
});
const [register, { receiveDrawerDataRef }] = useDrawerInner((data) => {
nextTick(() => {
// 1
setFieldsValue({
field2: data.data,
field1: data.info,
});
});
});
return { register, receiveDrawerDataRef, schemas, registerForm };
}, },
}); });
</script> </script>

View File

@ -1,21 +1,20 @@
<template> <template>
<div class="px-10"> <div class="px-10 py-4">
<Alert message="使用 useDrawer 进行抽屉操作" show-icon class="my-4" /> <Alert message="使用 useDrawer 进行抽屉操作" show-icon />
<a-button type="primary" class="mr-2" @click="openDrawerLoading">打开Drawer</a-button> <a-button type="primary" class="my-4" @click="openDrawerLoading">打开Drawer</a-button>
<Alert message="内外同时同时显示隐藏" show-icon class="my-4" /> <Alert message="内外同时同时显示隐藏" show-icon />
<a-button type="primary" class="mr-2" @click="openDrawer2">打开Drawer</a-button> <a-button type="primary" class="my-4" @click="openDrawer2">打开Drawer</a-button>
<Alert message="自适应高度/显示footer" show-icon class="my-4" /> <Alert message="自适应高度/显示footer" show-icon />
<a-button type="primary" class="mr-2" @click="openDrawer3">打开Drawer</a-button> <a-button type="primary" class="my-4" @click="openDrawer3">打开Drawer</a-button>
<Alert <Alert
message="内外数据交互,外部通过 transferModalData 发送,内部通过 receiveDrawerDataRef 接收。该数据具有响应式" message="内外数据交互,外部通过 transferModalData 发送,内部通过 receiveDrawerDataRef 接收。该数据具有响应式"
show-icon show-icon
class="my-4"
/> />
<a-button type="primary" class="mr-2" @click="send">打开Drawer并传递数据</a-button> <a-button type="primary" class="my-4" @click="send">打开Drawer并传递数据</a-button>
<Alert message="详情页模式" show-icon class="my-4" /> <Alert message="详情页模式" show-icon />
<a-button type="primary" class="mr-2" @click="openDrawer5">打开详情Drawer</a-button> <a-button type="primary" class="my-4" @click="openDrawer5">打开详情Drawer</a-button>
<Drawer1 @register="register1" /> <Drawer1 @register="register1" />
<Drawer2 @register="register2" /> <Drawer2 @register="register2" />
<Drawer3 @register="register3" /> <Drawer3 @register="register3" />

View File

@ -1,26 +1,24 @@
<template> <template>
<div class="px-10"> <div class="px-10 py-4">
<Alert <Alert
message="使用 useModal 进行弹窗操作默认可以拖动可以通过 draggable message="使用 useModal 进行弹窗操作默认可以拖动可以通过 draggable
参数进行控制是否可以拖动/全屏" 参数进行控制是否可以拖动/全屏"
show-icon show-icon
class="my-4"
/> />
<a-button type="primary" class="mr-2" @click="openModalLoading" <a-button type="primary" class="my-4" @click="openModalLoading"
>打开弹窗 默认可以拖动/全屏</a-button >打开弹窗 默认可以拖动/全屏</a-button
> >
<Alert message="内外同时同时显示隐藏" show-icon class="my-4" /> <Alert message="内外同时同时显示隐藏" show-icon />
<a-button type="primary" class="mr-2" @click="openModal2">打开弹窗</a-button> <a-button type="primary" class="my-4" @click="openModal2">打开弹窗</a-button>
<Alert message="自适应高度" show-icon class="my-4" /> <Alert message="自适应高度" show-icon />
<a-button type="primary" class="mr-2" @click="openModal3">打开弹窗</a-button> <a-button type="primary" class="my-4" @click="openModal3">打开弹窗</a-button>
<Alert <Alert
message="内外数据交互,外部通过 transferModalData 发送,内部通过 receiveDrawerDataRef 接收。该数据具有响应式" message="内外数据交互,外部通过 transferModalData 发送,内部通过 receiveDrawerDataRef 接收。该数据具有响应式"
show-icon show-icon
class="my-4"
/> />
<a-button type="primary" class="mr-2" @click="send">打开弹窗并传递数据</a-button> <a-button type="primary" class="my-4" @click="send">打开弹窗并传递数据</a-button>
<Modal1 @register="register1" /> <Modal1 @register="register1" />
<Modal2 @register="register2" /> <Modal2 @register="register2" />