Skip to content

Instantly share code, notes, and snippets.

@luandevpro
Created January 16, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luandevpro/3c11eef5da7358c41498144d6ff81b9f to your computer and use it in GitHub Desktop.
Save luandevpro/3c11eef5da7358c41498144d6ff81b9f to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import { Formik } from "formik";
import FormField from "./FormField";
import {
EditorState,
convertToRaw,
convertFromRaw
} from "draft-js";
export default class Draft extends Component {
constructor(props) {
super(props);
this.state = {
editorState: new EditorState.createEmpty(),
};
}
handleSubmit = (values, { resetForm }) => {
const rawContentState = convertToRaw(
values.editorState.getCurrentContent()
);
const editorState = EditorState.createWithContent(
convertFromRaw(rawContentState)
);
this.setState({
editorState,
});
console.log(JSON.stringify(rawContentState, null, 2));
resetForm({
editorState: new EditorState.createEmpty(),
});
};
render() {
var { editorState } = this.state;
return (
<div>
<Formik
initialValues={{ editorState }}
onSubmit={this.handleSubmit}
>
{props => <FormField {...props} />}
</Formik>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment