实际上,我想使用react-quill组件作为Form.item的一部分。
<ReactQuill
ref='editor'
onChange={this.onChange}
/>上面的组件是react-quill基本组件。我需要像下面提到的那样使用
<Field
label="Departure"
placeholder="Departure"
name="departure"
component={}
/>在<Field />上面,实际上它是从redux form导入道具,也就是,我在Antd Form中使用如下的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}
/>如上所示,我如何在Form.Item中使用antd Input,然后在Redux-Form Field中渲染。类似地,我需要使用React-Quill组件。
发布于 2019-02-13 01:47:14
要做到这一点,一种方法是在getFieldDecorator中包装一个隐藏的antd <Input />。然后,呈现react-quill输入并使用隐藏的<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>
);
}
}
发布于 2019-07-29 15:23:26
从https://www.npmjs.com/package/react-quill安装"react-quill": "^1.3.3"
我已经创建了一个formHOC util文件,我可以从中导入所有表单组件。同样地,也要设计这个组件。
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}
/>https://stackoverflow.com/questions/54644935
复制相似问题