mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 06:03:38 +08:00
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:
parent
47f30ee266
commit
f801318868
@ -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' }];
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user