[React] Antd Form.List 的基本使用

本文介绍了Ant Design中Form.List组件的基本用法,用于管理数组形式的表单字段。通过示例展示了如何添加和移除表单项,以及配置用户填写信息的标题。数据结构为infoList,包含中文和英文标题。

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

[React] Antd Form.List 的基本使用

Form.List 为字段提供数组化管理。

官网:https://ant.design/components/form-cn/#Form.List

<Form.List name='infoList'>
 {(fields, {add, remove}) => (
    <>
      <FormItem>
        {gettext('用户填写信息')}
        <Button
          type='primary'
          style={{marginLeft: '20px'}}
          ghost
          disabled={!canEdit}
          shape='circle'
          onClick={() => add()}
        >
          +
        </Button>
      </FormItem>
      <FormItem>
        <div style={{color: '#999'}}>{gettext('请在下方输入框中配置用户需要填写的信息标题,如手机号码')}</div>
      </FormItem>
      {fields.map(({key, name, ...restField}) => (
        <div className='infoItem' key={key}>
          <div className='removeBtn'>
            <Button danger ghost shape='circle' disabled={!canEdit} onClick={() => remove(name)}>
              -
            </Button>
          </div>
          <FormItem label={gettext('中文标题')} name={[name, 'info']} required rules={utils.form.setRules()}>
            <Input disabled={!canEdit} placeholder={gettext('请输入中文标题')} />
          </FormItem>

          <FormItem label={gettext('英文标题')} name={[name, 'infoEn']} required rules={utils.form.setRules()}>
            <Input disabled={!canEdit} placeholder={gettext('请输入英文标题')} />
          </FormItem>
        </div>
      ))}
    </>
  )}
</Form.List>

页面样式:

在这里插入图片描述

数据结构:

infolist: [
	{info:'姓名',infoEn:'Name'},
	{info:'邮箱',infoEn:'Email'},
]

在这里插入图片描述

### 使用 Ant Design 的 `Form.List` 实现动态表单项 为了实现动态增删表单项的功能,在 Ant Design 中可以利用 `Form.List` 组件。此组件允许开发者轻松管理不确定数量的子表单字段集合。 下面是一个具体的实例,展示了如何设置初始值使得页面加载时默认显示至少一个输入框,并提供了添加新条目和移除现有条目的功能: ```jsx import React from 'react'; import { Form, Input, Button } from 'antd'; const DynamicFormItemExample = () => { const layout = { labelCol: { span: 8 }, wrapperCol: { span: 16 }, }; return ( <Form name="dynamic_form_item" {...layout}> {/* 设置initialValue属性使首次渲染时就有一个空对象 */} <Form.List name="users" initialValue={[{}]}> {(fields, { add, remove }) => ( <> {fields.map(({ key, name, fieldKey, ...restField }) => ( <div style={{ marginBottom: 8 }} key={key}> <Form.Item required={false} {...restField} name={[name, 'firstName']} fieldKey={[fieldKey, 'firstName']} rules={[ { required: true, message: 'First Name is required', }, ]} > <Input placeholder="First Name" /> </Form.Item> <Button type="link" onClick={() => remove(name)}> Remove </Button> </div> ))} <Form.Item> <Button block onClick={() => add()} type="dashed"> Add Field </Button> </Form.Item> </> )} </Form.List> <Form.Item> <Button htmlType="submit">Submit</Button> </Form.Item> </Form> ); }; export default DynamicFormItemExample; ``` 这段代码实现了以下特性: - 页面初次加载时会自动展示一条空白记录供编辑[^1]。 - 用户可以通过点击按钮来新增更多的同类型输入区域[^1]。 - 对于不再需要的某一行,用户也可以选择将其删除。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值