Last active
June 11, 2019 19:54
-
-
Save NilsonLima/3ba1be568975d8d98342527e40bfbd1d 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
| // @flow | |
| import * as React from "react"; | |
| import { Editor, EditorState } from "draft-js"; | |
| type Props = { | |
| onChangeText?: (text: string) => void, | |
| onSubmit?: void => void | |
| }; | |
| type State = { | |
| editorState: EditorState | |
| }; | |
| export default class CustomEditor extends React.Component<Props, State> { | |
| static defaultProps = { | |
| onChangeText: () => {}, | |
| onSubmit: () => {} | |
| }; | |
| state = { editorState: EditorState.createEmpty() }; | |
| onChange = (editorState: EditorState) => { | |
| const { onChangeText } = this.props; | |
| const text = editorState.getCurrentContent().getPlainText(); | |
| this.setState({ editorState }, () => { | |
| if (onChangeText) onChangeText(text); | |
| }); | |
| }; | |
| handleReturn = (event: SyntheticKeyboardEvent<*>) => { | |
| const { onSubmit } = this.props; | |
| if (event.shiftKey) return "not-handled"; | |
| if (onSubmit) onSubmit(); | |
| return "handled"; | |
| }; | |
| render() { | |
| const { editorState } = this.state; | |
| return ( | |
| <Editor | |
| editorState={editorState} | |
| onChange={this.onChange} | |
| handleReturn={this.handleReturn} | |
| /> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment