refactor: Improve PromptPopup text area focus and cursor placement

This commit is contained in:
ousugo 2025-02-18 00:47:13 +08:00 committed by 亢奋猫
parent 3383280726
commit aba3874797

View File

@ -1,6 +1,6 @@
import { Input, Modal } from 'antd' import { Input, Modal } from 'antd'
import { TextAreaProps } from 'antd/es/input' import { TextAreaProps } from 'antd/es/input'
import { useState } from 'react' import { useRef, useState } from 'react'
import { Box } from '../Layout' import { Box } from '../Layout'
import { TopView } from '../TopView' import { TopView } from '../TopView'
@ -27,6 +27,7 @@ const PromptPopupContainer: React.FC<Props> = ({
}) => { }) => {
const [value, setValue] = useState(defaultValue) const [value, setValue] = useState(defaultValue)
const [open, setOpen] = useState(true) const [open, setOpen] = useState(true)
const textAreaRef = useRef<any>(null)
const onOk = () => { const onOk = () => {
setOpen(false) setOpen(false)
@ -41,17 +42,35 @@ const PromptPopupContainer: React.FC<Props> = ({
resolve(null) 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 PromptPopup.hide = onCancel
return ( return (
<Modal title={title} open={open} onOk={onOk} onCancel={onCancel} afterClose={onClose} centered> <Modal
title={title}
open={open}
onOk={onOk}
onCancel={onCancel}
afterClose={onClose}
afterOpenChange={handleAfterOpenChange}
centered>
<Box mb={8}>{message}</Box> <Box mb={8}>{message}</Box>
<Input.TextArea <Input.TextArea
ref={textAreaRef}
placeholder={inputPlaceholder} placeholder={inputPlaceholder}
value={value} value={value}
onChange={(e) => setValue(e.target.value)} onChange={(e) => setValue(e.target.value)}
allowClear allowClear
autoFocus
onPressEnter={onOk} onPressEnter={onOk}
rows={1} rows={1}
{...inputProps} {...inputProps}