refactor: refocus textarea after btn clicked

This commit is contained in:
xiaotianxt 2023-03-28 13:02:49 +08:00
parent 6c82f804ae
commit 493aa8c591

View File

@ -165,6 +165,7 @@ export function Chat(props: { showSideBar?: () => void }) {
setIsLoading(true); setIsLoading(true);
onUserInput(userInput).then(() => setIsLoading(false)); onUserInput(userInput).then(() => setIsLoading(false));
setUserInput(""); setUserInput("");
inputRef.current?.focus();
}; };
// stop response // stop response
@ -205,21 +206,10 @@ export function Chat(props: { showSideBar?: () => void }) {
// for auto-scroll // for auto-scroll
const latestMessageRef = useRef<HTMLDivElement>(null); const latestMessageRef = useRef<HTMLDivElement>(null);
const inputPanelRef = useRef<HTMLDivElement>(null); const inputRef = useRef<HTMLTextAreaElement>(null);
// wont scroll while hovering messages // wont scroll while hovering messages
const [autoScroll, setAutoScroll] = useState(false); const [autoScroll, setAutoScroll] = useState(false);
useEffect(() => {
const handleMouseDown = (e: MouseEvent) => {
// check if click on input panel
setAutoScroll(
!!inputPanelRef.current &&
inputPanelRef.current.contains(e.target as HTMLElement)
);
};
document.addEventListener("mousedown", handleMouseDown);
return () => document.removeEventListener("mousedown", handleMouseDown);
});
// preview messages // preview messages
const messages = (session.messages as RenderMessage[]) const messages = (session.messages as RenderMessage[])
@ -382,15 +372,18 @@ export function Chat(props: { showSideBar?: () => void }) {
</div> </div>
</div> </div>
<div className={styles["chat-input-panel"]} ref={inputPanelRef}> <div className={styles["chat-input-panel"]}>
<div className={styles["chat-input-panel-inner"]}> <div className={styles["chat-input-panel-inner"]}>
<textarea <textarea
ref={inputRef}
className={styles["chat-input"]} className={styles["chat-input"]}
placeholder={Locale.Chat.Input(submitKey)} placeholder={Locale.Chat.Input(submitKey)}
rows={3} rows={3}
onInput={(e) => setUserInput(e.currentTarget.value)} onInput={(e) => setUserInput(e.currentTarget.value)}
value={userInput} value={userInput}
onKeyDown={(e) => onInputKeyDown(e as any)} onKeyDown={(e) => onInputKeyDown(e as any)}
onFocus={() => setAutoScroll(true)}
onBlur={() => setAutoScroll(false)}
autoFocus autoFocus
/> />
<IconButton <IconButton