Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use ref #1031

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/scrollY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const Test = () => {
Scroll To key 9
</button>
<Table
reference={tblRef}
ref={tblRef}
columns={columns}
data={data}
scroll={{ y: 300 }}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/virtual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const Demo = () => {

return mergedWidth;
}}
reference={tblRef}
ref={tblRef}
/>
</div>
);
Expand Down
26 changes: 19 additions & 7 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ export interface TableProps<RecordType = unknown>

sticky?: boolean | TableSticky;

reference?: React.Ref<Reference>;

// =================================== Internal ===================================
/**
* @private Internal usage, may remove by refactor. Should always use `columns` instead.
Expand Down Expand Up @@ -170,7 +168,10 @@ function defaultEmpty() {
return 'No Data';
}

function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<RecordType>) {
function Table<RecordType extends DefaultRecordType>(
tableProps: TableProps<RecordType>,
ref: React.Ref<Reference>,
) {
const props = {
rowKey: 'key',
prefixCls: DEFAULT_PREFIX,
Expand All @@ -188,7 +189,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
scroll,
tableLayout,
direction,
reference,

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

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

export function genTable(shouldTriggerRender?: CompareProps<typeof Table>): typeof Table {
return makeImmutable(Table, shouldTriggerRender);
export type ForwardGenericTable = (<RecordType extends DefaultRecordType = any>(
props: TableProps<RecordType> & { ref?: React.Ref<Reference> },
) => React.ReactElement) & {
displayName?: string;
};

const RefTable = React.forwardRef(Table) as ForwardGenericTable;

if (process.env.NODE_ENV !== 'production') {
RefTable.displayName = 'Table';
}

export function genTable(shouldTriggerRender?: CompareProps<typeof Table>) {
return makeImmutable(RefTable, shouldTriggerRender) as ForwardGenericTable;
}

const ImmutableTable = genTable();
Expand Down
1 change: 0 additions & 1 deletion src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
React.useImperativeHandle(ref, () => {
const obj = {
scrollTo: (config: ScrollConfig) => {
console.log('!!!!', config);
listRef.current?.scrollTo(config);
},
} as unknown as GridRef;
Expand Down
23 changes: 17 additions & 6 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { warning } from 'rc-util';
import * as React from 'react';
import { INTERNAL_HOOKS } from '../constant';
import { makeImmutable } from '../context/TableContext';
import type { CustomizeScrollBody } from '../interface';
import type { CustomizeScrollBody, Reference } from '../interface';
import Table, { DEFAULT_PREFIX, type TableProps } from '../Table';
import Grid from './BodyGrid';
import { StaticContext } from './context';
Expand All @@ -23,7 +23,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
listItemHeight?: number;
}

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

let { x: scrollX, y: scrollY } = scroll || {};
Expand Down Expand Up @@ -68,15 +68,26 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
columns={columns}
internalHooks={INTERNAL_HOOKS}
tailor
ref={ref}
/>
</StaticContext.Provider>
);
}

export function genVirtualTable(
shouldTriggerRender?: CompareProps<typeof Table>,
): typeof VirtualTable {
return makeImmutable(VirtualTable, shouldTriggerRender);
export type ForwardGenericVirtualTable = (<RecordType>(
props: TableProps<RecordType> & { ref?: React.Ref<Reference> },
) => React.ReactElement) & {
displayName?: string;
};

const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;

if (process.env.NODE_ENV !== 'production') {
RefVirtualTable.displayName = 'VirtualTable';
}

export function genVirtualTable(shouldTriggerRender?: CompareProps<typeof Table>) {
return makeImmutable(RefVirtualTable, shouldTriggerRender) as ForwardGenericVirtualTable;
}

export default genVirtualTable();
4 changes: 2 additions & 2 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Table.Virtual', () => {
});
}

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

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

tblRef.current.scrollTo({
index: 99,
Expand Down
2 changes: 1 addition & 1 deletion tests/refs.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Table.Ref', () => {
dataIndex: 'key',
},
]}
reference={ref}
ref={ref}
scroll={{
y: 10,
}}
Expand Down
Loading