fix: Transfer selectInvert should be corrected (#47125)

This commit is contained in:
daisy 2024-01-24 10:54:15 +08:00 committed by GitHub
parent 30a4143afc
commit 079d8c92a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 33 deletions

View File

@ -1,6 +1,7 @@
/* eslint no-use-before-define: "off" */ /* eslint no-use-before-define: "off" */
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import Transfer from '..'; import Transfer from '..';
import { fireEvent, render } from '../../../tests/utils'; import { fireEvent, render } from '../../../tests/utils';
@ -87,30 +88,43 @@ describe('Transfer.Dropdown', () => {
}); });
describe('select invert', () => { describe('select invert', () => {
[ it('with pagination', () => {
{ name: 'with pagination', props: listProps, index: 2, keys: ['c', 'd'] },
{
name: 'without pagination',
props: { ...listProps, pagination: null as any },
index: 1,
keys: ['c', 'd', 'e'],
},
].forEach(({ name, props, index, keys }) => {
it(name, () => {
jest.useFakeTimers(); jest.useFakeTimers();
const onSelectChange = jest.fn(); const onSelectChange = jest.fn();
const { container } = render(<Transfer {...props} onSelectChange={onSelectChange} />); const { container } = render(
<Transfer {...listProps} selectedKeys={undefined} onSelectChange={onSelectChange} />,
);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!); fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => { act(() => {
jest.runAllTimers(); jest.runAllTimers();
}); });
clickItem(container, index); clickItem(container, 0);
expect(onSelectChange).toHaveBeenCalledWith(keys, []); expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd', 'e'], []);
clickItem(container, 2);
expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd'], []);
jest.useRealTimers(); jest.useRealTimers();
}); });
it('without pagination', () => {
jest.useFakeTimers();
const onSelectChange = jest.fn();
const { container } = render(
<Transfer {...listProps} pagination={null as any} onSelectChange={onSelectChange} />,
);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => {
jest.runAllTimers();
});
clickItem(container, 1);
expect(onSelectChange).toHaveBeenCalledWith(['c', 'd', 'e'], []);
jest.useRealTimers();
}); });
}); });

View File

@ -343,22 +343,19 @@ const TransferList = <RecordType extends KeyWiseTransferItem>(
key: 'selectInvert', key: 'selectInvert',
label: selectInvert, label: selectInvert,
onClick() { onClick() {
const availableKeys = getEnabledItemKeys( const availablePageItemKeys = getEnabledItemKeys(
pagination (listBodyRef.current?.items || []).map((entity) => entity.item),
? (listBodyRef.current?.items || []).map((entity) => entity.item)
: filteredItems,
); );
const checkedKeySet = new Set<string>(checkedKeys); const checkedKeySet = new Set(checkedKeys);
const newCheckedKeys: string[] = []; const newCheckedKeysSet = new Set(checkedKeySet);
const newUnCheckedKeys: string[] = []; availablePageItemKeys.forEach((key) => {
availableKeys.forEach((key) => {
if (checkedKeySet.has(key)) { if (checkedKeySet.has(key)) {
newUnCheckedKeys.push(key); newCheckedKeysSet.delete(key);
} else { } else {
newCheckedKeys.push(key); newCheckedKeysSet.add(key);
} }
}); });
onItemSelectAll?.(newCheckedKeys, 'replace'); onItemSelectAll?.(Array.from(newCheckedKeysSet), 'replace');
}, },
}, },
]; ];