Использование React-Quill с Form FormItem в дизайне antd - PullRequest
0 голосов
/ 12 февраля 2019

На самом деле я хочу использовать компонент реактивного пера в составе antd Form.item.

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

Вышеуказанный компонент является базовым компонентом реактивного пера.Мне нужно использовать, как указано ниже

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

выше <Field />, на самом деле это импорт реквизитов из избыточной формы, то есть я использую в Antd форму как 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 Input в Form.Item и рендеринг в Redux-Form Field.Точно так же мне нужно использовать React-Quill Component.

Ответы [ 2 ]

0 голосов
/ 29 июля 2019

Установка "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}
          />
0 голосов
/ 12 февраля 2019

Один из способов сделать это - обернуть скрытый код <Input /> в getFieldDecorator.Затем выполните рендеринг входной реакции и используйте скрытый <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>
    );
  }
}

Edit qyol7omww

...