fix(Table): preserveSelectedRowKeys should work when checkStrictly is false (#42784)

* fix(Table): preserveSelectedRowKeys should work when checkStrictly is false

* fix: 修改

* fix: use Set instead of Map
This commit is contained in:
linxianxi 2023-06-07 23:01:09 +08:00 committed by GitHub
parent 47f30ee266
commit f801318868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 12 deletions

View File

@ -1606,6 +1606,34 @@ describe('Table.rowSelection', () => {
);
});
it('cache with preserveSelectedRowKeys and checkStrictly false', () => {
const onChange = jest.fn();
const { container, rerender } = render(
<Table
dataSource={[{ name: 'light' }, { name: 'bamboo' }]}
rowSelection={{ onChange, preserveSelectedRowKeys: true, checkStrictly: false }}
rowKey="name"
/>,
);
fireEvent.click(container.querySelector('tbody input')!);
expect(onChange).toHaveBeenCalledWith(['light'], [{ name: 'light' }], { type: 'single' });
rerender(
<Table
dataSource={[{ name: 'bamboo' }]}
rowSelection={{ onChange, preserveSelectedRowKeys: true, checkStrictly: false }}
rowKey="name"
/>,
);
fireEvent.click(container.querySelector('tbody input')!);
expect(onChange).toHaveBeenCalledWith(
['light', 'bamboo'],
[{ name: 'light' }, { name: 'bamboo' }],
{ type: 'single' },
);
});
it('works with receive selectedRowKeys from [] to undefined', () => {
const onChange = jest.fn();
const dataSource = [{ name: 'Jack' }];

View File

@ -14,9 +14,10 @@ import type { CheckboxProps } from '../../checkbox';
import Checkbox from '../../checkbox';
import Dropdown from '../../dropdown';
import Radio from '../../radio';
import type { AnyObject } from '../Table';
import type {
ColumnsType,
ColumnType,
ColumnsType,
ExpandType,
GetPopupContainer,
GetRowKey,
@ -27,7 +28,6 @@ import type {
TableRowSelection,
TransformColumns,
} from '../interface';
import type { AnyObject } from '../Table';
// TODO: warning if use ajax!!!
@ -143,16 +143,25 @@ const useSelection = <RecordType extends AnyObject = any>(
updatePreserveRecordsCache(mergedSelectedKeys);
}, [mergedSelectedKeys]);
const { keyEntities } = useMemo(
() =>
checkStrictly
? { keyEntities: null }
: convertDataToEntities(data as unknown as DataNode[], {
externalGetKey: getRowKey as any,
childrenPropName: childrenColumnName,
}),
[data, getRowKey, checkStrictly, childrenColumnName],
);
const { keyEntities } = useMemo(() => {
if (checkStrictly) {
return { keyEntities: null };
}
let convertData = data;
if (preserveSelectedRowKeys) {
const keysSet = new Set(data.map((record, index) => getRowKey(record, index)));
// remove preserveRecords that duplicate data
const preserveRecords = Array.from(preserveRecordsRef.current).reduce(
(total: RecordType[], [key, value]) => (keysSet.has(key) ? total : total.concat(value)),
[],
);
convertData = [...convertData, ...preserveRecords];
}
return convertDataToEntities(convertData as unknown as DataNode[], {
externalGetKey: getRowKey as any,
childrenPropName: childrenColumnName,
});
}, [data, getRowKey, checkStrictly, childrenColumnName, preserveSelectedRowKeys]);
// Get flatten data
const flattedData = useMemo(