Created
January 16, 2019 13:25
-
-
Save luandevpro/3c11eef5da7358c41498144d6ff81b9f to your computer and use it in GitHub Desktop.
This file contains 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 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