From efe0e90d98bf1a6ee639a64d3bbcb14e875b4bd2 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: Tue, 12 Sep 2023 16:54:02 +0800 Subject: [PATCH 1/2] chore: scrollWidth make it always in virtual --- examples/horizontal-scroll.tsx | 36 ++++++++++++++++++++++++++++------ src/List.tsx | 3 ++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/examples/horizontal-scroll.tsx b/examples/horizontal-scroll.tsx index 75c71c7c..6d368866 100644 --- a/examples/horizontal-scroll.tsx +++ b/examples/horizontal-scroll.tsx @@ -70,16 +70,29 @@ const MyItem: React.ForwardRefRenderFunction< const ForwardMyItem = React.forwardRef(MyItem); -const data: Item[] = []; -for (let i = 0; i < 10000; i += 1) { - data.push({ - id: `id_${i}`, - height: 30 + Math.random() * 10, - }); +function getData(count: number) { + const data: Item[] = []; + for (let i = 0; i < count; i += 1) { + data.push({ + id: `id_${i}`, + height: Math.round(30 + Math.random() * 10), + }); + } + return data; } const Demo = () => { const [rtl, setRTL] = React.useState(false); + const [count, setCount] = React.useState('1000'); + const [data, setData] = React.useState([]); + + React.useEffect(() => { + const num = Number(count); + if (!Number.isNaN(num)) { + setData(getData(num)); + } + }, [count]); + return (
@@ -91,8 +104,19 @@ const Demo = () => { RTL: {String(rtl)} + { + const num = e.target.value; + + setCount(num); + }} + /> +
extends Omit, 'children' * By default `scrollWidth` is same as container. * When set this, it will show the horizontal scrollbar and * `scrollWidth` will be used as the real width instead of container width. + * When set, `virtual` will always be enabled. */ scrollWidth?: number; @@ -106,7 +107,7 @@ export function RawList(props: ListProps, ref: React.Ref) { // ================================= MISC ================================= const useVirtual = !!(virtual !== false && height && itemHeight); - const inVirtual = useVirtual && data && itemHeight * data.length > height; + const inVirtual = useVirtual && data && (itemHeight * data.length > height || !!scrollWidth); const isRTL = direction === 'rtl'; const mergedClassName = classNames(prefixCls, { [`${prefixCls}-rtl`]: isRTL }, className); From d9ca237051e502804f0ed9f403436151de0adc98 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: Tue, 12 Sep 2023 16:55:41 +0800 Subject: [PATCH 2/2] test: add test case --- tests/scrollWidth.test.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/scrollWidth.test.tsx b/tests/scrollWidth.test.tsx index 2ca215f8..b64f9ac4 100644 --- a/tests/scrollWidth.test.tsx +++ b/tests/scrollWidth.test.tsx @@ -152,6 +152,24 @@ describe('List.scrollWidth', () => { expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 }); }); + it('trigger event when less count', async () => { + const onVirtualScroll = jest.fn(); + + const { container } = await genList({ + itemHeight: ITEM_HEIGHT, + height: 100, + data: genData(1), + scrollWidth: 1000, + onVirtualScroll, + }); + + // Wheel + fireEvent.wheel(container.querySelector('.rc-virtual-list-holder')!, { + deltaX: 123, + }); + expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 }); + }); + it('shift wheel', async () => { const onVirtualScroll = jest.fn();