首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在antd设计中使用带有表单FormItem的React-Quill

在antd设计中使用带有表单FormItem的React-Quill
EN

Stack Overflow用户
提问于 2019-02-12 15:35:22
回答 2查看 2.6K关注 0票数 1

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

代码语言:javascript
复制
 <ReactQuill
   ref='editor'
   onChange={this.onChange}
 />

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

代码语言:javascript
复制
 <Field
   label="Departure"
   placeholder="Departure"
   name="departure"
   component={}
 />

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

代码语言:javascript
复制
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);

表单中的用法

代码语言:javascript
复制
<Field
  label="Destination"
  placeholder="Destination"
  name="destination"
  component={AInput}
/>

如上所示,我如何在Form.Item中使用antd Input,然后在Redux-Form Field中渲染。类似地,我需要使用React-Quill组件。

EN

回答 2

Stack Overflow用户

发布于 2019-02-13 01:47:14

要做到这一点,一种方法是在getFieldDecorator中包装一个隐藏的antd <Input />。然后,呈现react-quill输入并使用隐藏的<Input />来管理它的状态。请参阅使用普通<input />的示例

代码语言:javascript
复制
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>
    );
  }
}

票数 1
EN

Stack Overflow用户

发布于 2019-07-29 15:23:26

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

我已经创建了一个formHOC util文件,我可以从中导入所有表单组件。同样地,也要设计这个组件。

代码语言:javascript
复制
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.

使用

代码语言:javascript
复制
import {AEditor} from "../../utils/formHOC";
import { Field, reduxForm } from 'redux-form/immutable';
代码语言:javascript
复制
 <Field
            label="Departure"
            placeholder="Departure"
            name="departure"
            modules={modules}
            formats={formats}
            component={AEditor}
          />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54644935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档