Skip to content

Commit 702e6d4

Browse files
LengYXinLengYXin
authored andcommitted
fix: 修复useActive 中 items 为空数组导致的异常 (ant-design#824)
Co-authored-by: LengYXin <[email protected]>
1 parent 7990800 commit 702e6d4

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { renderHook } from '@testing-library/react';
2+
import React from 'react';
3+
import type { SuggestionItem } from '../index';
4+
import useActive from '../useActive';
5+
6+
describe('useActive', () => {
7+
it('should initialize activePaths with first item value when open is true and items is valid array', () => {
8+
const items: SuggestionItem[] = [
9+
{ label: 'Item 1', value: 'item1' },
10+
{ label: 'Item 2', value: 'item2' },
11+
];
12+
13+
const { result } = renderHook(() => useActive(items, true, false, jest.fn(), jest.fn()));
14+
15+
const [activePaths] = result.current;
16+
expect(activePaths).toEqual(['item1']);
17+
});
18+
19+
it('should not initialize activePaths when open is false', () => {
20+
const items: SuggestionItem[] = [{ label: 'Item 1', value: 'item1' }];
21+
22+
const { result } = renderHook(() => useActive(items, false, false, jest.fn(), jest.fn()));
23+
24+
const [activePaths] = result.current;
25+
expect(activePaths).toEqual([]);
26+
});
27+
28+
it('should not initialize activePaths when items is empty array', () => {
29+
const { result } = renderHook(() => useActive([], true, false, jest.fn(), jest.fn()));
30+
31+
const [activePaths] = result.current;
32+
expect(activePaths).toEqual([]);
33+
});
34+
35+
it('should not initialize activePaths when items is not a valid array', () => {
36+
// Test with null
37+
const { result: result1 } = renderHook(() =>
38+
useActive(null as any, true, false, jest.fn(), jest.fn()),
39+
);
40+
41+
const [activePaths1] = result1.current;
42+
expect(activePaths1).toEqual([]);
43+
44+
// Test with undefined
45+
const { result: result2 } = renderHook(() =>
46+
useActive(undefined as any, true, false, jest.fn(), jest.fn()),
47+
);
48+
49+
const [activePaths2] = result2.current;
50+
expect(activePaths2).toEqual([]);
51+
52+
// Test with non-array value
53+
const { result: result3 } = renderHook(() =>
54+
useActive('not-an-array' as any, true, false, jest.fn(), jest.fn()),
55+
);
56+
57+
const [activePaths3] = result3.current;
58+
expect(activePaths3).toEqual([]);
59+
});
60+
});

components/suggestion/useActive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEvent } from 'rc-util';
2-
import type { SuggestionItem } from '.';
32
import React from 'react';
3+
import type { SuggestionItem } from '.';
44

55
/**
66
* Since Cascader not support ref active, we use `value` to mock the active item.
@@ -117,7 +117,8 @@ export default function useActive(
117117
});
118118

119119
React.useEffect(() => {
120-
if (open) {
120+
// 确保 items 是一个数组且至少有一个元素
121+
if (open && Array.isArray(items) && items.length > 0) {
121122
setActivePaths([items[0].value]);
122123
}
123124
}, [open]);

0 commit comments

Comments
 (0)