feat: add focus handling for directory name input in KnowledgeDirectoryPanel
This commit is contained in:
@@ -12,7 +12,7 @@ import {
|
|||||||
Trash2Icon,
|
Trash2Icon,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import { useCallback, useEffect, useMemo, useState, type PointerEvent } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState, type PointerEvent } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
import { OptionCombobox } from "@/components/option-combobox";
|
import { OptionCombobox } from "@/components/option-combobox";
|
||||||
@@ -89,6 +89,8 @@ export function KnowledgeDirectoryPanel({
|
|||||||
onChanged,
|
onChanged,
|
||||||
}: KnowledgeDirectoryPanelProps) {
|
}: KnowledgeDirectoryPanelProps) {
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
|
const directoryNameInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const directoryNameFocusTimerRef = useRef<number | null>(null);
|
||||||
const [panelWidth, setPanelWidth] = useState(() => {
|
const [panelWidth, setPanelWidth] = useState(() => {
|
||||||
if (typeof window === "undefined") {
|
if (typeof window === "undefined") {
|
||||||
return DIRECTORY_PANEL_DEFAULT_WIDTH;
|
return DIRECTORY_PANEL_DEFAULT_WIDTH;
|
||||||
@@ -140,6 +142,28 @@ export function KnowledgeDirectoryPanel({
|
|||||||
localStorage.setItem(DIRECTORY_PANEL_WIDTH_STORAGE_KEY, String(panelWidth));
|
localStorage.setItem(DIRECTORY_PANEL_WIDTH_STORAGE_KEY, String(panelWidth));
|
||||||
}, [panelWidth]);
|
}, [panelWidth]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!dialog.open) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const frame = requestAnimationFrame(() => {
|
||||||
|
directoryNameFocusTimerRef.current = window.setTimeout(() => {
|
||||||
|
const input =
|
||||||
|
directoryNameInputRef.current ??
|
||||||
|
document.querySelector<HTMLInputElement>("[data-knowledge-directory-name-input='true']");
|
||||||
|
input?.focus({ preventScroll: true });
|
||||||
|
input?.select();
|
||||||
|
}, 80);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
cancelAnimationFrame(frame);
|
||||||
|
if (directoryNameFocusTimerRef.current !== null) {
|
||||||
|
window.clearTimeout(directoryNameFocusTimerRef.current);
|
||||||
|
directoryNameFocusTimerRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [dialog.open]);
|
||||||
|
|
||||||
function handleResizePointerDown(event: PointerEvent<HTMLDivElement>) {
|
function handleResizePointerDown(event: PointerEvent<HTMLDivElement>) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const startX = event.clientX;
|
const startX = event.clientX;
|
||||||
@@ -348,6 +372,9 @@ export function KnowledgeDirectoryPanel({
|
|||||||
<FieldLabel>{t("knowledge.directoryName")}</FieldLabel>
|
<FieldLabel>{t("knowledge.directoryName")}</FieldLabel>
|
||||||
<FieldContent>
|
<FieldContent>
|
||||||
<Input
|
<Input
|
||||||
|
ref={directoryNameInputRef}
|
||||||
|
autoFocus
|
||||||
|
data-knowledge-directory-name-input="true"
|
||||||
value={dialog.name}
|
value={dialog.name}
|
||||||
onChange={(event) =>
|
onChange={(event) =>
|
||||||
setDialog((current) => ({ ...current, name: event.target.value }))
|
setDialog((current) => ({ ...current, name: event.target.value }))
|
||||||
|
|||||||
Reference in New Issue
Block a user