I'm trying to append a drop down value to a textarea field, it works as a mention. So if there is an "@" call the dropdown and user will select a user, once selected the dropdown show hide it self, and the user should be able to append their comment data.
the issue im having is that
setCommentBodyis appending the selectedUser multiple times/ on every comment change in the textarea
My objective is
- check for @ symbol (which its already doing)
- render drop down once @ symbol is called( which is already doing)
- once a value is selected hide drop down and add their comment (dropdown hides only if @ symbol is removed, which it should hide after a value is selected)
The mention should pretty much work exactly how stackoverflow comment section has it.
this is what i have so far
const [comment_body, setCommentBody] = useState(""); const [mentionedUser, setMentionedUser] = useState(false); const commentChange = (comment) => { console.log("this is the selected User", selectedUser); // selected user is a reducer initalState // call this condition if @ is mentioned once if (comment.includes("@")) { setMentionedUser(true); // render dropwdown setCommentBody(comment.concat(selectedUser)); // append the selected user like @barnowl with the respective comment data } else { console.log("can you see me"); setMentionedUser(false); // hide dropdown setCommentBody(comment); } setGifUrl(""); // irrelvant to the problem ignore }; PostItemContainer
<CommentForm commentChange={(e: any) => commentChange(e.target.value)} comment_body={comment_body} onSubmit={(e) => commentSubmit(e, post.id)} gifUrl={selectGif} isGif={gifUrl} mentionedUser={mentionedUser} />; CommentForm (snippet)
.... <OurTextField type="gif-commentfield" selectedUser={selectedUser} comment_body={props.comment_body} commentChange={props.commentChange} setGifSelected={() => setGifSelected(true)} />; { props.mentionedUser && ( <select value={selectedUser} onChange={(e) => setSelectedOptionValue(e.target.value)} name="mentionedUsers" > {mentionUsers.map((item, key) => ( <option key={key} value={item.display}> {item.display} </option> ))} </select> ); } A minimal working example of my issue
https://codesandbox.io/s/practical-ives-lfckq?file=/src/App.js