Skip to content

Commit 2c044ac

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

File tree

5 files changed

+126
-118
lines changed

5 files changed

+126
-118
lines changed

src/StaticTable/index.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Grid from './BodyGrid';
77
import { StaticContext } from './context';
88
import useWidthColumns from './useWidthColumns';
99
import { makeImmutable } from '../context/TableContext';
10+
import { warning } from 'rc-util';
1011

1112
const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
1213
const { ref, onScroll } = props;
@@ -21,25 +22,40 @@ export interface StaticTableProps<RecordType> extends Omit<TableProps<RecordType
2122
};
2223
}
2324

25+
const PRESET_COLUMN_WIDTH = 100;
26+
2427
function VirtualTable<RecordType>(props: StaticTableProps<RecordType>) {
2528
const { columns, scroll } = props;
2629

2730
const { x: scrollX, y: scrollY } = scroll || {};
31+
let mergedScrollX = scrollX;
2832

2933
// Fill all column with width
30-
const filledWidthColumns = useWidthColumns(columns, scrollX);
34+
// const filledWidthColumns = useWidthColumns(columns, scrollX);
35+
if (typeof scrollX !== 'number') {
36+
mergedScrollX = ((columns || []).length + 1) * PRESET_COLUMN_WIDTH;
37+
38+
if (process.env.NODE_ENV !== 'production') {
39+
warning(false, '`scroll.x` in virtual table must be number.');
40+
}
41+
}
3142

3243
// ========================== Render ==========================
3344
return (
34-
<StaticContext.Provider value={{ scrollX, scrollY }}>
45+
<StaticContext.Provider value={{ scrollX: mergedScrollX, scrollY }}>
3546
<Table
3647
{...props}
48+
scroll={{
49+
...scroll,
50+
x: mergedScrollX,
51+
}}
3752
components={{
3853
body: renderBody,
3954
}}
40-
columns={filledWidthColumns}
55+
columns={columns}
56+
// columns={filledWidthColumns}
4157
internalHooks={INTERNAL_HOOKS}
42-
hideScrollColumn
58+
tailor
4359
/>
4460
</StaticContext.Provider>
4561
);

src/StaticTable/useWidthColumns.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/Table.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ export interface TableProps<RecordType = unknown>
138138
* !!! DO NOT USE IN PRODUCTION ENVIRONMENT !!!
139139
*/
140140
// Force trade scrollbar as 0 size.
141-
hideScrollColumn?: boolean;
141+
// Force column to be average width if not set
142+
tailor?: boolean;
142143

143144
/**
144145
* @private Internal usage, may remove by refactor.
@@ -193,7 +194,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
193194
internalHooks,
194195
transformColumns,
195196
internalRefs,
196-
hideScrollColumn,
197+
tailor,
197198

198199
sticky,
199200
} = props;
@@ -276,6 +277,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
276277
expandIcon: mergedExpandIcon,
277278
expandIconColumnIndex: expandableConfig.expandIconColumnIndex,
278279
direction,
280+
scrollWidth: useInternalHooks && tailor && typeof scroll?.x === 'number' ? scroll.x : null,
279281
},
280282
useInternalHooks ? transformColumns : null,
281283
);
@@ -450,7 +452,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
450452
const [supportSticky, setSupportSticky] = React.useState(true); // Only IE not support, we mark as support first
451453

452454
React.useEffect(() => {
453-
if (!hideScrollColumn || !useInternalHooks) {
455+
if (!tailor || !useInternalHooks) {
454456
if (scrollBodyRef.current instanceof Element) {
455457
setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.current).width);
456458
} else {

src/hooks/useColumns.tsx renamed to src/hooks/useColumns/index.tsx

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import toArray from 'rc-util/lib/Children/toArray';
22
import warning from 'rc-util/lib/warning';
33
import * as React from 'react';
4-
import { EXPAND_COLUMN } from '../constant';
4+
import { EXPAND_COLUMN } from '../../constant';
55
import type {
66
ColumnGroupType,
77
ColumnsType,
@@ -12,8 +12,19 @@ import type {
1212
Key,
1313
RenderExpandIcon,
1414
TriggerEventHandler,
15-
} from '../interface';
16-
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';
15+
} from '../../interface';
16+
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+
}
1728

1829
export function convertChildrenToColumns<RecordType>(
1930
children: React.ReactNode,
@@ -130,6 +141,7 @@ function useColumns<RecordType>(
130141
expandRowByClick,
131142
columnWidth,
132143
fixed,
144+
scrollWidth,
133145
}: {
134146
prefixCls?: string;
135147
columns?: ColumnsType<RecordType>;
@@ -146,6 +158,7 @@ function useColumns<RecordType>(
146158
expandRowByClick?: boolean;
147159
columnWidth?: number | string;
148160
fixed?: FixedType;
161+
scrollWidth?: number;
149162
},
150163
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
151164
): [ColumnsType<RecordType>, readonly ColumnType<RecordType>[]] {
@@ -259,15 +272,65 @@ function useColumns<RecordType>(
259272

260273
// ========================== Flatten =========================
261274
const flattenColumns = React.useMemo(() => {
275+
let tmpColumns = mergedColumns;
276+
262277
if (direction === 'rtl') {
263-
return revertForRtl(flatColumns(mergedColumns));
278+
tmpColumns = revertForRtl(flatColumns(mergedColumns));
279+
} else {
280+
tmpColumns = flatColumns(mergedColumns);
281+
}
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+
});
264324
}
265-
return flatColumns(mergedColumns);
266-
}, [mergedColumns, direction]);
325+
326+
return tmpColumns;
327+
}, [mergedColumns, direction, scrollWidth]);
328+
267329
// Only check out of production since it's waste for each render
268330
if (process.env.NODE_ENV !== 'production') {
269331
warningFixed(direction === 'rtl' ? flattenColumns.slice().reverse() : flattenColumns);
270332
}
333+
271334
return [mergedColumns, flattenColumns];
272335
}
273336

tests/FixedHeader.spec.jsx

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,27 @@ describe('Table.FixedHeader', () => {
3636
scroll={{ y: 10 }}
3737
/>,
3838
);
39-
wrapper
40-
.find(RcResizeObserver.Collection)
41-
.first()
42-
.props()
43-
.onBatchResize([
44-
{
45-
data: wrapper.find('ResizeObserver').at(0).props().data,
46-
size: { width: 100, offsetWidth: 100 },
47-
},
48-
{
49-
data: wrapper.find('ResizeObserver').at(1).props().data,
50-
size: { width: 200, offsetWidth: 200 },
51-
},
52-
{
53-
data: wrapper.find('ResizeObserver').at(2).props().data,
54-
size: { width: 0, offsetWidth: 0 },
55-
},
56-
]);
57-
await safeAct(wrapper);
39+
40+
async function triggerResize(resizeList) {
41+
wrapper.find(RcResizeObserver.Collection).first().props().onBatchResize(resizeList);
42+
await safeAct(wrapper);
43+
wrapper.update();
44+
}
45+
46+
await triggerResize([
47+
{
48+
data: wrapper.find('ResizeObserver').at(0).props().data,
49+
size: { width: 100, offsetWidth: 100 },
50+
},
51+
{
52+
data: wrapper.find('ResizeObserver').at(1).props().data,
53+
size: { width: 200, offsetWidth: 200 },
54+
},
55+
{
56+
data: wrapper.find('ResizeObserver').at(2).props().data,
57+
size: { width: 0, offsetWidth: 0 },
58+
},
59+
]);
5860

5961
expect(wrapper.find('.rc-table-header table').props().style.visibility).toBeFalsy();
6062

@@ -64,7 +66,17 @@ describe('Table.FixedHeader', () => {
6466

6567
// Update columns
6668
wrapper.setProps({ columns: [col2, col1] });
67-
wrapper.update();
69+
70+
await triggerResize([
71+
{
72+
data: wrapper.find('ResizeObserver').at(0).props().data,
73+
size: { width: 200, offsetWidth: 200 },
74+
},
75+
{
76+
data: wrapper.find('ResizeObserver').at(1).props().data,
77+
size: { width: 100, offsetWidth: 100 },
78+
},
79+
]);
6880

6981
expect(wrapper.find('colgroup col').at(0).props().style.width).toEqual(200);
7082
expect(wrapper.find('colgroup col').at(1).props().style.width).toEqual(100);

0 commit comments

Comments
 (0)