mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 19:19:57 +08:00
parent
e613861e63
commit
0689982def
@ -797,4 +797,28 @@ describe('Table.rowSelection', () => {
|
||||
.simulate('change', { target: { checked: true } });
|
||||
expect(onChange.mock.calls[0][1]).toEqual([expect.objectContaining({ name: 'bamboo' })]);
|
||||
});
|
||||
|
||||
it('do not cache selected keys', () => {
|
||||
const onChange = jest.fn();
|
||||
const wrapper = mount(
|
||||
<Table
|
||||
dataSource={[{ name: 'light' }, { name: 'bamboo' }]}
|
||||
rowSelection={{ onChange }}
|
||||
rowKey="name"
|
||||
/>,
|
||||
);
|
||||
|
||||
wrapper
|
||||
.find('tbody input')
|
||||
.first()
|
||||
.simulate('change', { target: { checked: true } });
|
||||
expect(onChange).toHaveBeenCalledWith(['light'], [{ name: 'light' }]);
|
||||
|
||||
wrapper.setProps({ dataSource: [{ name: 'bamboo' }] });
|
||||
wrapper
|
||||
.find('tbody input')
|
||||
.first()
|
||||
.simulate('change', { target: { checked: true } });
|
||||
expect(onChange).toHaveBeenCalledWith(['bamboo'], [{ name: 'bamboo' }]);
|
||||
});
|
||||
});
|
||||
|
@ -117,12 +117,21 @@ export default function useSelection<RecordType>(
|
||||
|
||||
const setSelectedKeys = React.useCallback(
|
||||
(keys: Key[]) => {
|
||||
setInnerSelectedKeys(keys);
|
||||
const availableKeys: Key[] = [];
|
||||
const records: RecordType[] = [];
|
||||
|
||||
const records = keys.map(key => getRecordByKey(key));
|
||||
keys.forEach(key => {
|
||||
const record = getRecordByKey(key);
|
||||
if (record !== undefined) {
|
||||
availableKeys.push(key);
|
||||
records.push(record);
|
||||
}
|
||||
});
|
||||
|
||||
setInnerSelectedKeys(availableKeys);
|
||||
|
||||
if (onSelectionChange) {
|
||||
onSelectionChange(keys, records);
|
||||
onSelectionChange(availableKeys, records);
|
||||
}
|
||||
},
|
||||
[setInnerSelectedKeys, getRecordByKey, onSelectionChange],
|
||||
|
Loading…
Reference in New Issue
Block a user