Skip to content

Commit

Permalink
Merge e5151e7 into d63bc29
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 13, 2021
2 parents d63bc29 + e5151e7 commit 3d87702
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Table.tsx
Expand Up @@ -165,7 +165,10 @@ export interface TableProps<RecordType = unknown> extends LegacyExpandableProps<
sticky?: boolean | TableSticky;
}

function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordType>) {
function InternalTable<RecordType extends DefaultRecordType>(
props: TableProps<RecordType>,
ref: React.MutableRefObject<HTMLDivElement>,
) {
const {
prefixCls,
className,
Expand Down Expand Up @@ -364,7 +367,9 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
);

// ====================== Scroll ======================
const fullTableRef = React.useRef<HTMLDivElement>();
let fullTableRef = React.useRef<HTMLDivElement>();
fullTableRef = ref || fullTableRef;

const scrollHeaderRef = React.useRef<HTMLDivElement>();
const scrollBodyRef = React.useRef<HTMLDivElement>();
const scrollSummaryRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -831,6 +836,18 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
);
}

const TableRef = React.forwardRef(InternalTable);

type InternalTableType = typeof TableRef;

interface TableInterface extends InternalTableType {
Column: typeof Column;
ColumnGroup: typeof ColumnGroup;
Summary: typeof Summary;
}

const Table = TableRef as TableInterface;

Table.Column = Column;

Table.ColumnGroup = ColumnGroup;
Expand Down
20 changes: 20 additions & 0 deletions tests/Table.spec.js
Expand Up @@ -931,4 +931,24 @@ describe('Table.Basic', () => {
expect(onExpandedRowsChange).toHaveBeenCalledWith(['parent', 'bamboo']);
});
});

it('should support custom ref', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});

const columns = [
{
title: 'Name',
key: 'name',
dataIndex: 'name',
},
];
const Wrapper = () => {
const ref = React.useRef();
return <Table ref={ref} columns={columns} />;
};
mount(<Wrapper />);
expect(spy).not.toBeCalled();

spy.mockRestore();
});
});

0 comments on commit 3d87702

Please sign in to comment.