Fix Transfer selection when searching text, close #5993

This commit is contained in:
afc163 2017-05-03 19:05:31 +08:00
parent f5d371f6e9
commit c8d9e64809
2 changed files with 28 additions and 1 deletions

View File

@ -212,4 +212,21 @@ describe('Transfer', () => {
wrapper.find(TransferOperation).find(Button).at(1).simulate('click');
expect(handleChange).toHaveBeenCalledWith(['1', '3', '4'], 'right', ['1']);
});
it('should check correctly when there is a search text', () => {
const props = { ...listCommonProps };
delete props.targetKeys;
delete props.selectedKeys;
const handleSelectChange = jest.fn();
const wrapper = mount(
<Transfer {...props} showSearch onSelectChange={handleSelectChange} render={item => item.title} />
);
wrapper.find(TransferItem).filterWhere(n => n.prop('item').key === 'b').simulate('click');
expect(handleSelectChange).toHaveBeenLastCalledWith(['b'], []);
wrapper.find(TransferSearch).at(0).find('input').simulate('change', { target: { value: 'a' } });
wrapper.find(TransferList).at(0).find('.ant-transfer-list-header input[type="checkbox"]').simulate('change');
expect(handleSelectChange).toHaveBeenLastCalledWith(['b', 'a'], []);
wrapper.find(TransferList).at(0).find('.ant-transfer-list-header input[type="checkbox"]').simulate('change');
expect(handleSelectChange).toHaveBeenLastCalledWith(['b'], []);
});
});

View File

@ -187,7 +187,17 @@ abstract class Transfer extends React.Component<TransferProps, any> {
}
handleSelectAll = (direction, filteredDataSource, checkAll) => {
const holder = checkAll ? [] : filteredDataSource.map(item => item.key);
const originalSelectedKeys = this.state[this.getSelectedKeysName(direction)] || [];
const currentKeys = filteredDataSource.map(item => item.key);
// Only operate current keys from original selected keys
const newKeys1 = originalSelectedKeys.filter(key => currentKeys.indexOf(key) === -1);
const newKeys2 = [...originalSelectedKeys];
currentKeys.forEach((key) => {
if (newKeys2.indexOf(key) === -1) {
newKeys2.push(key);
}
});
const holder = checkAll ? newKeys1 : newKeys2;
this.handleSelectChange(direction, holder);
if (!this.props.selectedKeys) {