From c920165c1d50c4e5a17dfd7e4c79300ef1109ee9 Mon Sep 17 00:00:00 2001 From: LengYXin Date: Tue, 13 May 2025 17:24:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DuseActive=20?= =?UTF-8?q?=E4=B8=AD=20items=20=E4=B8=BA=E7=A9=BA=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/suggestion/useActive.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/components/suggestion/useActive.ts b/components/suggestion/useActive.ts index e71a6be6f..fe0f32581 100644 --- a/components/suggestion/useActive.ts +++ b/components/suggestion/useActive.ts @@ -117,6 +117,7 @@ export default function useActive( }); React.useEffect(() => { + if(items?.length === 0) return; if (open) { setActivePaths([items[0].value]); } From ea1d8abd6c943e5a93b784deaec723370904fe38 Mon Sep 17 00:00:00 2001 From: Clever Date: Tue, 13 May 2025 09:33:29 +0000 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DuseActive=20?= =?UTF-8?q?=E4=B8=AD=20items=20=E4=B8=BA=E7=A9=BA=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/suggestion/useActive.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/suggestion/useActive.ts b/components/suggestion/useActive.ts index fe0f32581..1669ec938 100644 --- a/components/suggestion/useActive.ts +++ b/components/suggestion/useActive.ts @@ -117,7 +117,8 @@ export default function useActive( }); React.useEffect(() => { - if(items?.length === 0) return; + // 确保 items 是一个数组且至少有一个元素 + if (!Array.isArray(items) || items.length === 0) return; if (open) { setActivePaths([items[0].value]); } From 866950e4a67038f72354c278f090f37e294a6e6b Mon Sep 17 00:00:00 2001 From: LengYXin Date: Wed, 16 Jul 2025 18:04:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DuseActive=20?= =?UTF-8?q?=E4=B8=AD=20items=20=E4=B8=BA=E7=A9=BA=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82=E5=B8=B8+=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../suggestion/__tests__/useActive.test.tsx | 60 +++++++++++++++++++ components/suggestion/useActive.ts | 5 +- 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 components/suggestion/__tests__/useActive.test.tsx diff --git a/components/suggestion/__tests__/useActive.test.tsx b/components/suggestion/__tests__/useActive.test.tsx new file mode 100644 index 000000000..9784ea633 --- /dev/null +++ b/components/suggestion/__tests__/useActive.test.tsx @@ -0,0 +1,60 @@ +import { renderHook } from '@testing-library/react'; +import React from 'react'; +import type { SuggestionItem } from '../index'; +import useActive from '../useActive'; + +describe('useActive', () => { + it('should initialize activePaths with first item value when open is true and items is valid array', () => { + const items: SuggestionItem[] = [ + { label: 'Item 1', value: 'item1' }, + { label: 'Item 2', value: 'item2' }, + ]; + + const { result } = renderHook(() => useActive(items, true, false, jest.fn(), jest.fn())); + + const [activePaths] = result.current; + expect(activePaths).toEqual(['item1']); + }); + + it('should not initialize activePaths when open is false', () => { + const items: SuggestionItem[] = [{ label: 'Item 1', value: 'item1' }]; + + const { result } = renderHook(() => useActive(items, false, false, jest.fn(), jest.fn())); + + const [activePaths] = result.current; + expect(activePaths).toEqual([]); + }); + + it('should not initialize activePaths when items is empty array', () => { + const { result } = renderHook(() => useActive([], true, false, jest.fn(), jest.fn())); + + const [activePaths] = result.current; + expect(activePaths).toEqual([]); + }); + + it('should not initialize activePaths when items is not a valid array', () => { + // Test with null + const { result: result1 } = renderHook(() => + useActive(null as any, true, false, jest.fn(), jest.fn()), + ); + + const [activePaths1] = result1.current; + expect(activePaths1).toEqual([]); + + // Test with undefined + const { result: result2 } = renderHook(() => + useActive(undefined as any, true, false, jest.fn(), jest.fn()), + ); + + const [activePaths2] = result2.current; + expect(activePaths2).toEqual([]); + + // Test with non-array value + const { result: result3 } = renderHook(() => + useActive('not-an-array' as any, true, false, jest.fn(), jest.fn()), + ); + + const [activePaths3] = result3.current; + expect(activePaths3).toEqual([]); + }); +}); diff --git a/components/suggestion/useActive.ts b/components/suggestion/useActive.ts index 1669ec938..3380d1596 100644 --- a/components/suggestion/useActive.ts +++ b/components/suggestion/useActive.ts @@ -1,6 +1,6 @@ import { useEvent } from 'rc-util'; -import type { SuggestionItem } from '.'; import React from 'react'; +import type { SuggestionItem } from '.'; /** * Since Cascader not support ref active, we use `value` to mock the active item. @@ -118,8 +118,7 @@ export default function useActive( React.useEffect(() => { // 确保 items 是一个数组且至少有一个元素 - if (!Array.isArray(items) || items.length === 0) return; - if (open) { + if (open && Array.isArray(items) && items.length > 0) { setActivePaths([items[0].value]); } }, [open]);