Skip to content

Commit 84a5b44

Browse files
authored
fix: Width exceed logic (#1034)
* fix: over strecth logic * test: add test case
1 parent dcdfb08 commit 84a5b44

File tree

6 files changed

+154
-7
lines changed

6 files changed

+154
-7
lines changed

docs/demo/virtual-columns.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Virtual Columns
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/virtual-columns.tsx"></code>

docs/examples/fixedColumns.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react';
1+
import type { ColumnType } from 'rc-table';
22
import Table from 'rc-table';
3-
import { ColumnType } from 'rc-table/lib/interface';
4-
import './assets/index.less';
3+
import React from 'react';
4+
import '../../assets/index.less';
55

66
interface RecordType {
77
a: string;

docs/examples/virtual-columns.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import '../../assets/index.less';
3+
import { VirtualTable } from '../../src';
4+
import type { ColumnsType } from '../../src/interface';
5+
6+
interface RecordType {
7+
a: string;
8+
b?: string;
9+
c?: string;
10+
}
11+
12+
const columns1: ColumnsType = [
13+
{ title: 'title1', dataIndex: 'a', width: 100 },
14+
{ title: 'title2', dataIndex: 'b', width: 100 },
15+
{
16+
title: 'title13',
17+
dataIndex: 'c',
18+
},
19+
];
20+
21+
const columns2: ColumnsType = [
22+
{ title: 'title1', dataIndex: 'a', width: 100 },
23+
{ title: 'title2', dataIndex: 'b', width: 100 },
24+
];
25+
26+
const columns3: ColumnsType = [
27+
{ title: 'title1', dataIndex: 'a', width: 500 },
28+
{ title: 'title2', dataIndex: 'b', width: 500 },
29+
];
30+
31+
const data: RecordType[] = new Array(4 * 10000).fill(null).map((_, index) => ({
32+
a: `a${index}`,
33+
b: `b${index}`,
34+
c: `c${index}`,
35+
}));
36+
37+
const Demo = () => {
38+
const [columns, setColumns] = React.useState(columns1);
39+
40+
return (
41+
<div style={{ width: 800, padding: `0 64px` }}>
42+
<label>
43+
<input type="radio" checked={columns === columns1} onChange={() => setColumns(columns1)} />
44+
Fill Rest
45+
</label>
46+
<label>
47+
<input type="radio" checked={columns === columns2} onChange={() => setColumns(columns2)} />
48+
Stretch
49+
</label>
50+
<label>
51+
<input type="radio" checked={columns === columns3} onChange={() => setColumns(columns3)} />
52+
Over Size
53+
</label>
54+
55+
<VirtualTable
56+
getContainerWidth={(_, w) => w - 1}
57+
columns={columns}
58+
scroll={{ y: 200 }}
59+
data={data}
60+
rowKey="a"
61+
/>
62+
</div>
63+
);
64+
};
65+
66+
export default Demo;

src/hooks/useColumns/useWidthColumns.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export default function useWidthColumns(
3838
});
3939

4040
// Fill width
41-
let restWidth = Math.max(scrollWidth - totalWidth, missWidthCount);
41+
const maxFitWidth = Math.max(scrollWidth, clientWidth);
42+
let restWidth = Math.max(maxFitWidth - totalWidth, missWidthCount);
4243
let restCount = missWidthCount;
4344
const avgWidth = restWidth / missWidthCount;
4445

@@ -67,8 +68,6 @@ export default function useWidthColumns(
6768
return clone;
6869
});
6970

70-
const maxFitWidth = Math.max(scrollWidth, clientWidth);
71-
7271
// If realTotal is less than clientWidth,
7372
// We need extend column width
7473
if (realTotal < maxFitWidth) {

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EXPAND_COLUMN, INTERNAL_HOOKS } from './constant';
22
import { FooterComponents as Summary } from './Footer';
3-
import type { Reference } from './interface';
3+
import type { ColumnType, Reference } from './interface';
44
import Column from './sugar/Column';
55
import ColumnGroup from './sugar/ColumnGroup';
66
import type { TableProps } from './Table';
@@ -22,6 +22,7 @@ export {
2222
genVirtualTable,
2323
type VirtualTableProps,
2424
type Reference,
25+
type ColumnType,
2526
};
2627

2728
export default Table;

tests/Virtual.spec.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,77 @@ describe('Table.Virtual', () => {
323323
index: 99,
324324
});
325325
});
326+
327+
describe('auto width', () => {
328+
async function prepareTable(columns: any[]) {
329+
const { container } = getTable({
330+
getContainerWidth: () => 300,
331+
columns: columns,
332+
});
333+
334+
resize(container.querySelector('.rc-table')!);
335+
await waitFakeTimer();
336+
337+
return container;
338+
}
339+
340+
it('fill rest', async () => {
341+
const container = await prepareTable([
342+
{
343+
dataIndex: 'name',
344+
width: 100,
345+
},
346+
{
347+
dataIndex: 'age',
348+
},
349+
]);
350+
351+
expect(container.querySelectorAll('col')[0]).toHaveStyle({
352+
width: '100px',
353+
});
354+
expect(container.querySelectorAll('col')[1]).toHaveStyle({
355+
width: '200px',
356+
});
357+
});
358+
359+
it('stretch', async () => {
360+
const container = await prepareTable([
361+
{
362+
dataIndex: 'name',
363+
width: 100,
364+
},
365+
{
366+
dataIndex: 'age',
367+
width: 100,
368+
},
369+
]);
370+
371+
expect(container.querySelectorAll('col')[0]).toHaveStyle({
372+
width: '150px',
373+
});
374+
expect(container.querySelectorAll('col')[1]).toHaveStyle({
375+
width: '150px',
376+
});
377+
});
378+
379+
it('exceed', async () => {
380+
const container = await prepareTable([
381+
{
382+
dataIndex: 'name',
383+
width: 500,
384+
},
385+
{
386+
dataIndex: 'age',
387+
width: 600,
388+
},
389+
]);
390+
391+
expect(container.querySelectorAll('col')[0]).toHaveStyle({
392+
width: '500px',
393+
});
394+
expect(container.querySelectorAll('col')[1]).toHaveStyle({
395+
width: '600px',
396+
});
397+
});
398+
});
326399
});

0 commit comments

Comments
 (0)