From 5d6588198489c0c05987610eaa0f120a157c32bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Tue, 20 Jun 2023 13:41:35 +0800 Subject: [PATCH] fix: Transfer with rowKey will be unselectable (#43115) * test: test driven * fix: keys mapping * chore: add bug version --- components/transfer/__tests__/index.test.tsx | 30 +++++++++++++++++--- components/transfer/hooks/useSelection.ts | 6 +++- scripts/post-script.js | 1 + 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/components/transfer/__tests__/index.test.tsx b/components/transfer/__tests__/index.test.tsx index a9e8343603..6920c8bb83 100644 --- a/components/transfer/__tests__/index.test.tsx +++ b/components/transfer/__tests__/index.test.tsx @@ -496,10 +496,32 @@ describe('Transfer', () => { expect(onScroll).toHaveBeenLastCalledWith('right', expect.anything()); }); - it('should support rowKey is function', () => { - expect(() => { - render( record.key} />); - }).not.toThrow(); + it('support rowKey', () => { + const onSelectChange = jest.fn(); + + const Demo = () => { + const [selectedKeys, setSelectedKeys] = useState([]); + + return ( + `key_${record.key}`} + onSelectChange={(keys) => { + onSelectChange(keys); + setSelectedKeys(keys); + }} + /> + ); + }; + + const { container } = render(); + + fireEvent.click(container.querySelector('.ant-transfer-list-content input')!); + expect(onSelectChange).toHaveBeenCalledWith(['key_a']); + expect( + container.querySelector('.ant-transfer-list-content input')!.checked, + ).toBeTruthy(); }); it('should support render value and label in item', () => { diff --git a/components/transfer/hooks/useSelection.ts b/components/transfer/hooks/useSelection.ts index 1f594f9648..c2be5571bf 100644 --- a/components/transfer/hooks/useSelection.ts +++ b/components/transfer/hooks/useSelection.ts @@ -7,6 +7,10 @@ function filterKeys(keys: string[], dataKeys: Set) { return keys.length === filteredKeys.length ? keys : filteredKeys; } +function flattenKeys(keys: Set) { + return Array.from(keys).join(';'); +} + export default function useSelection( leftDataSource: T[], rightDataSource: T[], @@ -44,7 +48,7 @@ export default function useSelection( React.useEffect(() => { setSourceSelectedKeys(filterKeys(sourceSelectedKeys, leftKeys)); setTargetSelectedKeys(filterKeys(targetSelectedKeys, rightKeys)); - }, [leftKeys, rightKeys]); + }, [flattenKeys(leftKeys), flattenKeys(rightKeys)]); return [ // Keys diff --git a/scripts/post-script.js b/scripts/post-script.js index 7fd39745b8..831ebae788 100644 --- a/scripts/post-script.js +++ b/scripts/post-script.js @@ -37,6 +37,7 @@ const DEPRECIATED_VERSION = { 'https://github.com/ant-design/cssinjs/pull/108', 'https://github.com/ant-design/ant-design/pull/41993', ], + '5.6.2': ['https://github.com/ant-design/ant-design/issues/43113'], }; function matchDeprecated(version) {