Skip to content

Commit 4ffafff

Browse files
authored
fix: scroll.x not working when no fixed column exists (#1023)
* fix: table col cal * test: add test case
1 parent aa2aa44 commit 4ffafff

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/VirtualTable/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
3030

3131
// Fill scrollX
3232
if (typeof scrollX !== 'number') {
33+
if (process.env.NODE_ENV !== 'production') {
34+
warning(!scrollX, '`scroll.x` in virtual table must be number.');
35+
}
36+
3337
scrollX = 1;
3438
}
3539

src/hooks/useColumns/useWidthColumns.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ export default function useWidthColumns(
6767
return clone;
6868
});
6969

70+
const maxFitWidth = Math.max(scrollWidth, clientWidth);
71+
7072
// If realTotal is less than clientWidth,
7173
// We need extend column width
72-
if (realTotal < clientWidth) {
73-
const scale = clientWidth / realTotal;
74+
if (realTotal < maxFitWidth) {
75+
const scale = maxFitWidth / realTotal;
7476

75-
restWidth = clientWidth;
77+
restWidth = maxFitWidth;
7678

7779
filledColumns.forEach((col: any, index) => {
7880
const colWidth = Math.floor(col.width * scale);
@@ -83,7 +85,7 @@ export default function useWidthColumns(
8385
});
8486
}
8587

86-
return [filledColumns, Math.max(realTotal, clientWidth)];
88+
return [filledColumns, Math.max(realTotal, maxFitWidth)];
8789
}
8890

8991
return [flattenColumns, scrollWidth];

tests/Virtual.spec.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ describe('Table.Virtual', () => {
103103
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
104104

105105
getTable({
106-
scroll: {} as any,
106+
scroll: {
107+
x: true,
108+
} as any,
107109
});
108110

111+
expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.x` in virtual table must be number.');
109112
expect(errSpy).toHaveBeenCalledWith('Warning: `scroll.y` in virtual table must be number.');
110113
});
111114

@@ -199,7 +202,7 @@ describe('Table.Virtual', () => {
199202
},
200203
],
201204
scroll: {
202-
x: 1128,
205+
x: 100,
203206
y: 10,
204207
},
205208
data: [{}],
@@ -244,4 +247,25 @@ describe('Table.Virtual', () => {
244247
expect(container.querySelectorAll('col')[0]).toHaveStyle({ width: '100px' });
245248
expect(container.querySelectorAll('col')[1]).toHaveStyle({ width: '100px' });
246249
});
250+
251+
it('should fill width as scrollX if scrollX is larger', async () => {
252+
const { container } = getTable({
253+
columns: [
254+
{
255+
width: 100,
256+
},
257+
],
258+
scroll: {
259+
x: 1128,
260+
y: 10,
261+
},
262+
getContainerWidth: () => 200,
263+
data: [{}],
264+
});
265+
266+
expect(container.querySelector('.rc-virtual-list')).toHaveAttribute(
267+
'data-scroll-width',
268+
'1128',
269+
);
270+
});
247271
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
},
1515
"types": ["vitest/globals"]
1616
},
17-
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]
17+
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
1818
}

0 commit comments

Comments
 (0)