我正在尝试学习如何使用 React-Final-Form(简称 RFF)。
我已经学会了如何使用<Field>
组件,但现在我需要添加一个自定义组件来使用 RFF 不提供的 WYSIWYG 编辑器。
所以,我选择了 react-draft-wysiwyg。
好的,首先这里是我的表格:
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;
这是编辑器文件:
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
返回正确的<WYSIWYGEditor />
值,那里没有问题,但我不知道如何通过使用name='editor_content'
以及单击表单submit
按钮时将此组件集成到 RFF 流中。
任何帮助深表感谢。
乔