I'm trying to learn how to use React-Final-Form (RFF in short).
I've learnt how to use <Field> component but now I need to add a custom component to use a WYSIWYG editor which is NOT provided by RFF.
So, I've chosen react-draft-wysiwyg.
Ok, first here my form:
const FormComponent = () => {
const handleSubmitOnClick = () => ({
news_title,
news_subtitle,
editor_content,
image_url,
}) => {
const data = {
"user": {
news_title: news_title,
news_subtitle: news_subtitle,
editor_content: editor_content <- here the content from the WYSIWYG editor
image_url: image_url
}
}
// API call here ....
}
return (
<>
<h1>News Main Page</h1>
<Form
onSubmit={handleSubmitOnClick()}
>
{
({
handleSubmit,
values,
submitting,
}) => (
<form onSubmit={handleSubmit} data-testid="form">
<Field
name='news_title'
placeholder='News Title'
validate={required}
>
{({ input, meta, placeholder }) => (
<div className={meta.active ? 'active' : ''}>
<input {...input}
type='text'
placeholder={placeholder}
/>
</div>
)}
</Field>
<Field
name='news_subtitle'
placeholder='News SubTitle'
validate={required}
>
{({ input, meta, placeholder }) => (
<div className={meta.active ? 'active' : ''}>
<input {...input}
type='text'
placeholder={placeholder}
/>
</div>
)}
</Field>
<WYSIWYGEditor /> **** HERE THE ISSUE ****
<MyDropzone />
<button
type="submit"
className="signup-button"
disabled={submitting}
>
Continue
</button>
</form>
)}
</Form>
</>
)
}
export default FormComponent;
This is the Editor file:
import React, { useState } from 'react';
// Components
import { EditorState, convertToRaw } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import draftToHtml from 'draftjs-to-html';
// Hooks version of the Class below (done by me)
const WYSIWYGEditor = () => {
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const onEditorStateChange = editorState => {
return setEditorState(editorState)
}
return (
<div className="editor">
<Editor
editorState={editorState}
wrapperClassName="demo-wrapper"
editorClassName="demo-editor"
onEditorStateChange={onEditorStateChange}
/>
{
console.log('editorState => ', draftToHtml(convertToRaw(editorState.getCurrentContent())))
}
</div>
)
}
export default WYSIWYGEditor
The <WYSIWYGEditor /> returns the correct value, no prob there, but I don't how to integrate this component to the RFF flow by using name='editor_content' and when the form submit button is clicked.
Any help is much appreciated.
Joe