Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
}

if (!disabled) {
triggerOpen(false, {
lazy: true,
});
// triggerOpen(false, {
// lazy: true,
// });

Choose a reason for hiding this comment

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

medium

This commented-out code is no longer necessary since the logic for closing the dropdown on blur has been moved to the SelectInput component. It should be removed to improve code clarity.

onBlur?.(event);
}
};
Expand Down
9 changes: 8 additions & 1 deletion src/SelectInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { clsx } from 'clsx';
import type { ComponentsConfig } from '../hooks/useComponents';
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
import { composeRef } from '@rc-component/util/lib/ref';
import { macroTask } from '../hooks/useOpen';

export interface SelectInputRef {
focus: (options?: FocusOptions) => void;
Expand Down Expand Up @@ -197,7 +198,13 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
});

const onInternalBlur: SelectInputProps['onBlur'] = (event) => {
toggleOpen(false);
macroTask(() => {
const inputNode = getDOM(inputRef.current);
if (inputNode !== document.activeElement && !inputNode.contains(document.activeElement)) {
toggleOpen(false);
}
});

onBlur?.(event);
};
Comment on lines 200 to 212
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

blur 处理逻辑修复了焦点转移问题,但需要添加空值检查。

使用 macroTask 延迟关闭逻辑确保在 document.activeElement 更新后再检查焦点位置,这正确修复了当焦点转移到同一组件内的其他输入元素时错误关闭下拉框的问题。

但是,getDOM(inputRef.current) 可能返回 null,应添加空值检查以避免潜在的运行时错误。

应用以下 diff 添加空值检查:

  const onInternalBlur: SelectInputProps['onBlur'] = (event) => {
    macroTask(() => {
      const inputNode = getDOM(inputRef.current);
-     if (inputNode !== document.activeElement && !inputNode.contains(document.activeElement)) {
+     if (inputNode && inputNode !== document.activeElement && !inputNode.contains(document.activeElement)) {
        toggleOpen(false);
      }
    });

    onBlur?.(event);
  };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const onInternalBlur: SelectInputProps['onBlur'] = (event) => {
toggleOpen(false);
macroTask(() => {
const inputNode = getDOM(inputRef.current);
if (inputNode !== document.activeElement && !inputNode.contains(document.activeElement)) {
toggleOpen(false);
}
});
onBlur?.(event);
};
const onInternalBlur: SelectInputProps['onBlur'] = (event) => {
macroTask(() => {
const inputNode = getDOM(inputRef.current);
if (inputNode && inputNode !== document.activeElement && !inputNode.contains(document.activeElement)) {
toggleOpen(false);
}
});
onBlur?.(event);
};
🤖 Prompt for AI Agents
In src/SelectInput/index.tsx around lines 200 to 209, the blur handler uses
getDOM(inputRef.current) without checking for null which can cause a runtime
error; update the macroTask callback to first check that inputNode is not null
(or undefined) before comparing it to document.activeElement or calling
inputNode.contains(...), and only call toggleOpen(false) when inputNode exists
and the activeElement is outside it; keep the existing onBlur?.(event) call
unchanged.


Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const internalMacroTask = (fn: VoidFunction) => {
channel.port2.postMessage(null);
};

const macroTask = (fn: VoidFunction, times = 1) => {
export const macroTask = (fn: VoidFunction, times = 1) => {
if (times <= 0) {
fn();
return;
Expand Down Expand Up @@ -68,15 +68,15 @@ export default function useOpen(
});

const toggleOpen = useEvent<TriggerOpenType>((nextOpen, config = {}) => {
const { ignoreNext = false, lazy = false } = config;
const { ignoreNext = false } = config;

taskIdRef.current += 1;
const id = taskIdRef.current;

const nextOpenVal = typeof nextOpen === 'boolean' ? nextOpen : !mergedOpen;

// Since `mergedOpen` is post-processed, we need to check if the value really changed
if (nextOpenVal || !lazy) {
if (nextOpenVal) {
if (!taskLockRef.current) {
triggerEvent(nextOpenVal);

Expand Down
3 changes: 3 additions & 0 deletions tests/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ describe('Select.Combobox', () => {
}

fireEvent.blur(container.querySelector('input')!);
container.querySelector('input').blur();

await delay(100);

expectOpen(container, false);
Expand Down Expand Up @@ -591,6 +593,7 @@ describe('Select.Combobox', () => {

fireEvent.blur(container.querySelector('input'));
act(() => {
container.querySelector('input').blur();
jest.runAllTimers();
});

Expand Down
2 changes: 2 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ describe('Select.Basic', () => {
toggleOpen(container);
fireEvent.blur(container.querySelector('input'));
act(() => {
// Force trigger blur to active to document.body
container.querySelector('input').blur();
jest.runAllTimers();
});
expectOpen(container, false);
Expand Down
Loading