Skip to content

Commit f068a2b

Browse files
Ye-YiChenPenghuiXu
andauthored
fix: skip drag-scroll when the target or its ancestor is draggable (#386)
`useScrollDrag` only checked `e.target.draggable`, but the mousedown target is the deepest node in the event path while `draggable` is set on the ancestor being dragged (e.g. `<li draggable><span>...</span></li>` in Tree). The drag-scroll lock was wrongly set in that case and the list kept scrolling. Walk up the ancestors and read the IDL attribute, so implicitly draggable elements (`a[href]`, `img`) are covered as well. Co-authored-by: PenghuiXu <penghuixu@tencent.com>
1 parent a7cdfda commit f068a2b

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/hooks/useScrollDrag.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ export function getPageXY(
1313
return obj[horizontal ? 'pageX' : 'pageY'] - window[horizontal ? 'scrollX' : 'scrollY'];
1414
}
1515

16+
/**
17+
* Check the element itself or any of its ancestors is draggable.
18+
* Use the IDL attribute instead of `[draggable]` selector so that
19+
* implicitly draggable elements (`a[href]`, `img`) are also covered.
20+
*/
21+
function isDraggable(ele: HTMLElement | null): boolean {
22+
let current = ele;
23+
while (current) {
24+
if (current.draggable) {
25+
return true;
26+
}
27+
current = current.parentElement;
28+
}
29+
return false;
30+
}
31+
1632
export default function useScrollDrag(
1733
inVirtual: boolean,
1834
componentRef: React.RefObject<HTMLElement>,
@@ -45,7 +61,9 @@ export default function useScrollDrag(
4561

4662
const onMouseDown = (e: MouseEvent) => {
4763
// Skip if element set draggable
48-
if ((e.target as HTMLElement).draggable || e.button !== 0) {
64+
// Note the mousedown target is the deepest node in the event path (e.g. the
65+
// title span inside a Tree node), while `draggable` is set on the ancestor.
66+
if (isDraggable(e.target as HTMLElement) || e.button !== 0) {
4967
return;
5068
}
5169
// Skip if nest List has handled this event

‎tests/scroll.test.js‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,83 @@ describe('List.Scroll', () => {
819819
// Assert that scroll did not change after drag
820820
expect(getScrollTop(container)).toEqual(0);
821821
});
822+
823+
it('can not move when dragging a child of a draggable item', () => {
824+
const onScroll = jest.fn();
825+
const { container } = render(
826+
<List
827+
component="ul"
828+
itemKey="id"
829+
itemHeight={20}
830+
height={100}
831+
data={genData(100)}
832+
onScroll={onScroll}
833+
>
834+
{({ id }) => (
835+
<li draggable>
836+
<span className="drag-child">{id}</span>
837+
</li>
838+
)}
839+
</List>,
840+
);
841+
842+
// Initial scroll should be 0
843+
expect(getScrollTop(container)).toEqual(0);
844+
845+
// Mousedown / mousemove happen on the inner child, while `draggable`
846+
// is set on the <li> ancestor. The child's own `draggable` is false,
847+
// so the fix must walk up to the ancestor to skip the drag scroll.
848+
const child = container.querySelector('.drag-child');
849+
fireEvent.mouseDown(child, { button: 0 });
850+
const moveEvent = createEvent.mouseMove(child);
851+
moveEvent.pageY = 100;
852+
fireEvent(child, moveEvent);
853+
act(() => {
854+
jest.advanceTimersByTime(100);
855+
});
856+
fireEvent.mouseUp(child);
857+
858+
// Should not scroll because an ancestor is draggable
859+
expect(getScrollTop(container)).toEqual(0);
860+
});
861+
862+
it('can not move when dragging a child of an implicitly draggable item', () => {
863+
const onScroll = jest.fn();
864+
const { container } = render(
865+
<List
866+
component="ul"
867+
itemKey="id"
868+
itemHeight={20}
869+
height={100}
870+
data={genData(100)}
871+
onScroll={onScroll}
872+
>
873+
{({ id }) => (
874+
<li>
875+
<a href="#">
876+
<span className="drag-child">{id}</span>
877+
</a>
878+
</li>
879+
)}
880+
</List>,
881+
);
882+
883+
expect(getScrollTop(container)).toEqual(0);
884+
885+
// `a[href]` is draggable by default and carries no `draggable` attribute,
886+
// so matching on the attribute would miss it.
887+
const child = container.querySelector('.drag-child');
888+
fireEvent.mouseDown(child, { button: 0 });
889+
const moveEvent = createEvent.mouseMove(child);
890+
moveEvent.pageY = 100;
891+
fireEvent(child, moveEvent);
892+
act(() => {
893+
jest.advanceTimersByTime(100);
894+
});
895+
fireEvent.mouseUp(child);
896+
897+
expect(getScrollTop(container)).toEqual(0);
898+
});
822899
});
823900

824901
it('not scroll jump for item height change', async () => {

0 commit comments

Comments
 (0)