Skip to content

Commit 818f9de

Browse files
authored
fix: Skip scroll when draggable (#304)
* fix: draggable * test: add test case * chore: translate
1 parent 82566ea commit 818f9de

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/hooks/useScrollDrag.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default function useScrollDrag(
3939
};
4040

4141
const onMouseDown = (e: MouseEvent) => {
42+
// Skip if element set draggable
43+
if ((e.target as HTMLElement).draggable) {
44+
return;
45+
}
4246
// Skip if nest List has handled this event
4347
const event = e as MouseEvent & {
4448
_virtualHandled?: boolean;

tests/scroll.test.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -576,22 +576,8 @@ describe('List.Scroll', () => {
576576
);
577577
});
578578

579-
it('mouse down drag', () => {
580-
const onScroll = jest.fn();
581-
const { container } = render(
582-
<List
583-
component="ul"
584-
itemKey="id"
585-
itemHeight={20}
586-
height={100}
587-
data={genData(100)}
588-
onScroll={onScroll}
589-
>
590-
{({ id }) => <li>{id}</li>}
591-
</List>,
592-
);
593-
594-
function dragDown(mouseY) {
579+
describe('mouse down drag', () => {
580+
function dragDown(container, mouseY) {
595581
fireEvent.mouseDown(container.querySelector('li'));
596582

597583
let moveEvent = createEvent.mouseMove(container.querySelector('li'));
@@ -605,18 +591,57 @@ describe('List.Scroll', () => {
605591
fireEvent.mouseUp(container.querySelector('li'));
606592
}
607593

608-
function getScrollTop() {
594+
function getScrollTop(container) {
609595
const innerEle = container.querySelector('.rc-virtual-list-holder-inner');
610596
const { transform } = innerEle.style;
611597
return Number(transform.match(/\d+/)[0]);
612598
}
613599

614-
// Drag down
615-
dragDown(100);
616-
expect(getScrollTop()).toBeGreaterThan(0);
600+
it('can move', () => {
601+
const onScroll = jest.fn();
602+
const { container } = render(
603+
<List
604+
component="ul"
605+
itemKey="id"
606+
itemHeight={20}
607+
height={100}
608+
data={genData(100)}
609+
onScroll={onScroll}
610+
>
611+
{({ id }) => <li>{id}</li>}
612+
</List>,
613+
);
617614

618-
// Drag up
619-
dragDown(-100);
620-
expect(getScrollTop()).toBe(0);
615+
// Drag down
616+
dragDown(container, 100);
617+
expect(getScrollTop(container)).toBeGreaterThan(0);
618+
619+
// Drag up
620+
dragDown(container, -100);
621+
expect(getScrollTop(container)).toBe(0);
622+
});
623+
624+
it('can not move when item add draggable', () => {
625+
const onScroll = jest.fn();
626+
const { container } = render(
627+
<List
628+
component="ul"
629+
itemKey="id"
630+
itemHeight={20}
631+
height={100}
632+
data={genData(100)}
633+
onScroll={onScroll}
634+
>
635+
{({ id }) => <li draggable>{id}</li>}
636+
</List>,
637+
);
638+
639+
// Initial scroll should be 0
640+
expect(getScrollTop(container)).toEqual(0);
641+
// Simulate drag action
642+
dragDown(container, 100);
643+
// Assert that scroll did not change after drag
644+
expect(getScrollTop(container)).toEqual(0);
645+
});
621646
});
622647
});

0 commit comments

Comments
 (0)