From 80dd12932c941c5b6afc4e1eeb82ebac4d3d8fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 20 Sep 2023 11:32:43 +0800 Subject: [PATCH 1/2] refactor: use one more time --- src/hooks/useScrollTo.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hooks/useScrollTo.tsx b/src/hooks/useScrollTo.tsx index 04030b73..a7903fab 100644 --- a/src/hooks/useScrollTo.tsx +++ b/src/hooks/useScrollTo.tsx @@ -45,6 +45,7 @@ export default function useScrollTo( offset: number; originAlign: ScrollAlign; targetAlign?: 'top' | 'bottom'; + lastTop?: number; }>(null); // ========================== Sync Scroll ========================== @@ -63,6 +64,7 @@ export default function useScrollTo( 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) { @@ -73,7 +75,7 @@ export default function useScrollTo( 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]); @@ -102,9 +104,6 @@ export default function useScrollTo( } // Scroll to - let targetTop: number | null = null; - let inView = false; - switch (mergedAlign) { case 'top': targetTop = itemTop - offset; @@ -120,16 +119,16 @@ export default function useScrollTo( 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; } } @@ -140,6 +139,7 @@ export default function useScrollTo( ...ori, times: ori.times + 1, targetAlign: newTargetAlign, + lastTop: targetTop, })); } } else if (process.env.NODE_ENV !== 'production' && syncState?.times === MAX_TIMES) { From 2457da9ec880716b0c1fd86553dd45bc37f73117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 20 Sep 2023 13:52:04 +0800 Subject: [PATCH 2/2] test: update test --- src/hooks/useScrollTo.tsx | 2 +- tests/scroll.test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/hooks/useScrollTo.tsx b/src/hooks/useScrollTo.tsx index a7903fab..e55a4a96 100644 --- a/src/hooks/useScrollTo.tsx +++ b/src/hooks/useScrollTo.tsx @@ -145,7 +145,7 @@ export default function useScrollTo( } 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]); diff --git a/tests/scroll.test.js b/tests/scroll.test.js index dd61bf96..ee60b303 100644 --- a/tests/scroll.test.js +++ b/tests/scroll.test.js @@ -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) })); @@ -24,6 +25,9 @@ describe('List.Scroll', () => { width: 100, height: 100, }), + offsetParent: { + get: () => document.body, + }, }); }); @@ -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', () => {