This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a69a55
commit fed4a5a
Showing
8 changed files
with
360 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/Components/SideSheet/Comments/Components/InputField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { | ||
useRef, useState, useEffect, MutableRefObject, | ||
} from "react" | ||
import styled from "styled-components" | ||
|
||
const StyledDiv = styled.div` | ||
background-color: #F7F7F7; | ||
border-bottom: 1px solid #6F6F6F; | ||
cursor: text; | ||
padding: 8px; | ||
` | ||
|
||
const StyledP = styled.p<{ isPlaceholder: boolean }>` | ||
color: ${({ isPlaceholder }) => (isPlaceholder ? "grey" : "black")}; | ||
margin: 0; | ||
min-height: 18px; | ||
&:focus { | ||
outline: none; | ||
} | ||
span { | ||
color: #007079; | ||
font-weight: 500; | ||
` | ||
|
||
interface Props { | ||
placeholder?: string | ||
setSearchTerm: React.Dispatch<React.SetStateAction<string>> | ||
setShowTagDropDown: React.Dispatch<React.SetStateAction<boolean>> | ||
newReviewComment: any | ||
setNewReviewComment: React.Dispatch<React.SetStateAction<any>> | ||
taggedUsers: string[] | ||
} | ||
|
||
const InputField: React.FC<Props> = ({ | ||
placeholder = "Write a comment...", | ||
setSearchTerm, | ||
setShowTagDropDown, | ||
newReviewComment, | ||
setNewReviewComment, | ||
taggedUsers, | ||
}) => { | ||
const pRef = useRef<HTMLParagraphElement>(null) | ||
const [isPlaceholderShown, setIsPlaceholderShown] = useState(true) | ||
|
||
useEffect(() => { | ||
if (pRef.current) { | ||
console.log("re-rendering with the text: ", newReviewComment?.text) | ||
pRef.current.innerHTML = newReviewComment?.text || "" | ||
} | ||
}, [taggedUsers]) | ||
|
||
useEffect(() => { | ||
if (pRef.current && isPlaceholderShown) { | ||
pRef.current.innerHTML = placeholder | ||
} | ||
}, [isPlaceholderShown, placeholder]) | ||
|
||
const handleCommentChange = (commentText: string) => { | ||
const lastAtPos = commentText.lastIndexOf("@") | ||
const shouldShowDropdown = lastAtPos !== -1 | ||
|
||
setShowTagDropDown(shouldShowDropdown) | ||
|
||
if (shouldShowDropdown) { | ||
const termAfterAt = commentText.slice(lastAtPos + 1) | ||
setSearchTerm(termAfterAt) | ||
} else { | ||
setSearchTerm("") | ||
} | ||
|
||
const comment = { ...newReviewComment, text: commentText } | ||
setNewReviewComment(comment) | ||
} | ||
|
||
const handleFocus = () => { | ||
if (pRef.current) { | ||
pRef.current.contentEditable = "true" | ||
pRef.current.focus() | ||
if (isPlaceholderShown) { | ||
pRef.current.innerText = "" | ||
} | ||
} | ||
} | ||
|
||
const handleBlur = () => { | ||
if (pRef.current) { | ||
pRef.current.contentEditable = "false" | ||
const content = pRef.current.innerHTML | ||
handleCommentChange(content) | ||
if (content.trim() === "") { | ||
setIsPlaceholderShown(true) | ||
pRef.current.innerHTML = placeholder | ||
} else { | ||
setIsPlaceholderShown(false) | ||
} | ||
} | ||
} | ||
const handleInput = () => { | ||
if (pRef.current) { | ||
const content = pRef.current.innerHTML | ||
if (content !== placeholder) { | ||
handleCommentChange(content) | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<StyledDiv onClick={handleFocus}> | ||
<StyledP | ||
ref={pRef} | ||
onBlur={handleBlur} | ||
onInput={handleInput} | ||
isPlaceholder={isPlaceholderShown} | ||
/> | ||
</StyledDiv> | ||
) | ||
} | ||
|
||
export default InputField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.