From 4ffafffea1584b033e2fb4daf9cdeceff55187c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Tue, 19 Sep 2023 11:02:59 +0800 Subject: [PATCH] fix: scroll.x not working when no fixed column exists (#1023) * fix: table col cal * test: add test case --- src/VirtualTable/index.tsx | 4 ++++ src/hooks/useColumns/useWidthColumns.tsx | 10 +++++---- tests/Virtual.spec.tsx | 28 ++++++++++++++++++++++-- tsconfig.json | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/VirtualTable/index.tsx b/src/VirtualTable/index.tsx index c92aaf74b..57d6d68c7 100644 --- a/src/VirtualTable/index.tsx +++ b/src/VirtualTable/index.tsx @@ -30,6 +30,10 @@ function VirtualTable(props: VirtualTableProps) { // Fill scrollX if (typeof scrollX !== 'number') { + if (process.env.NODE_ENV !== 'production') { + warning(!scrollX, '`scroll.x` in virtual table must be number.'); + } + scrollX = 1; } diff --git a/src/hooks/useColumns/useWidthColumns.tsx b/src/hooks/useColumns/useWidthColumns.tsx index cacb04751..eb108da53 100644 --- a/src/hooks/useColumns/useWidthColumns.tsx +++ b/src/hooks/useColumns/useWidthColumns.tsx @@ -67,12 +67,14 @@ export default function useWidthColumns( return clone; }); + const maxFitWidth = Math.max(scrollWidth, clientWidth); + // If realTotal is less than clientWidth, // We need extend column width - if (realTotal < clientWidth) { - const scale = clientWidth / realTotal; + if (realTotal < maxFitWidth) { + const scale = maxFitWidth / realTotal; - restWidth = clientWidth; + restWidth = maxFitWidth; filledColumns.forEach((col: any, index) => { const colWidth = Math.floor(col.width * scale); @@ -83,7 +85,7 @@ export default function useWidthColumns( }); } - return [filledColumns, Math.max(realTotal, clientWidth)]; + return [filledColumns, Math.max(realTotal, maxFitWidth)]; } return [flattenColumns, scrollWidth]; diff --git a/tests/Virtual.spec.tsx b/tests/Virtual.spec.tsx index fc4400800..637a41f87 100644 --- a/tests/Virtual.spec.tsx +++ b/tests/Virtual.spec.tsx @@ -103,9 +103,12 @@ describe('Table.Virtual', () => { const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); getTable({ - scroll: {} as any, + scroll: { + x: true, + } as any, }); + expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.x` in virtual table must be number.'); expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.y` in virtual table must be number.'); }); @@ -199,7 +202,7 @@ describe('Table.Virtual', () => { }, ], scroll: { - x: 1128, + x: 100, y: 10, }, data: [{}], @@ -244,4 +247,25 @@ describe('Table.Virtual', () => { expect(container.querySelectorAll('col')[0]).toHaveStyle({ width: '100px' }); expect(container.querySelectorAll('col')[1]).toHaveStyle({ width: '100px' }); }); + + it('should fill width as scrollX if scrollX is larger', async () => { + const { container } = getTable({ + columns: [ + { + width: 100, + }, + ], + scroll: { + x: 1128, + y: 10, + }, + getContainerWidth: () => 200, + data: [{}], + }); + + expect(container.querySelector('.rc-virtual-list')).toHaveAttribute( + 'data-scroll-width', + '1128', + ); + }); }); diff --git a/tsconfig.json b/tsconfig.json index 857021e22..613a69611 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,5 +14,5 @@ }, "types": ["vitest/globals"] }, - "include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"] + "include": [".dumirc.ts", "**/*.ts", "**/*.tsx"] }