Skip to content

Commit

Permalink
fix: should not out of range (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Jan 12, 2021
1 parent a8359eb commit b7c1a43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
maxScrollHeightRef.current = maxScrollHeight;

function keepInRange(newScrollTop: number) {
let newTop = Math.max(newScrollTop, 0);
let newTop = newScrollTop;
if (!Number.isNaN(maxScrollHeightRef.current)) {
newTop = Math.min(newTop, maxScrollHeightRef.current);
}
newTop = Math.max(newScrollTop, 0);
return newTop;
}

Expand All @@ -225,8 +226,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
syncScrollTop(newTop);
}

// This code may only trigger in test case.
// But we still need a sync if some special escape
// When data size reduce. It may trigger native scroll event back to fit scroll position
function onFallbackScroll(e: React.UIEvent<HTMLDivElement>) {
const { scrollTop: newScrollTop } = e.currentTarget;
if (newScrollTop !== scrollTop) {
Expand Down
29 changes: 29 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,33 @@ describe('List.Scroll', () => {
.instance(),
);
});

it('scroll should in range', () => {
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();

act(() => {
const wheelEvent = new Event('wheel');
wheelEvent.deltaY = 9999999;
ulElement.dispatchEvent(wheelEvent);

jest.runAllTimers();
});

wrapper.setProps({ data: genData(1) });
act(() => {
wrapper
.find('.rc-virtual-list-holder')
.props()
.onScroll({
currentTarget: {
scrollTop: 0,
},
});
});

wrapper.setProps({ data: genData(100) });

expect(wrapper.find('ScrollBar').props().scrollTop).toEqual(0);
});
});

1 comment on commit b7c1a43

@vercel
Copy link

@vercel vercel bot commented on b7c1a43 Jan 12, 2021

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.