From 079d8c92a3061015f8c2e391581c31a94f992778 Mon Sep 17 00:00:00 2001 From: daisy <47104575+linxianxi@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:54:15 +0800 Subject: [PATCH] fix: Transfer selectInvert should be corrected (#47125) --- .../transfer/__tests__/dropdown.test.tsx | 58 ++++++++++++------- components/transfer/list.tsx | 19 +++--- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/components/transfer/__tests__/dropdown.test.tsx b/components/transfer/__tests__/dropdown.test.tsx index 03ce333976..f5e5421341 100644 --- a/components/transfer/__tests__/dropdown.test.tsx +++ b/components/transfer/__tests__/dropdown.test.tsx @@ -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'; @@ -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(); - fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!); - act(() => { - jest.runAllTimers(); - }); - - clickItem(container, index); - expect(onSelectChange).toHaveBeenCalledWith(keys, []); - - jest.useRealTimers(); + const onSelectChange = jest.fn(); + const { container } = render( + , + ); + fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!); + act(() => { + jest.runAllTimers(); }); + + clickItem(container, 0); + expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd', 'e'], []); + + clickItem(container, 2); + expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd'], []); + + jest.useRealTimers(); + }); + + it('without pagination', () => { + jest.useFakeTimers(); + + const onSelectChange = jest.fn(); + const { container } = render( + , + ); + fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!); + act(() => { + jest.runAllTimers(); + }); + + clickItem(container, 1); + expect(onSelectChange).toHaveBeenCalledWith(['c', 'd', 'e'], []); + + jest.useRealTimers(); }); }); diff --git a/components/transfer/list.tsx b/components/transfer/list.tsx index 0d6963b3e3..46c9e17725 100644 --- a/components/transfer/list.tsx +++ b/components/transfer/list.tsx @@ -343,22 +343,19 @@ const TransferList = ( 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(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'); }, }, ];