Skip to content

Commit f9d464a

Browse files
committed
feat: add focus functionality to CommentCreate input when replying
1 parent cd4be95 commit f9d464a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/components/CommentCreate/index.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useState, useMemo, useCallback, useEffect } from "react";
1+
import React, {
2+
useState,
3+
useMemo,
4+
useCallback,
5+
useEffect,
6+
useRef,
7+
} from "react";
28
import * as S from "./styles";
39
import ViewMarkdown from "@components/ViewMarkdown";
410

@@ -18,6 +24,8 @@ const CommentCreate: React.FC<ICommentCreate> = ({
1824
const [comment, setComment] = useState("");
1925
const [preview, setPreview] = useState<boolean>(false);
2026

27+
const inputRef = useRef<HTMLTextAreaElement>(null);
28+
2129
const account = useMemo(
2230
() => ({
2331
isLogged: true,
@@ -29,10 +37,6 @@ const CommentCreate: React.FC<ICommentCreate> = ({
2937
[]
3038
);
3139

32-
const clearComment = useCallback(() => {
33-
setComment("");
34-
}, []);
35-
3640
const withMention = useCallback(
3741
(content: string) => {
3842
if (!isReplyOverReply) {
@@ -78,6 +82,12 @@ const CommentCreate: React.FC<ICommentCreate> = ({
7882
[account]
7983
);
8084

85+
useEffect(() => {
86+
if (isReplying) {
87+
inputRef.current?.focus();
88+
}
89+
}, [isReplying]);
90+
8191
return (
8292
<S.Container>
8393
<S.CommentSection>
@@ -94,6 +104,7 @@ const CommentCreate: React.FC<ICommentCreate> = ({
94104
placeholder="Deixe seu comentário..."
95105
value={comment}
96106
onChange={handleChange}
107+
ref={inputRef}
97108
/>
98109
)}
99110

0 commit comments

Comments
 (0)