基于antd组件 Form表单封装FormPro
由于labelCol和wrapperCol不好去调整inline排列的表单;并且不方便控制每一行的表单展示个数;
import React from 'react';
import { Form, Input } from 'antd';
const index = (props) => {
const { formItemList = [], ...formProps } = props;
return (
<>
<Form
style={{
display: 'flex',
justifyContent: 'space-between',
flexWrap: 'wrap',
}}
colon={false}
{...formProps}
>
{formItemList.map((item, idx) => {
const { render, labelwidth, wrapperwidth, ...otherItem } = item;
return (
<Form.Item
{...otherItem}
key={String(idx)}
label={
item.label ? (
<div style={{ width: labelwidth }}>
{item.label}
{item.colon === false ? '' : ':'}
</div>
) : (
''
)
}
style={{
width: wrapperwidth,
marginBottom: 16,
...(item.style || {}),
}}
>
{render ? render() : <Input placeholder={item.placeholder} />}
</Form.Item>
);
})}
</Form>
</>
);
};
export default index;
# FormPro 基于 antd v4.18.9
# 使用方法可参考 antd Form 表单文档
# 封装属性介绍
# 基本使用
\```
const [form] = Form.useForm();
const formItemList = [
{
name: 'test',
label: '测试 label',
labelwidth: 50,
labelAlign: 'left',
wrapperwidth: '45%',
rules: [
{
required: true,
message: '请填写内容',
},
],
},
];
\```
# 建议使用 form 实例
<FormPro formItemList={formItemList} form={form}>
###### 参数 ------------------- 说明 ------------------------- 类型
formItemList 配置需要渲染的表单数据 FormItemListType[]
labelwidth label盒子的宽度 number | string
wrapperwidth ant-form-item的容器宽度 number | string
render 自定义渲染表单input类型 () => ReactNode