Skip to content

Commit

Permalink
fix(useVirtualList): get correct offset when itemHeight is fixed (#2279)
Browse files Browse the repository at this point in the history
Co-authored-by: huangcheng <huangcheng.lq@bytedance.com>
  • Loading branch information
leedawn and hchlq authored Apr 21, 2024
1 parent 6d3c3ba commit 3ba7cb6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/hooks/src/useVirtualList/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('useVirtualList', () => {

expect(hook.result.current[0].length).toBe(10);
expect(container.scrollTop).toBe(20 * 30);
expect(hook.result.current[0][0].data).toBe(20);
expect(hook.result.current[0][0].index).toBe(20);
});

it('test with dynamic height', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/useVirtualList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {

const getOffset = (scrollTop: number) => {
if (isNumber(itemHeightRef.current)) {
return Math.floor(scrollTop / itemHeightRef.current) + 1;
return Math.floor(scrollTop / itemHeightRef.current);
}
let sum = 0;
let offset = 0;
Expand Down

0 comments on commit 3ba7cb6

Please sign in to comment.