Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)

// Close will clean up single mode search text
React.useEffect(() => {
if (!mergedOpen && !multiple && mode !== 'combobox') {
if (!mergedOpen && !multiple && mode !== 'combobox' && displayValues?.length) {
onInternalSearch('', false, false);
}
}, [mergedOpen]);
Comment on lines 430 to 434

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

这个 useEffect 存在依赖项缺失的问题。它的回调函数中使用了在组件作用域中定义的 displayValuesmultiplemode,但它们没有被添加到依赖项数组中。这违反了 React Hooks 的规则,可能会导致 effect 使用了来自先前渲染的陈旧值,从而引发难以追踪的 bug。

为了修复这个问题,请将这些变量添加到依赖项数组中。

注意: onInternalSearch 函数也应该被视为一个依赖项。但由于它在每次渲染时都会被重新创建,直接添加它会导致此 effect 在每次渲染时都执行。正确的做法是使用 useCallbackuseEvent (此文件中已有使用) 来记忆 onInternalSearch,然后再将其添加到依赖项数组中。

Suggested change
React.useEffect(() => {
if (!mergedOpen && !multiple && mode !== 'combobox') {
if (!mergedOpen && !multiple && mode !== 'combobox' && displayValues?.length) {
onInternalSearch('', false, false);
}
}, [mergedOpen]);
React.useEffect(() => {
if (!mergedOpen && !multiple && mode !== 'combobox' && displayValues?.length) {
onInternalSearch('', false, false);
}
}, [mergedOpen, displayValues, multiple, mode]);

Expand Down
38 changes: 38 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ describe('Select.Basic', () => {
expectOpen(container);
});

it('should not clear searchValue', () => {
const App: React.FC = () => {
const [options, setOptions] = React.useState<{ label: string; value: string }[]>([]);
const [isFetching, setIsFetching] = React.useState<boolean>(false);

function handleSearch() {
setIsFetching(true);
setTimeout(() => {
setOptions([]);
setIsFetching(false);
}, 1000);
}

return (
<Select
style={{ width: 300 }}
showSearch={{ filterOption: false, onSearch: handleSearch }}
placeholder="Type to search users..."
loading={isFetching}
options={options}
notFoundContent={isFetching ? 'Loading...' : null}
/>
);
};
const { container } = render(<App />);
const searchInput = container.querySelector('input') as HTMLInputElement;
expect(searchInput).toBeTruthy();
expect(searchInput.value).toBe('');
fireEvent.change(searchInput, { target: { value: 'user query' } });
expect(searchInput.value).toBe('user query');
act(() => {
jest.advanceTimersByTime(1000);
});
expect(searchInput.value).toBe('user query');
jest.useRealTimers();
});

describe('render', () => {
function genSelect(props?: Partial<SelectProps>) {
return (
Expand Down Expand Up @@ -1058,6 +1095,7 @@ describe('Select.Basic', () => {
);
}
}

const { container } = render(<Controlled />);
expectOpen(container);
toggleOpen(container);
Expand Down
Loading