Skip to content

Commit

Permalink
Added a reference to the text field to focus when replying
Browse files Browse the repository at this point in the history
  • Loading branch information
berkaydoner committed Dec 8, 2021
1 parent b835e29 commit efb7cf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 8 additions & 3 deletions frontend/src/Views/Comments/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Avatar from '@mui/material/Avatar';
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Grid from "@mui/material/Grid";
export default function Comment(props){
const Comment = React.forwardRef((props,ref) => {
const [open, setOpen] = React.useState(true);
const object = props.content
const handleClick = () => {
Expand All @@ -14,6 +14,9 @@ export default function Comment(props){
if (!props.newcomment.includes("@")) {
props.setReply("@" + object.username + " " + props.newcomment)
}
setTimeout(() => {
ref.current.focus();
}, 100);
}
const formatDate = (dateString) => {
const months = ["Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"]
Expand Down Expand Up @@ -61,10 +64,12 @@ export default function Comment(props){
</ListItem>
{object.answers !== null && object.answers !== undefined && object.answers !== []
? object.answers.map(d =>
<Comment content={d} newcomment={props.newcomment} setReply={props.setReply}/>
<Comment content={d} newcomment={props.newcomment} setReply={props.setReply} ref={ref}/>
)
: null
}
</React.Fragment>
);
}
})
export default Comment;

4 changes: 3 additions & 1 deletion frontend/src/Views/Comments/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Button from "@mui/material/Button";
import TextField from '@mui/material/TextField';
import Comment from "./Comment";
import List from '@mui/material/List';

import Grid from "@mui/material/Grid";
import DialogContent from "@mui/material/DialogContent";
import {useRef} from "react";
Expand All @@ -12,6 +13,7 @@ export default function Comments(props){
const typoStyle = {fontSize:13,display:"flex", flexDirection: "column", justifyContent: "center"}
const [open, setOpen] = React.useState(false);
const [newComment, setNewComment] = React.useState("");
let textInput = useRef(null);

const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
Expand Down Expand Up @@ -49,7 +51,7 @@ export default function Comments(props){
<Grid container spacing={3}>
<Grid item sm={10}>
<TextField id="comment" fullWidth size="small" variant="outlined" placeholder="Add comment..."
autoFocus style={{marginLeft:"3%",marginBottom:"3%"}} value={newComment||""} onChange={handleInput}></TextField>
inputRef={textInput} autoFocus style={{marginLeft:"3%",marginBottom:"3%"}} value={newComment||""} onChange={handleInput}></TextField>
</Grid>
<Grid item sm={2}>
<Button onClick={handlePostComment}>Share</Button>
Expand Down

0 comments on commit efb7cf5

Please sign in to comment.