Skip to content

Commit 038c9ae

Browse files
authored
fix(Dialog): ensure draggable functionality (#3753)
1 parent e03960c commit 038c9ae

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

packages/components/dialog/hooks/useDialogDrag.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const useDialogDrag = (props: DialogDragProps) => {
1616
const dragOffset = useRef({ x: 0, y: 0 });
1717

1818
useMouseEvent(dialogCardRef, {
19+
enabled: canDraggable,
1920
onDown: (e) => {
2021
const { offsetLeft, offsetTop, offsetWidth, offsetHeight, style } = dialogCardRef.current;
2122
if (offsetWidth > screenWidth || offsetHeight > screenHeight) return;
@@ -42,8 +43,6 @@ const useDialogDrag = (props: DialogDragProps) => {
4243
dialogCardRef.current.style.cursor = 'default';
4344
},
4445
});
45-
46-
if (!canDraggable) return;
4746
};
4847

4948
export default useDialogDrag;

packages/components/hooks/useMouseEvent.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface MouseContext {
1515
}
1616

1717
type MouseEventOptions = {
18-
enableTouch?: boolean;
18+
enabled?: boolean;
19+
enableTouch?: boolean; // 支持触摸事件
1920
onDown?: (e: MouseCallback, ctx: MouseContext) => void;
2021
onMove?: (e: MouseCallback, ctx: MouseContext) => void;
2122
onUp?: (e: MouseCallback, ctx: MouseContext) => void;
@@ -24,7 +25,7 @@ type MouseEventOptions = {
2425
};
2526

2627
const useMouseEvent = (elementRef: React.RefObject<HTMLElement>, options: MouseEventOptions) => {
27-
const { enableTouch = true } = options; // 默认支持触摸事件
28+
const { enabled = true, enableTouch = true } = options;
2829
const isMovingRef = useRef(false);
2930

3031
const normalizeEvent = (e: MouseEventLike) => {
@@ -100,7 +101,7 @@ const useMouseEvent = (elementRef: React.RefObject<HTMLElement>, options: MouseE
100101

101102
useEffect(() => {
102103
const el = elementRef.current;
103-
if (!el) return;
104+
if (!el || !enabled) return;
104105

105106
// 基本上只要开启了鼠标事件,就会用到这三个
106107
// 有的组件虽然只需要 mousemove 的回调结果,但也需要 mousedown 和 mouseup 来控制状态
@@ -125,7 +126,7 @@ const useMouseEvent = (elementRef: React.RefObject<HTMLElement>, options: MouseE
125126
el.removeEventListener('touchend', handleMouseUp);
126127
};
127128
// eslint-disable-next-line react-hooks/exhaustive-deps
128-
}, [elementRef.current, options]);
129+
}, [elementRef.current, options, enabled]);
129130

130131
return {
131132
isMoving: isMovingRef.current,

0 commit comments

Comments
 (0)