Skip to content

Commit

Permalink
fix(drawer): 修复不同位置打开抽屉,拖拽不生效的问题 (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean-gao authored Sep 25, 2023
1 parent 76a7af1 commit 59932d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
9 changes: 6 additions & 3 deletions components/drawer/drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ const Drawer = defineComponent({
: 'width';
});

const placement = computed(() => props.placement);
const resizable = computed(() => props.resizable);

const styles = computed(() => {
const sty: CSSProperties = { width: '100%', height: '100%' };
// 初始化的时候 数字直接拼px,如果是字符串直接应用,拖拽后数值覆盖
Expand All @@ -213,10 +216,10 @@ const Drawer = defineComponent({
drawerRef,
dragClass,
} = useResizable({
propsKey: propsKey.value,
placement: props.placement,
propsKey,
placement,
drawerSize,
resizable: props.resizable,
resizable,
prefixCls,
});

Expand Down
25 changes: 13 additions & 12 deletions components/drawer/useResizable.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { computed, onMounted, ref } from 'vue';
import { computed, onMounted, ref, ComputedRef } from 'vue';
import { useEventListener } from '@vueuse/core';

export const useResizable = (config: {
propsKey: 'width' | 'height';
placement: string;
propsKey: ComputedRef<'width' | 'height'>;
placement: ComputedRef<string>;
drawerSize: {
width: string | number;
height: string | number;
};
resizable: boolean;
resizable: ComputedRef<boolean>;
prefixCls: string;
}) => {
const { propsKey, drawerSize, placement, resizable, prefixCls } = config;
Expand Down Expand Up @@ -39,13 +39,13 @@ export const useResizable = (config: {
const onMousedown = (e: MouseEvent) => {
if (drawerRef.value) {
// 鼠标按下时的初始位置
start = propsKey === 'width' ? e.clientX : e.clientY;
start = propsKey.value === 'width' ? e.clientX : e.clientY;

isActive.value = true;

// 拖拽的时候拿到实时的宽度或者高度
lastSizeValue =
propsKey === 'width'
propsKey.value === 'width'
? drawerRef.value.offsetWidth
: drawerRef.value.offsetHeight;
}
Expand All @@ -65,15 +65,16 @@ export const useResizable = (config: {

// 偏移量
const offset =
(propsKey === 'width' ? event.clientX : event.clientY) - start;
(propsKey.value === 'width' ? event.clientX : event.clientY) -
start;

// 根据 位置 偏移量正负加减
if (['left', 'top'].includes(placement)) {
if (['left', 'top'].includes(placement.value)) {
// 鼠标移动时改变宽度或者高度
drawerSize[propsKey] = lastSizeValue + offset;
drawerSize[propsKey.value] = lastSizeValue + offset;
} else {
// 鼠标移动时改变宽度或者高度
drawerSize[propsKey] = lastSizeValue - offset;
drawerSize[propsKey.value] = lastSizeValue - offset;
}
};

Expand All @@ -83,7 +84,7 @@ export const useResizable = (config: {
if (isHover.value) {
classArr.push(`${prefixCls}-drag-hover`);
}
switch (placement) {
switch (placement.value) {
case 'left':
classArr.push(`${prefixCls}-drag-left`);
break;
Expand All @@ -102,7 +103,7 @@ export const useResizable = (config: {

// 可拖拽的时候才发起事件监听
onMounted(() => {
if (resizable) {
if (resizable.value) {
useEventListener(window.document, 'mousemove', doResize);
useEventListener(window.document, 'mouseup', onMouseup);
}
Expand Down
17 changes: 16 additions & 1 deletion docs/.vitepress/components/drawer/resizable.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<template>
<FForm>
<FFormItem label="位置:">
<FRadioGroup v-model="placement">
<FRadio value="top">top</FRadio>
<FRadio value="bottom">bottom</FRadio>
<FRadio value="left">left</FRadio>
<FRadio value="right">right</FRadio>
</FRadioGroup>
</FFormItem>
</FForm>

<FDivider></FDivider>

<FButton @click="open">打开抽屉</FButton>
<FDrawer
v-model:show="show"
title="这里是标题"
resizable
placement="right"
:placement="placement"
width="600px"
@ok="show = false"
>
Expand All @@ -20,13 +33,15 @@ import { ref } from 'vue';
export default {
setup() {
const show = ref(false);
const placement = ref('right');
const open = () => {
show.value = true;
};
return {
show,
open,
placement,
};
},
};
Expand Down

0 comments on commit 59932d9

Please sign in to comment.