Skip to content

Instantly share code, notes, and snippets.

@GenesisSam
Created November 6, 2019 07:44
Show Gist options
  • Save GenesisSam/ba9f282e351b7331c3898f373fb74428 to your computer and use it in GitHub Desktop.
Save GenesisSam/ba9f282e351b7331c3898f373fb74428 to your computer and use it in GitHub Desktop.
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