Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI Chat] positions cursor at end of input when editing #25831

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions components/ai_chat/resources/page/components/edit_input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ interface Props {

function EditInput(props: Props) {
const [text, setText] = React.useState(props.text)
const textareaRef = React.useRef<HTMLTextAreaElement>(null)

React.useEffect(() => {
if (textareaRef.current) {
/**
* When editing, we want the cursor to be positioned
* at the end of the text. Typically we would also call
* focus() here, but that isn't necessary in this case
* because the textarea already has [autoFocus] set.
*/
const length = textareaRef.current.value.length
textareaRef.current.setSelectionRange(length, length)
}
}, [])

const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing &&
!props.isSubmitDisabled) {
Expand All @@ -38,6 +53,7 @@ function EditInput(props: Props) {
data-replicated-value={text}
>
<textarea
ref={textareaRef}
value={text}
onChange={(e) => setText(e.currentTarget.value)}
onKeyDown={handleKeyDown}
Expand Down
Loading