1

实际上我想使用 react-quill 组件作为 antd Form.item 的一部分。

 <ReactQuill
   ref='editor'
   onChange={this.onChange}
 />

以上组件是 react-quill 基础组件。我需要像下面提到的那样使用

 <Field
   label="Departure"
   placeholder="Departure"
   name="departure"
   component={}
 />

上面<Field />,实际上是从 redux 表单导入道具,也就是我在AntdForm 中使用 Form.Item 如下

import {
  Form,
  Input,
} from 'antd'

const FormItem = Form.Item;

const makeField = Component => ({
  input,
  meta,
  children,
  hasFeedback,
  label,
  labelRight,
  ...rest
}) => {
  const hasError = meta.touched && meta.invalid;
  return (
    <FormItem
      {...formItemLayout}
      label={label}
      validateStatus={hasError ? 'error' : 'success'}
      hasFeedback={hasFeedback && hasError}
      help={hasError && meta.error}
    >
      <Component {...input} {...rest}>
        {children}
      </Component>
      {labelRight && (
        <span style={{ color: (rest.disabled && '#5a5a5a') || '#9e9e9e' }}>
          {labelRight}
        </span>
      )}
    </FormItem>
  );
};

export const AInput = makeField(Input);

表格中的用法

<Field
  label="Destination"
  placeholder="Destination"
  name="destination"
  component={AInput}
/>

如上图,我是如何使用antd InputinForm.Item而不是 render in 的Redux-Form Field。同样,我需要使用React-Quill组件。

4

2 回答 2

1

一种方法是将隐藏的 antd 包裹<Input />getFieldDecorator. 然后,渲染 react-quill 输入并使用 hidden<Input />来管理它的状态。请参阅使用普通的此示例<input />

class Form extends React.Component {
  handleChange = e => {
    const { value } = e.target;

    this.props.form.setFieldsValue({ input: value });
  };

  render() {
    const { getFieldValue, getFieldDecorator } = this.props.form;

    return (
      <Form layout="inline">
        {/* This is a hidden antd Input used to manage form state */}
        {getFieldDecorator("input")(<Input style={{ display: "none" }} />)}

        <input
          type="text"
          onChange={this.handleChange}
          value={getFieldValue("input")}
        />

        <Form.Item>
          <Button
            type="primary"
            htmlType="submit"
            onClick={() => console.log(getFieldValue("input"))}
          >
            test
          </Button>
        </Form.Item>
      </Form>
    );
  }
}

编辑 qyol7omww

于 2019-02-12T17:47:13.500 回答
0

"react-quill": "^1.3.3"https://www.npmjs.com/package/react-quill安装

我已经制作了我的一个 formHOC 实用程序文件,我从中导入了所有表单组件。同样设计这个组件。

import ReactQuill from "react-quill"; // third party library "react-quill": "^1.3.3",

import {
  Form,
} from 'antd'; 

// customization shown below

const FormItem = Form.Item;

var formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 8 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 16 },
  },
};

const makeEditorField = Component => ({
                                  input,
                                  meta,
                                  children,
                                  hasFeedback,
                                  label,
                                  labelRight,
                                  ...rest
                                }) => {
  const hasError = meta.touched && meta.invalid;
  return (
    <FormItem
      {...formItemLayout}
      label={label}
      validateStatus={hasError ? 'error' : 'success'}
      hasFeedback={hasFeedback && hasError}
      help={hasError && meta.error}
    >
      <Component {...input} {...rest}
       onBlur={(range, source, quill) => {
         input.onBlur(quill.getHTML());
       }}
      >
        {children}
      </Component>
      {labelRight && (
        <span style={{ color: (rest.disabled && '#5a5a5a') || '#9e9e9e' }}>
          {labelRight}
        </span>
      )}
    </FormItem>
  );
};

export const AEditor= makeEditorField(ReactQuill); // Export it to use other files.

用法

import {AEditor} from "../../utils/formHOC";
import { Field, reduxForm } from 'redux-form/immutable';
 <Field
            label="Departure"
            placeholder="Departure"
            name="departure"
            modules={modules}
            formats={formats}
            component={AEditor}
          />
于 2019-07-29T07:23:26.310 回答