3

我正在尝试学习如何使用 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 流中。

任何帮助深表感谢。

4

2 回答 2

4

好的,我通过查看 React-Final-Form 网站自己找到了解决方案。

为了<Field>在您的 RFF 表单中使用自定义解决方案或第 3 方组件,您需要添加以下内容:

// RFF 形式的主组件文件

....

<Field
  name="editor_content"
  component={WYSIWYGEditor}
/>

etc...

请注意:不要尝试以这种方式传递组件,component={<WYSIWYGEditor />}否则它会返回关于不允许传递对象的错误不要忘记导入组件:)

上面的示例将传递给自定义组件以收集数据并在表单中使用它,这是如何inputmeta

// 所见即所得编辑器文件

const WYSIWYGEditor = ({ input, meta }) => {
  const [editorState, setEditorState] = useState(EditorState.createEmpty());
  const onEditorStateChange = editorState => {
    setEditorState(editorState)
    return input.onChange(draftToHtml(convertToRaw(editorState.getCurrentContent())))

etc....
  } 

您会注意到,在onEditorStateChange我已经能够input从道具中使用的函数中,并且通过使用该onChange方法,我可以将返回的值传回给父组件(RFF 表单)。

我希望这对其他人有帮助。快乐编码!

于 2020-02-18T11:34:59.960 回答
2

这非常有帮助。如果不是 etc.... 你会放入完整版本的代码或接近它,它会更有帮助。我最终得到了这个:

import * as React from 'react';
import { useState } from 'react';
import { EditorState, convertToRaw } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';

// Hooks version of the Class below (done by me)
const WYSIWYGEditor = ({ input, meta }) => {
    const [editorState, setEditorState] = useState(EditorState.createEmpty());
    const onEditorStateChange = editorState => {
        setEditorState(editorState)
        return input.onChange(convertToRaw(editorState.getCurrentContent()))
    }

    return (
        <div className="editor">
            <Editor 
            editorState={editorState} 
            wrapperClassName="demo-wrapper"
            editorClassName="demo-editor"
            onEditorStateChange={onEditorStateChange}
            />
            {
                console.log('editorState => ', convertToRaw(editorState.getCurrentContent()))
            }
        </div>
    )
}

export default WYSIWYGEditor;
于 2021-01-26T06:50:22.297 回答