Skip to content

Commit b7c1a43

Browse files
authored
fix: should not out of range (#101)
1 parent a8359eb commit b7c1a43

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/List.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
207207
maxScrollHeightRef.current = maxScrollHeight;
208208

209209
function keepInRange(newScrollTop: number) {
210-
let newTop = Math.max(newScrollTop, 0);
210+
let newTop = newScrollTop;
211211
if (!Number.isNaN(maxScrollHeightRef.current)) {
212212
newTop = Math.min(newTop, maxScrollHeightRef.current);
213213
}
214+
newTop = Math.max(newScrollTop, 0);
214215
return newTop;
215216
}
216217

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

228-
// This code may only trigger in test case.
229-
// But we still need a sync if some special escape
229+
// When data size reduce. It may trigger native scroll event back to fit scroll position
230230
function onFallbackScroll(e: React.UIEvent<HTMLDivElement>) {
231231
const { scrollTop: newScrollTop } = e.currentTarget;
232232
if (newScrollTop !== scrollTop) {

tests/scroll.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,33 @@ describe('List.Scroll', () => {
222222
.instance(),
223223
);
224224
});
225+
226+
it('scroll should in range', () => {
227+
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
228+
const ulElement = wrapper.find('ul').instance();
229+
230+
act(() => {
231+
const wheelEvent = new Event('wheel');
232+
wheelEvent.deltaY = 9999999;
233+
ulElement.dispatchEvent(wheelEvent);
234+
235+
jest.runAllTimers();
236+
});
237+
238+
wrapper.setProps({ data: genData(1) });
239+
act(() => {
240+
wrapper
241+
.find('.rc-virtual-list-holder')
242+
.props()
243+
.onScroll({
244+
currentTarget: {
245+
scrollTop: 0,
246+
},
247+
});
248+
});
249+
250+
wrapper.setProps({ data: genData(100) });
251+
252+
expect(wrapper.find('ScrollBar').props().scrollTop).toEqual(0);
253+
});
225254
});

0 commit comments

Comments
 (0)