封装antd Form表单,简便inline排版不方便控制每行个数和label与wrap的宽度

博客主要讲述基于antd组件进行Form表单封装成FormPro。原因是原表单中labelCol和wrapperCol难以调整inline排列,且不方便控制每行表单展示个数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值