Skip to content

Commit a82ab91

Browse files
authored
fix: ScrollTo should not out of range (#57)
* fix: Not scroll out of range * add test case
1 parent 36d8d26 commit a82ab91

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

examples/basic.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ const Demo = () => {
138138
visible
139139
</button>
140140

141+
<button
142+
type="button"
143+
onClick={() => {
144+
listRef.current.scrollTo({
145+
index: data.length - 2,
146+
align: 'top',
147+
});
148+
}}
149+
>
150+
Scroll To Last (top)
151+
</button>
152+
<button
153+
type="button"
154+
onClick={() => {
155+
listRef.current.scrollTo({
156+
index: 0,
157+
align: 'bottom',
158+
});
159+
}}
160+
>
161+
Scroll To First (bottom)
162+
</button>
163+
141164
<button
142165
type="button"
143166
onClick={() => {

src/List.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
103103
value = newTop;
104104
}
105105

106-
componentRef.current.scrollTop = value;
107-
return value;
106+
const alignedTop = keepInRange(value);
107+
108+
componentRef.current.scrollTop = alignedTop;
109+
return alignedTop;
108110
});
109111
}
110112

@@ -205,10 +207,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
205207

206208
// ================================ Scroll ================================
207209
function onScrollBar(newScrollTop: number) {
208-
const newTop = keepInRange(newScrollTop);
209-
if (newTop !== scrollTop) {
210-
syncScrollTop(newTop);
211-
}
210+
const newTop = newScrollTop;
211+
syncScrollTop(newTop);
212212
}
213213

214214
// This code may only trigger in test case.
@@ -230,8 +230,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
230230
isScrollAtBottom,
231231
offsetY => {
232232
syncScrollTop(top => {
233-
const newTop = keepInRange(top + offsetY);
234-
233+
const newTop = top + offsetY;
235234
return newTop;
236235
});
237236
},

tests/scroll.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ describe('List.Scroll', () => {
7070
expect(wrapper.find('ul').instance().scrollTop).toEqual(600);
7171
});
7272

73+
it('scroll top should not out of range', () => {
74+
listRef.current.scrollTo({ index: 0, align: 'bottom' });
75+
jest.runAllTimers();
76+
expect(wrapper.find('ul').instance().scrollTop).toEqual(0);
77+
});
78+
7379
it('key scroll', () => {
7480
listRef.current.scrollTo({ key: '30', align: 'bottom' });
7581
jest.runAllTimers();

0 commit comments

Comments
 (0)