fix: Table selection cache (#24338)

* test driven

* fix logic
This commit is contained in:
二货机器人 2020-05-21 11:02:12 +08:00 committed by GitHub
parent e613861e63
commit 0689982def
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -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' }]);
});
});

View File

@ -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],