diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 938c99a937..76e9bc4b6e 100644 --- a/components/table/__tests__/Table.rowSelection.test.js +++ b/components/table/__tests__/Table.rowSelection.test.js @@ -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( + , + ); + + 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' }]); + }); }); diff --git a/components/table/hooks/useSelection.tsx b/components/table/hooks/useSelection.tsx index 026a12ed1c..184f0e6ecb 100644 --- a/components/table/hooks/useSelection.tsx +++ b/components/table/hooks/useSelection.tsx @@ -117,12 +117,21 @@ export default function useSelection( 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],