fix: isolate note title in memoized component, prevent search input stealing focus

This commit is contained in:
Hermes
2026-08-01 13:02:55 +00:00
parent 9b0c1fb447
commit 420ebfe24a
+31 -1
View File
@@ -77,6 +77,36 @@ const NoteTitleInput = memo(function NoteTitleInput({ noteId, initialTitle }: {
); );
}); });
// Isolated title input component - memo prevents re-render when parent re-renders
const NoteTitleInput = memo(function NoteTitleInput({ noteId, initialTitle }: { noteId: string; initialTitle: string }) {
const [title, setTitle] = useState(initialTitle);
const queryClient = useQueryClient();
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
setTitle(initialTitle);
inputRef.current?.focus();
}, [noteId]);
const handleBlur = () => {
if (title !== initialTitle) {
api.patch<Note>("/notes/" + noteId, { title: title }).then(() => {
queryClient.invalidateQueries({ queryKey: ["notes"] });
});
}
};
return (
<input
ref={inputRef}
value={title}
onChange={(e) => setTitle(e.target.value)}
onBlur={handleBlur}
className="flex h-9 w-full rounded-md border-0 bg-transparent px-0 py-1 text-lg font-semibold text-base shadow-none transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 disabled:cursor-not-allowed disabled:opacity-50"
/>
);
});
function NotesPage() { function NotesPage() {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
@@ -150,7 +180,7 @@ function NotesPage() {
<div className="p-3 border-b"> <div className="p-3 border-b">
<div className="relative"> <div className="relative">
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" /> <Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
<Input placeholder="Search notes..." value={search} onChange={(e) => setSearch(e.target.value)} className="pl-8" /> <Input placeholder="Search notes..." value={search} onChange={(e) => setSearch(e.target.value)} className="pl-8" tabIndex={-1} />
</div> </div>
</div> </div>
<div className="p-2"> <div className="p-2">