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: Transfer selectInvert should be corrected #47125

Merged
merged 1 commit into from
Jan 24, 2024
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
54 changes: 34 additions & 20 deletions components/transfer/__tests__/dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint no-use-before-define: "off" */
import React from 'react';
import { act } from 'react-dom/test-utils';

import Transfer from '..';
import { fireEvent, render } from '../../../tests/utils';

Expand Down Expand Up @@ -87,30 +88,43 @@ describe('Transfer.Dropdown', () => {
});

describe('select invert', () => {
[
{ 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();
it('with pagination', () => {
jest.useFakeTimers();

const onSelectChange = jest.fn();
const { container } = render(
<Transfer {...listProps} selectedKeys={undefined} onSelectChange={onSelectChange} />,
);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => {
jest.runAllTimers();
});

const onSelectChange = jest.fn();
const { container } = render(<Transfer {...props} onSelectChange={onSelectChange} />);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => {
jest.runAllTimers();
});
clickItem(container, 0);
expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd', 'e'], []);

clickItem(container, index);
expect(onSelectChange).toHaveBeenCalledWith(keys, []);
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();
});
});

Expand Down
19 changes: 8 additions & 11 deletions components/transfer/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,19 @@ const TransferList = <RecordType extends KeyWiseTransferItem>(
key: 'selectInvert',
label: selectInvert,
onClick() {
const availableKeys = getEnabledItemKeys(
pagination
? (listBodyRef.current?.items || []).map((entity) => entity.item)
: filteredItems,
const availablePageItemKeys = getEnabledItemKeys(
(listBodyRef.current?.items || []).map((entity) => entity.item),
);
const checkedKeySet = new Set<string>(checkedKeys);
const newCheckedKeys: string[] = [];
const newUnCheckedKeys: string[] = [];
availableKeys.forEach((key) => {
const checkedKeySet = new Set(checkedKeys);
const newCheckedKeysSet = new Set(checkedKeySet);
availablePageItemKeys.forEach((key) => {
if (checkedKeySet.has(key)) {
newUnCheckedKeys.push(key);
newCheckedKeysSet.delete(key);
} else {
newCheckedKeys.push(key);
newCheckedKeysSet.add(key);
}
});
onItemSelectAll?.(newCheckedKeys, 'replace');
onItemSelectAll?.(Array.from(newCheckedKeysSet), 'replace');
},
},
];
Expand Down