Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/hooks/useScrollTo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function useScrollTo<T>(
offset: number;
originAlign: ScrollAlign;
targetAlign?: 'top' | 'bottom';
lastTop?: number;
}>(null);

// ========================== Sync Scroll ==========================
Expand All @@ -63,6 +64,7 @@ export default function useScrollTo<T>(
const height = containerRef.current.clientHeight;
let needCollectHeight = false;
let newTargetAlign: 'top' | 'bottom' | null = targetAlign;
let targetTop: number | null = null;

// Go to next frame if height not exist
if (height) {
Expand All @@ -73,7 +75,7 @@ export default function useScrollTo<T>(
let itemTop = 0;
let itemBottom = 0;

const maxLen = Math.min(data.length, index);
const maxLen = Math.min(data.length - 1, index);

for (let i = 0; i <= maxLen; i += 1) {
const key = getKey(data[i]);
Expand Down Expand Up @@ -102,9 +104,6 @@ export default function useScrollTo<T>(
}

// Scroll to
let targetTop: number | null = null;
let inView = false;

switch (mergedAlign) {
case 'top':
targetTop = itemTop - offset;
Expand All @@ -120,16 +119,16 @@ export default function useScrollTo<T>(
newTargetAlign = 'top';
} else if (itemBottom > scrollBottom) {
newTargetAlign = 'bottom';
} else {
// No need to collect since already in view
inView = true;
}
}
}

if (targetTop !== null) {
syncScrollTop(targetTop);
} else if (!inView) {
}

// One more time for sync
if (targetTop !== syncState.lastTop) {
needCollectHeight = true;
}
}
Expand All @@ -140,12 +139,13 @@ export default function useScrollTo<T>(
...ori,
times: ori.times + 1,
targetAlign: newTargetAlign,
lastTop: targetTop,
}));
}
} else if (process.env.NODE_ENV !== 'production' && syncState?.times === MAX_TIMES) {
warning(
false,
'Seems `scrollTo` with `rc-virtual-list` reach toe max limitation. Please fire issue for us. Thanks.',
'Seems `scrollTo` with `rc-virtual-list` reach the max limitation. Please fire issue for us. Thanks.',
);
}
}, [syncState, containerRef.current]);
Expand Down
18 changes: 18 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mount } from 'enzyme';
import { spyElementPrototypes } from './utils/domHook';
import List from '../src';
import { createEvent, fireEvent, render } from '@testing-library/react';
import { resetWarned } from 'rc-util/lib/warning';

function genData(count) {
return new Array(count).fill(null).map((_, index) => ({ id: String(index) }));
Expand All @@ -24,6 +25,9 @@ describe('List.Scroll', () => {
width: 100,
height: 100,
}),
offsetParent: {
get: () => document.body,
},
});
});

Expand Down Expand Up @@ -137,6 +141,20 @@ describe('List.Scroll', () => {
scrollTo({ index: 30 });
expect(container.querySelector('ul').scrollTop).toEqual(600);
});

it('exceed should not warning', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error');

const { scrollTo } = presetList();
scrollTo({ index: 9999999999, align: 'top' });

errSpy.mock.calls.forEach((msgs) => {
expect(msgs[0]).not.toContain('max limitation');
});

errSpy.mockRestore();
});
});

it('inject wheel', () => {
Expand Down