🐛 fix: topic renaming input focus issue in context menu (#11323)

Fixes LOBE-2838

This commit resolves the issue where the input field wasn't properly focused when renaming topics through the context menu.

Changes:
- Created FocusableInput component that ensures input focus using queueMicrotask
- Replaced autoFocus prop with proper ref-based focus management
- Simplified onBlur handler logic
- Removed duplicate toggleEditing call from handleUpdate

The queueMicrotask approach ensures the focus happens after the Popover has fully rendered and positioned itself.
This commit is contained in:
Innei
2026-01-08 15:35:08 +08:00
committed by GitHub
parent 31c05e0031
commit dd065fc991

View File

@@ -1,5 +1,6 @@
import { Input, Popover } from '@lobehub/ui';
import { memo, useCallback, useState } from 'react';
import { Input, type InputProps, Popover } from '@lobehub/ui';
import type { InputRef } from 'antd';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useChatStore } from '@/store/chat';
@@ -9,6 +10,18 @@ interface EditingProps {
toggleEditing: (visible?: boolean) => void;
}
function FocusableInput({ ...props }: InputProps) {
const ref = useRef<InputRef>(null);
useEffect(() => {
queueMicrotask(() => {
if (ref.current) {
ref.current.input?.focus();
}
});
}, []);
return <Input {...props} ref={ref} />;
}
const Editing = memo<EditingProps>(({ id, title, toggleEditing }) => {
const [newTitle, setNewTitle] = useState(title);
const [editing, updateTopicTitle] = useChatStore((s) => [
@@ -41,19 +54,14 @@ const Editing = memo<EditingProps>(({ id, title, toggleEditing }) => {
);
}
}
toggleEditing(false);
}, [newTitle, title, id, updateTopicTitle, toggleEditing]);
return (
<Popover
content={
<Input
autoFocus
<FocusableInput
defaultValue={title}
onBlur={() => {
handleUpdate();
toggleEditing(false);
}}
onBlur={handleUpdate}
onChange={(e) => setNewTitle(e.target.value)}
onClick={(e) => e.stopPropagation()}
onPressEnter={() => {
@@ -64,6 +72,7 @@ const Editing = memo<EditingProps>(({ id, title, toggleEditing }) => {
}
onOpenChange={(open) => {
if (!open) handleUpdate();
toggleEditing(open);
}}
open={editing}