feat: support ref for Table (#32136)

* feat: support ref/prefix/suffix for Table

close #25306

* docs: documentat Table prefix/suffix props

* chore: fix lint error

* refactor: remove prefix/suffix props
This commit is contained in:
JounQin 2021-09-17 11:17:29 +08:00 committed by GitHub
parent 61c21a07f7
commit 15d55bc3e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -99,7 +99,10 @@ export interface TableProps<RecordType>
showSorterTooltip?: boolean | TooltipProps;
}
function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
function InternalTable<RecordType extends object = any>(
props: TableProps<RecordType>,
ref: React.MutableRefObject<HTMLDivElement>,
) {
const {
prefixCls: customizePrefixCls,
className,
@ -483,7 +486,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
className,
);
return (
<div className={wrapperClassNames} style={style}>
<div ref={ref} className={wrapperClassNames} style={style}>
<Spin spinning={false} {...spinProps}>
{topPaginationNode}
<RcTable<RecordType>
@ -513,6 +516,21 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
);
}
const TableRef = React.forwardRef(InternalTable);
type InternalTableType = typeof TableRef;
interface TableInterface extends InternalTableType {
SELECTION_ALL: 'SELECT_ALL';
SELECTION_INVERT: 'SELECT_INVERT';
SELECTION_NONE: 'SELECT_NONE';
Column: typeof Column;
ColumnGroup: typeof ColumnGroup;
Summary: typeof Summary;
}
const Table = TableRef as TableInterface;
Table.defaultProps = {
rowKey: 'key',
};

View File

@ -261,4 +261,21 @@ describe('Table', () => {
mount(<Table columns={columns} rowKey={record => record.key} />);
expect(warnSpy).not.toBeCalled();
});
it('should support ref', () => {
warnSpy.mockReset();
const columns = [
{
title: 'Name',
key: 'name',
dataIndex: 'name',
},
];
const Wrapper = () => {
const ref = React.useRef();
return <Table ref={ref} columns={columns} />;
};
mount(<Wrapper />);
expect(warnSpy).not.toBeCalled();
});
});