Compare commits

...

2 Commits

Author SHA1 Message Date
crazyair
57d266d967 feat: test 2026-01-04 11:16:22 +08:00
crazyair
d25784fbc8 feat: table support auto index 2026-01-01 08:58:32 +08:00
2 changed files with 38 additions and 24 deletions

View File

@@ -60,10 +60,10 @@ import type {
TablePaginationPosition,
TableRowSelection,
} from './interface';
import TableMeasureRowContext from './TableMeasureRowContext';
import RcTable from './RcTable';
import RcVirtualTable from './RcTable/VirtualTable';
import useStyle from './style';
import TableMeasureRowContext from './TableMeasureRowContext';
export type { ColumnsType, TablePaginationConfig };
@@ -343,27 +343,6 @@ const InternalTable = <RecordType extends AnyObject = AnyObject>(
nativeElement: rootRef.current!,
}));
// ============================ RowKey ============================
const rowKey = customizeRowKey || table?.rowKey || 'key';
if (process.env.NODE_ENV !== 'production') {
warning(
!(typeof rowKey === 'function' && rowKey.length > 1),
'usage',
'`index` parameter of `rowKey` function is deprecated. There is no guarantee that it will work as expected.',
);
}
const getRowKey = React.useMemo<GetRowKey<RecordType>>(() => {
if (typeof rowKey === 'function') {
return rowKey;
}
return (record: RecordType) => record?.[rowKey as string];
}, [rowKey]);
const [getRecordByKey] = useLazyKVMap(rawData, childrenColumnName, getRowKey);
// ============================ Events =============================
const changeEventInfo: Partial<ChangeEventInfo<RecordType>> = {};
@@ -533,6 +512,35 @@ const InternalTable = <RecordType extends AnyObject = AnyObject>(
mergedPagination?.total,
]);
// ============================ RowKey ============================
const rowKey = customizeRowKey || table?.rowKey || 'key';
if (process.env.NODE_ENV !== 'production') {
warning(
!(typeof rowKey === 'function' && rowKey.length > 1),
'usage',
'`index` parameter of `rowKey` function is deprecated. There is no guarantee that it will work as expected.',
);
}
const getRowKey = React.useMemo<GetRowKey<RecordType>>(() => {
if (typeof rowKey === 'function') {
return rowKey;
}
return (record, index) => {
if (typeof customizeRowKey === 'undefined') {
if (mergedPagination) {
return `${mergedPagination.current}-${index}`;
}
return index;
}
return record?.[rowKey as string];
};
}, [rowKey, mergedPagination]);
const [getRecordByKey] = useLazyKVMap(rawData, childrenColumnName, getRowKey);
// ========================== Selections ==========================
const [transformSelectionColumns, selectedKeySet] = useSelection<RecordType>(
{

View File

@@ -5,7 +5,7 @@ import type { TableColumnsType, TableProps } from 'antd';
type TableRowSelection<T extends object = object> = TableProps<T>['rowSelection'];
interface DataType {
key: React.Key;
key?: React.Key;
name: string;
age: number;
address: string;
@@ -42,6 +42,7 @@ const App: React.FC = () => {
};
const rowSelection: TableRowSelection<DataType> = {
preserveSelectedRowKeys: true,
selectedRowKeys,
onChange: onSelectChange,
selections: [
@@ -79,7 +80,12 @@ const App: React.FC = () => {
],
};
return <Table<DataType> rowSelection={rowSelection} columns={columns} dataSource={dataSource} />;
return (
<>
{JSON.stringify(selectedRowKeys)}
<Table<DataType> rowSelection={rowSelection} columns={columns} dataSource={dataSource} />
</>
);
};
export default App;