Created
June 28, 2020 23:53
-
-
Save dohomi/a25f0c146537035dd0dca741403f6253 to your computer and use it in GitHub Desktop.
Rich Text editor based on mui-rte which exposes innerHTML and innerTEXT
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
import MUIRichTextEditor from 'mui-rte' | |
import React from 'react' | |
import { stateToHTML } from 'draft-js-export-html' | |
import { TMUIRichTextEditorProps } from 'mui-rte/src/MUIRichTextEditor' | |
type CallbackProps = { | |
data: string | |
html: string | |
text: string | |
editorState: any | |
} | |
type RichTextProps = { | |
rteRef: any | |
getContent: (data: CallbackProps) => void | |
rteProps?: TMUIRichTextEditorProps | |
} | |
export default function RichText({ | |
rteRef, | |
getContent, | |
rteProps | |
}: RichTextProps): JSX.Element { | |
let _editorState: any = null | |
return ( | |
<MUIRichTextEditor | |
controls={[ | |
'bold', | |
'italic', | |
'underline', | |
'bulletList', | |
'numberList', | |
'undo', | |
'redo', | |
'clear' | |
]} | |
label={'Type your message here...'} | |
{...rteProps} | |
ref={rteRef} | |
onChange={(state) => { | |
_editorState = state | |
}} | |
onSave={(data) => { | |
const html = stateToHTML(_editorState.getCurrentContent()) | |
const temp = document.createElement('div') | |
temp.innerHTML = html | |
getContent({ | |
data, | |
editorState: _editorState, | |
html: html, | |
text: temp.textContent || temp.innerText | |
}) | |
}} | |
/> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment