Skip to content

Instantly share code, notes, and snippets.

@NilsonLima
Last active June 11, 2019 19:54
Show Gist options
  • Save NilsonLima/3ba1be568975d8d98342527e40bfbd1d to your computer and use it in GitHub Desktop.
Save NilsonLima/3ba1be568975d8d98342527e40bfbd1d to your computer and use it in GitHub Desktop.
// @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