Skip to content

Commit ed838a7

Browse files
committed
refactor: move hooks
1 parent 2c044ac commit ed838a7

File tree

3 files changed

+74
-64
lines changed

3 files changed

+74
-64
lines changed

src/StaticTable/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { CustomizeScrollBody } from '../interface';
55
import Table, { type TableProps } from '../Table';
66
import Grid from './BodyGrid';
77
import { StaticContext } from './context';
8-
import useWidthColumns from './useWidthColumns';
98
import { makeImmutable } from '../context/TableContext';
109
import { warning } from 'rc-util';
1110

@@ -53,7 +52,6 @@ function VirtualTable<RecordType>(props: StaticTableProps<RecordType>) {
5352
body: renderBody,
5453
}}
5554
columns={columns}
56-
// columns={filledWidthColumns}
5755
internalHooks={INTERNAL_HOOKS}
5856
tailor
5957
/>

src/hooks/useColumns/index.tsx

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@ import type {
1414
TriggerEventHandler,
1515
} from '../../interface';
1616
import { INTERNAL_COL_DEFINE } from '../../utils/legacyUtil';
17-
18-
function parseColWidth(totalWidth: number, width: string | number = '') {
19-
if (typeof width === 'number') {
20-
return width;
21-
}
22-
23-
if (width.endsWith('%')) {
24-
return (totalWidth * parseFloat(width)) / 100;
25-
}
26-
return null;
27-
}
17+
import useWidthColumns from './useWidthColumns';
2818

2919
export function convertChildrenToColumns<RecordType>(
3020
children: React.ReactNode,
@@ -272,66 +262,21 @@ function useColumns<RecordType>(
272262

273263
// ========================== Flatten =========================
274264
const flattenColumns = React.useMemo(() => {
275-
let tmpColumns = mergedColumns;
276-
277265
if (direction === 'rtl') {
278-
tmpColumns = revertForRtl(flatColumns(mergedColumns));
279-
} else {
280-
tmpColumns = flatColumns(mergedColumns);
266+
return revertForRtl(flatColumns(mergedColumns));
281267
}
282-
283-
// Fill width if needed
284-
if (scrollWidth && scrollWidth > 0) {
285-
let totalWidth = 0;
286-
let missWidthCount = 0;
287-
288-
// collect not given width column
289-
tmpColumns.forEach((col: any) => {
290-
const colWidth = parseColWidth(scrollWidth, col.width);
291-
292-
if (colWidth) {
293-
totalWidth += colWidth;
294-
} else {
295-
missWidthCount += 1;
296-
}
297-
});
298-
299-
// Fill width
300-
let restWidth = scrollWidth - totalWidth;
301-
let restCount = missWidthCount;
302-
const avgWidth = restWidth / missWidthCount;
303-
304-
tmpColumns = tmpColumns.map((col: any) => {
305-
const clone = {
306-
...col,
307-
};
308-
309-
const colWidth = parseColWidth(scrollWidth, clone.width);
310-
311-
if (colWidth) {
312-
clone.width = colWidth;
313-
} else {
314-
const colAvgWidth = Math.floor(avgWidth);
315-
316-
clone.width = restCount === 1 ? restWidth : colAvgWidth;
317-
318-
restWidth -= colAvgWidth;
319-
restCount -= 1;
320-
}
321-
322-
return clone;
323-
});
324-
}
325-
326-
return tmpColumns;
268+
return flatColumns(mergedColumns);
327269
}, [mergedColumns, direction, scrollWidth]);
328270

329271
// Only check out of production since it's waste for each render
330272
if (process.env.NODE_ENV !== 'production') {
331273
warningFixed(direction === 'rtl' ? flattenColumns.slice().reverse() : flattenColumns);
332274
}
333275

334-
return [mergedColumns, flattenColumns];
276+
// ========================= FillWidth ========================
277+
const filledColumns = useWidthColumns(flattenColumns, scrollWidth);
278+
279+
return [mergedColumns, filledColumns];
335280
}
336281

337282
export default useColumns;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as React from 'react';
2+
import type { ColumnsType } from '../../interface';
3+
4+
function parseColWidth(totalWidth: number, width: string | number = '') {
5+
if (typeof width === 'number') {
6+
return width;
7+
}
8+
9+
if (width.endsWith('%')) {
10+
return (totalWidth * parseFloat(width)) / 100;
11+
}
12+
return null;
13+
}
14+
15+
/**
16+
* Fill all column with width
17+
*/
18+
export default function useWidthColumns(columns: ColumnsType<any>, scrollWidth: number) {
19+
const filledColumns = React.useMemo(() => {
20+
// Fill width if needed
21+
if (scrollWidth && scrollWidth > 0) {
22+
let totalWidth = 0;
23+
let missWidthCount = 0;
24+
25+
// collect not given width column
26+
columns.forEach((col: any) => {
27+
const colWidth = parseColWidth(scrollWidth, col.width);
28+
29+
if (colWidth) {
30+
totalWidth += colWidth;
31+
} else {
32+
missWidthCount += 1;
33+
}
34+
});
35+
36+
// Fill width
37+
let restWidth = scrollWidth - totalWidth;
38+
let restCount = missWidthCount;
39+
const avgWidth = restWidth / missWidthCount;
40+
41+
return columns.map((col: any) => {
42+
const clone = {
43+
...col,
44+
};
45+
46+
const colWidth = parseColWidth(scrollWidth, clone.width);
47+
48+
if (colWidth) {
49+
clone.width = colWidth;
50+
} else {
51+
const colAvgWidth = Math.floor(avgWidth);
52+
53+
clone.width = restCount === 1 ? restWidth : colAvgWidth;
54+
55+
restWidth -= colAvgWidth;
56+
restCount -= 1;
57+
}
58+
59+
return clone;
60+
});
61+
}
62+
63+
return columns;
64+
}, [columns, scrollWidth]);
65+
66+
return filledColumns;
67+
}

0 commit comments

Comments
 (0)