Skip to content

Commit 65cd4f8

Browse files
authored
refactor: use ref (#1031)
* refactor: move to merge into ref * chore: clean up
1 parent 979bafa commit 65cd4f8

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed

docs/examples/scrollY.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const Test = () => {
6060
Scroll To key 9
6161
</button>
6262
<Table
63-
reference={tblRef}
63+
ref={tblRef}
6464
columns={columns}
6565
data={data}
6666
scroll={{ y: 300 }}

docs/examples/virtual.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ const Demo = () => {
236236

237237
return mergedWidth;
238238
}}
239-
reference={tblRef}
239+
ref={tblRef}
240240
/>
241241
</div>
242242
);

src/Table.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ export interface TableProps<RecordType = unknown>
121121

122122
sticky?: boolean | TableSticky;
123123

124-
reference?: React.Ref<Reference>;
125-
126124
// =================================== Internal ===================================
127125
/**
128126
* @private Internal usage, may remove by refactor. Should always use `columns` instead.
@@ -170,7 +168,10 @@ function defaultEmpty() {
170168
return 'No Data';
171169
}
172170

173-
function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<RecordType>) {
171+
function Table<RecordType extends DefaultRecordType>(
172+
tableProps: TableProps<RecordType>,
173+
ref: React.Ref<Reference>,
174+
) {
174175
const props = {
175176
rowKey: 'key',
176177
prefixCls: DEFAULT_PREFIX,
@@ -188,7 +189,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
188189
scroll,
189190
tableLayout,
190191
direction,
191-
reference,
192192

193193
// Additional Part
194194
title,
@@ -314,7 +314,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
314314
const scrollBodyRef = React.useRef<HTMLDivElement>();
315315
const scrollBodyContainerRef = React.useRef<HTMLDivElement>();
316316

317-
React.useImperativeHandle(reference, () => {
317+
React.useImperativeHandle(ref, () => {
318318
return {
319319
nativeElement: fullTableRef.current,
320320
scrollTo: config => {
@@ -876,8 +876,20 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
876876
return <TableContext.Provider value={TableContextValue}>{fullTable}</TableContext.Provider>;
877877
}
878878

879-
export function genTable(shouldTriggerRender?: CompareProps<typeof Table>): typeof Table {
880-
return makeImmutable(Table, shouldTriggerRender);
879+
export type ForwardGenericTable = (<RecordType extends DefaultRecordType = any>(
880+
props: TableProps<RecordType> & { ref?: React.Ref<Reference> },
881+
) => React.ReactElement) & {
882+
displayName?: string;
883+
};
884+
885+
const RefTable = React.forwardRef(Table) as ForwardGenericTable;
886+
887+
if (process.env.NODE_ENV !== 'production') {
888+
RefTable.displayName = 'Table';
889+
}
890+
891+
export function genTable(shouldTriggerRender?: CompareProps<typeof Table>) {
892+
return makeImmutable(RefTable, shouldTriggerRender) as ForwardGenericTable;
881893
}
882894

883895
const ImmutableTable = genTable();

src/VirtualTable/BodyGrid.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
7373
React.useImperativeHandle(ref, () => {
7474
const obj = {
7575
scrollTo: (config: ScrollConfig) => {
76-
console.log('!!!!', config);
7776
listRef.current?.scrollTo(config);
7877
},
7978
} as unknown as GridRef;

src/VirtualTable/index.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { warning } from 'rc-util';
44
import * as React from 'react';
55
import { INTERNAL_HOOKS } from '../constant';
66
import { makeImmutable } from '../context/TableContext';
7-
import type { CustomizeScrollBody } from '../interface';
7+
import type { CustomizeScrollBody, Reference } from '../interface';
88
import Table, { DEFAULT_PREFIX, type TableProps } from '../Table';
99
import Grid from './BodyGrid';
1010
import { StaticContext } from './context';
@@ -23,7 +23,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
2323
listItemHeight?: number;
2424
}
2525

26-
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
26+
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: React.Ref<Reference>) {
2727
const { columns, scroll, sticky, prefixCls = DEFAULT_PREFIX, className, listItemHeight } = props;
2828

2929
let { x: scrollX, y: scrollY } = scroll || {};
@@ -68,15 +68,26 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
6868
columns={columns}
6969
internalHooks={INTERNAL_HOOKS}
7070
tailor
71+
ref={ref}
7172
/>
7273
</StaticContext.Provider>
7374
);
7475
}
7576

76-
export function genVirtualTable(
77-
shouldTriggerRender?: CompareProps<typeof Table>,
78-
): typeof VirtualTable {
79-
return makeImmutable(VirtualTable, shouldTriggerRender);
77+
export type ForwardGenericVirtualTable = (<RecordType>(
78+
props: TableProps<RecordType> & { ref?: React.Ref<Reference> },
79+
) => React.ReactElement) & {
80+
displayName?: string;
81+
};
82+
83+
const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;
84+
85+
if (process.env.NODE_ENV !== 'production') {
86+
RefVirtualTable.displayName = 'VirtualTable';
87+
}
88+
89+
export function genVirtualTable(shouldTriggerRender?: CompareProps<typeof Table>) {
90+
return makeImmutable(RefVirtualTable, shouldTriggerRender) as ForwardGenericVirtualTable;
8091
}
8192

8293
export default genVirtualTable();

tests/Virtual.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('Table.Virtual', () => {
7777
});
7878
}
7979

80-
function getTable(props?: Partial<VirtualTableProps<any>>) {
80+
function getTable(props?: Partial<VirtualTableProps<any>> & { ref?: React.Ref<Reference> }) {
8181
return render(
8282
<VirtualTable
8383
columns={[
@@ -311,7 +311,7 @@ describe('Table.Virtual', () => {
311311

312312
it('scrollTo should pass', async () => {
313313
const tblRef = React.createRef<Reference>();
314-
getTable({ reference: tblRef });
314+
getTable({ ref: tblRef });
315315

316316
tblRef.current.scrollTo({
317317
index: 99,

tests/refs.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Table.Ref', () => {
3434
dataIndex: 'key',
3535
},
3636
]}
37-
reference={ref}
37+
ref={ref}
3838
scroll={{
3939
y: 10,
4040
}}

0 commit comments

Comments
 (0)