Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Table selection cache #24338

Merged
merged 2 commits into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/table/__tests__/Table.rowSelection.test.js
Expand Up @@ -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' }]);
});
});
15 changes: 12 additions & 3 deletions components/table/hooks/useSelection.tsx
Expand Up @@ -117,12 +117,21 @@ export default function useSelection<RecordType>(

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],
Expand Down