From aba3874797c5e981e1e280b6f87815dcb1434a86 Mon Sep 17 00:00:00 2001 From: ousugo Date: Tue, 18 Feb 2025 00:47:13 +0800 Subject: [PATCH] refactor: Improve PromptPopup text area focus and cursor placement --- .../src/components/Popups/PromptPopup.tsx | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/components/Popups/PromptPopup.tsx b/src/renderer/src/components/Popups/PromptPopup.tsx index c6fe6ddb..a6fccede 100644 --- a/src/renderer/src/components/Popups/PromptPopup.tsx +++ b/src/renderer/src/components/Popups/PromptPopup.tsx @@ -1,6 +1,6 @@ import { Input, Modal } from 'antd' import { TextAreaProps } from 'antd/es/input' -import { useState } from 'react' +import { useRef, useState } from 'react' import { Box } from '../Layout' import { TopView } from '../TopView' @@ -27,6 +27,7 @@ const PromptPopupContainer: React.FC = ({ }) => { const [value, setValue] = useState(defaultValue) const [open, setOpen] = useState(true) + const textAreaRef = useRef(null) const onOk = () => { setOpen(false) @@ -41,17 +42,35 @@ const PromptPopupContainer: React.FC = ({ resolve(null) } + const handleAfterOpenChange = (visible: boolean) => { + if (visible) { + const textArea = textAreaRef.current?.resizableTextArea?.textArea + if (textArea) { + textArea.focus() + const length = textArea.value.length + textArea.setSelectionRange(length, length) + } + } + } + PromptPopup.hide = onCancel return ( - + {message} setValue(e.target.value)} allowClear - autoFocus onPressEnter={onOk} rows={1} {...inputProps}