Skip to content

Commit

Permalink
chore: Lock points when scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 20, 2020
1 parent f224b24 commit aa2cc04
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion examples/animate.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.motion {
transition: all .3s;
transition: all 0.3s;
}

.item {
Expand All @@ -11,6 +11,10 @@
line-height: 31px;
position: relative;

&:hover {
background: rgba(255, 0, 0, 0.1);
}

&::after {
content: '';
border-bottom: 1px solid gray;
Expand Down
11 changes: 11 additions & 0 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
virtual !== false && height && itemHeight && data && itemHeight * data.length > height;

const [scrollTop, setScrollTop] = React.useState(0);
const [scrollMoving, setScrollMoving] = React.useState(false);

const mergedClassName = classNames(prefixCls, className);
const mergedData = data || EMPTY_DATA;
Expand Down Expand Up @@ -241,6 +242,10 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {

if (inVirtual) {
componentStyle.overflowY = 'hidden';

if (scrollMoving) {
componentStyle.pointerEvents = 'none';
}
}
}

Expand Down Expand Up @@ -277,6 +282,12 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
scrollHeight={scrollHeight}
count={mergedData.length}
onScroll={onScrollBar}
onStartMove={() => {
setScrollMoving(true);
}}
onStopMove={() => {
setScrollMoving(false);
}}
/>
)}
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/ScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface ScrollBarProps {
height: number;
count: number;
onScroll: (scrollTop: number) => void;
onStartMove: () => void;
onStopMove: () => void;
}

interface ScrollBarState {
Expand Down Expand Up @@ -69,12 +71,15 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
};

onMouseDown: React.MouseEventHandler = e => {
const { onStartMove } = this.props;

this.setState({
dragging: true,
pageY: e.pageY,
startTop: this.getTop(),
});

onStartMove();
this.patchEvents();
e.stopPropagation();
e.preventDefault();
Expand Down Expand Up @@ -102,7 +107,10 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
};

onMouseUp = () => {
const { onStopMove } = this.props;
this.setState({ dragging: false });

onStopMove();
this.removeEvents();
};

Expand Down
17 changes: 17 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ describe('List.Scroll', () => {
window.dispatchEvent(mouseMoveEvent);
});

expect(wrapper.find('.rc-virtual-list-holder').props().style.pointerEvents).toEqual('none');

act(() => {
jest.runAllTimers();
});
Expand All @@ -142,4 +144,19 @@ describe('List.Scroll', () => {
expect(wrapper.find('.rc-virtual-list-scrollbar-thumb')).toHaveLength(0);
});
});

it('no bubble', () => {
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });

// Mouse down
const preventDefault = jest.fn();
const stopPropagation = jest.fn();
wrapper.find('.rc-virtual-list-scrollbar').simulate('mousedown', {
preventDefault,
stopPropagation,
});

expect(preventDefault).toHaveBeenCalled();
expect(stopPropagation).toHaveBeenCalled();
});
});

1 comment on commit aa2cc04

@vercel
Copy link

@vercel vercel bot commented on aa2cc04 Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.