Created
November 6, 2019 07:44
-
-
Save GenesisSam/ba9f282e351b7331c3898f373fb74428 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const CustomTextRenderInput: React.FC<IProps> = ({ | |
readonly, | |
value = "", | |
onChange, | |
}) => { | |
const [currentOriginalContent, setOriginalContent] = React.useState(value); | |
const [currentRenderedContent, setRenderedContent] = React.useState( | |
mentionRender(value, CommonRenderer), | |
); | |
const handleChange = React.useCallback((e: ContentEditableEvent) => { | |
const text = e.currentTarget.textContent || ""; | |
const linkifyResult = mentionRender(text, CommonRenderer); | |
setOriginalContent(text); | |
setRenderedContent(linkifyResult); | |
if (onChange) onChange(text); | |
}, []); | |
const handleKeyDown = React.useCallback( | |
(e: React.KeyboardEvent) => { | |
if (e.keyCode === 13 && !e.shiftKey) { | |
e.preventDefault(); | |
} | |
//--------------------- | |
// Unknonw: 이 코드가 호출될때 currentOriginalContent 에 데이터가 없다고 찍힘 | |
//--------------------- | |
if ((e.keyCode === 8 || e.keyCode === 46) && !currentOriginalContent) { | |
e.preventDefault(); | |
} | |
}, | |
[currentOriginalContent], | |
); | |
return ( | |
<Wrapper> | |
<ContentEditable | |
className="editor" | |
disabled={readonly} | |
html={currentRenderedContent} | |
onChange={handleChange} | |
onKeyDown={handleKeyDown} | |
/> | |
</Wrapper> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment