From 3882c8053e0b42a30a1f50134233c1ae06c89bc3 Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 21 May 2020 10:41:57 +0800 Subject: [PATCH 1/2] test driven --- .../__tests__/Table.rowSelection.test.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 938c99a937f1..57c4bf23601f 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.only('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' }]); + }); }); From 6492e1f7610fb37c284421fad3fe84e4b177f4cb Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 21 May 2020 10:47:13 +0800 Subject: [PATCH 2/2] fix logic --- .../table/__tests__/Table.rowSelection.test.js | 2 +- components/table/hooks/useSelection.tsx | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 57c4bf23601f..76e9bc4b6eeb 100644 --- a/components/table/__tests__/Table.rowSelection.test.js +++ b/components/table/__tests__/Table.rowSelection.test.js @@ -798,7 +798,7 @@ describe('Table.rowSelection', () => { expect(onChange.mock.calls[0][1]).toEqual([expect.objectContaining({ name: 'bamboo' })]); }); - it.only('do not cache selected keys', () => { + it('do not cache selected keys', () => { const onChange = jest.fn(); const wrapper = mount(
( const setSelectedKeys = React.useCallback( (keys: Key[]) => { - setInnerSelectedKeys(keys); + const availableKeys: Key[] = []; + const records: RecordType[] = []; + + keys.forEach(key => { + const record = getRecordByKey(key); + if (record !== undefined) { + availableKeys.push(key); + records.push(record); + } + }); - const records = keys.map(key => getRecordByKey(key)); + setInnerSelectedKeys(availableKeys); if (onSelectionChange) { - onSelectionChange(keys, records); + onSelectionChange(availableKeys, records); } }, [setInnerSelectedKeys, getRecordByKey, onSelectionChange],