Skip to content

Commit 6f935c6

Browse files
committed
feat: Add internal hook
1 parent 7d86997 commit 6f935c6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Table.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import { findAllChildrenKeys, renderExpandIcon } from './utils/expandUtil';
6868
// Used for conditions cache
6969
const EMPTY_DATA = [];
7070

71+
export const INTERNAL_HOOKS = 'rc-table-internal-hook';
72+
7173
export interface TableProps<RecordType extends DefaultRecordType>
7274
extends LegacyExpandableProps<RecordType> {
7375
prefixCls?: string;
@@ -101,14 +103,30 @@ export interface TableProps<RecordType extends DefaultRecordType>
101103
onHeaderRow?: GetComponentProps<ColumnType<RecordType>[]>;
102104
emptyText?: React.ReactNode | (() => React.ReactNode);
103105

104-
// Internal
106+
// =================================== Internal ===================================
107+
/**
108+
* @private Internal usage, may remove by refactor. Should always use `columns` instead.
109+
*
110+
* !!! DO NOT USE IN PRODUCTION ENVIRONMENT !!!
111+
*/
112+
internalHooks?: string;
113+
105114
/**
106115
* @private Internal usage, may remove by refactor. Should always use `columns` instead.
107116
*
108117
* !!! DO NOT USE IN PRODUCTION ENVIRONMENT !!!
109118
*/
110119
// Used for antd table transform column with additional column
111120
transformColumns?: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>;
121+
122+
/**
123+
* @private Internal usage, may remove by refactor.
124+
*
125+
* !!! DO NOT USE IN PRODUCTION ENVIRONMENT !!!
126+
*/
127+
internalRefs?: {
128+
body: React.MutableRefObject<HTMLDivElement>;
129+
};
112130
}
113131

114132
function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordType>) {
@@ -136,7 +154,9 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
136154
onHeaderRow,
137155

138156
// Internal
157+
internalHooks,
139158
transformColumns,
159+
internalRefs,
140160
} = props;
141161

142162
const mergedData = data || EMPTY_DATA;
@@ -279,7 +299,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
279299
expandIcon: mergedExpandIcon,
280300
expandIconColumnIndex,
281301
},
282-
transformColumns,
302+
internalHooks === INTERNAL_HOOKS ? transformColumns : null,
283303
);
284304

285305
const columnContext = {
@@ -360,6 +380,13 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
360380
}
361381
}, [fixColumn]);
362382

383+
// ================== INTERNAL HOOKS ==================
384+
React.useEffect(() => {
385+
if (internalHooks === INTERNAL_HOOKS && internalRefs) {
386+
internalRefs.body.current = scrollBodyRef.current;
387+
}
388+
});
389+
363390
// ====================== Render ======================
364391
const TableComponent = getComponent(['table'], 'table');
365392

@@ -421,6 +448,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
421448
if (fixHeader) {
422449
groupTableNode = (
423450
<>
451+
{/* Header Table */}
424452
{showHeader !== false && (
425453
<div
426454
style={{
@@ -434,6 +462,8 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
434462
<FixedHeader {...headerProps} {...columnContext} />
435463
</div>
436464
)}
465+
466+
{/* Body Table */}
437467
<div
438468
style={{
439469
...scrollXStyle,

tests/Table.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
33
import Table from '../src';
4+
import { INTERNAL_HOOKS } from '../src/Table';
45

56
describe('Table.Basic', () => {
67
const data = [{ key: 'key0', name: 'Lucy' }, { key: 'key1', name: 'Jack' }];
@@ -646,11 +647,29 @@ describe('Table.Basic', () => {
646647
it('transformColumns', () => {
647648
const wrapper = mount(
648649
createTable({
650+
internalHooks: INTERNAL_HOOKS,
649651
transformColumns: columns => [{ title: 'before' }, ...columns, { title: 'after' }],
650652
}),
651653
);
652654

653655
expect(wrapper.render()).toMatchSnapshot();
654656
});
657+
658+
it('internalRefs', () => {
659+
const internalRefs = {
660+
body: React.createRef(),
661+
};
662+
663+
mount(
664+
createTable({
665+
internalHooks: INTERNAL_HOOKS,
666+
internalRefs,
667+
scroll: { y: 20 },
668+
}),
669+
);
670+
671+
expect(internalRefs.body).toBeTruthy();
672+
expect(internalRefs.body.current instanceof HTMLDivElement).toBeTruthy();
673+
});
655674
});
656675
});

0 commit comments

Comments
 (0)