refactor: update

refactor-drawer
ajuner 2021-09-28 14:10:24 +08:00 committed by tangjinzhou
parent 85ce6b6e24
commit 42fad57e49
6 changed files with 63 additions and 46 deletions

View File

@ -5,8 +5,8 @@
<placement /> <placement />
<render-in-current /> <render-in-current />
<form-in-drawer /> <form-in-drawer />
<user-profile />
<multi-level-drawer /> <multi-level-drawer />
<user-profile />
<size /> <size />
</demo-sort> </demo-sort>
</template> </template>

View File

@ -1,6 +1,6 @@
<docs> <docs>
--- ---
order: 6 order: 5
title: title:
zh-CN: 多层抽屉 zh-CN: 多层抽屉
en-US: Multi-level drawer en-US: Multi-level drawer

View File

@ -48,10 +48,10 @@ Render in current dom. custom container, check getContainer.
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, onMounted, ref } from 'vue'; import { defineComponent, ref } from 'vue';
export default defineComponent({ export default defineComponent({
setup() { setup() {
const visible = ref(true); const visible = ref(false);
const afterVisibleChange = (bool: boolean) => { const afterVisibleChange = (bool: boolean) => {
console.log('visible', bool); console.log('visible', bool);
@ -65,10 +65,6 @@ export default defineComponent({
visible.value = false; visible.value = false;
}; };
onMounted(() => {
visible.value = false;
});
return { return {
visible, visible,
afterVisibleChange, afterVisibleChange,

View File

@ -17,15 +17,11 @@ The default width (or height) of Drawer is `378px`, and there is a presetted lar
</docs> </docs>
<template> <template>
<a-button type="primary" @click="showDrawer('default')">Open Default Size (378px)</a-button> <a-button type="primary" style="margin-right: 8px" @click="showDrawer('default')">
Open Default Size (378px)
</a-button>
<a-button type="primary" @click="showDrawer('large')">Open Large Size (736px)</a-button> <a-button type="primary" @click="showDrawer('large')">Open Large Size (736px)</a-button>
<a-drawer <a-drawer title="Basic Drawer" :size="size" :visible="visible" @close="onClose">
title="Basic Drawer"
:size="size"
:placement="placement"
:visible="visible"
@close="onClose"
>
<template #extra> <template #extra>
<a-button style="margin-right: 8px" @click="onClose">Cancel</a-button> <a-button style="margin-right: 8px" @click="onClose">Cancel</a-button>
<a-button type="primary" @click="onClose">Submit</a-button> <a-button type="primary" @click="onClose">Submit</a-button>

View File

@ -1,6 +1,6 @@
<docs> <docs>
--- ---
order: 5 order: 6
title: title:
zh-CN: 信息预览抽屉 zh-CN: 信息预览抽屉
en-US: Preview drawer en-US: Preview drawer

View File

@ -6,6 +6,7 @@ import omit from '../../_util/omit';
import supportsPassive from '../../_util/supportsPassive'; import supportsPassive from '../../_util/supportsPassive';
import { DrawerChildProps } from './IDrawerPropTypes'; import { DrawerChildProps } from './IDrawerPropTypes';
import type { IDrawerChildProps } from './IDrawerPropTypes'; import type { IDrawerChildProps } from './IDrawerPropTypes';
import setStyle from '../../_util/setStyle';
import { import {
addEventListener, addEventListener,
@ -21,11 +22,48 @@ import {
const currentDrawer: Record<string, boolean> = {}; const currentDrawer: Record<string, boolean> = {};
export interface scrollLockOptions {
container: HTMLElement;
}
const createScrollLocker = (options: scrollLockOptions) => {
let scrollBarSize = 0;
let cacheStyleMap = new Map();
return {
getContainer: () => {
return options?.container;
},
getCacheStyleMap: () => {
return cacheStyleMap;
},
lock: () => {
cacheStyleMap.set(
options.container,
setStyle(
{
width: scrollBarSize !== 0 ? `calc(100% - ${scrollBarSize}px)` : undefined,
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
},
{
element: options.container || document.body,
},
),
);
},
unLock: () => {
setStyle(cacheStyleMap.get(options.container) || {}, { element: options.container });
cacheStyleMap.delete(options.container);
},
};
};
const DrawerChild = defineComponent({ const DrawerChild = defineComponent({
inheritAttrs: false, inheritAttrs: false,
props: DrawerChildProps, props: DrawerChildProps,
emits: ['close', 'handleClick', 'change'], emits: ['close', 'handleClick', 'change'],
setup(props, { emit, slots, expose }) { setup(props, { emit, slots }) {
const state = reactive({ const state = reactive({
levelDom: [], levelDom: [],
dom: null, dom: null,
@ -40,6 +78,7 @@ const DrawerChild = defineComponent({
x: null, x: null,
y: null, y: null,
}, },
scrollLocker: null,
}); });
onMounted(() => { onMounted(() => {
@ -55,6 +94,13 @@ const DrawerChild = defineComponent({
.replace('.', Math.round(Math.random() * 9).toString()), .replace('.', Math.round(Math.random() * 9).toString()),
).toString(16)}`; ).toString(16)}`;
getLevelDom(props); getLevelDom(props);
if (container) {
state.scrollLocker = createScrollLocker({
container: container.parentNode,
});
}
if (open) { if (open) {
if (container && container.parentNode === document.body) { if (container && container.parentNode === document.body) {
currentDrawer[state.drawerId] = open; currentDrawer[state.drawerId] = open;
@ -67,14 +113,15 @@ const DrawerChild = defineComponent({
} }
}); });
if (showMask) { if (showMask) {
props.scrollLocker?.lock(); state.scrollLocker?.lock();
} }
} }
}); });
}); });
onUpdated(() => { onUpdated(() => {
const { open, getContainer, scrollLocker, showMask, autoFocus } = props; const { open, getContainer, showMask, autoFocus } = props;
const container = getContainer?.(); const container = getContainer?.();
if (container && container.parentNode === document.body) { if (container && container.parentNode === document.body) {
currentDrawer[state.drawerId] = !!open; currentDrawer[state.drawerId] = !!open;
@ -85,21 +132,21 @@ const DrawerChild = defineComponent({
domFocus(); domFocus();
} }
if (showMask) { if (showMask) {
scrollLocker?.lock(); state.scrollLocker?.lock();
} }
} else { } else {
scrollLocker?.unLock(); state.scrollLocker?.unLock();
} }
}); });
onUnmounted(() => { onUnmounted(() => {
const { open, scrollLocker } = props; const { open } = props;
delete currentDrawer[state.drawerId]; delete currentDrawer[state.drawerId];
if (open) { if (open) {
setLevelTransform(false); setLevelTransform(false);
document.body.style.touchAction = ''; document.body.style.touchAction = '';
} }
scrollLocker?.unLock(); state.scrollLocker?.unLock();
}); });
watch( watch(
@ -400,28 +447,6 @@ const DrawerChild = defineComponent({
}; };
}; };
const getDerivedStateFromProps = (
props: IDrawerChildProps,
{ prevProps }: { prevProps: IDrawerChildProps },
) => {
const nextState = {
prevProps: props,
};
if (prevProps !== undefined) {
const { placement, level } = props;
if (placement !== prevProps.placement) {
// test bug, dom
state.contentDom = null;
}
if (level !== prevProps.level) {
getLevelDom(props);
}
}
return nextState;
};
expose({ getDerivedStateFromProps });
return () => { return () => {
const { const {
width, width,