antd-react-form-动态增删表单项

以antd4.x版本为例。代码如下:

const [checkList] = useState([{ label: '客户端', value: 1 }, { label: '服务端', value: 2 }, { label: 'MTOP', value: 3 }]);// checked列表
  const [showFlag, setShowFlag] = useState(false); // 控制是否可以增添表单        
       <Form
          {...formItemLayout}
          form={form}
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
        >
          <Form.Item
            label="场景名称"
            name="sceneName"
            rules={[
              {
                required: true,
                message: '请输入场景名称',
              },
              { pattern: new RegExp(/^\S+/, 'g'), message: '场景名称不能包含空格' },
            ]}
          >
            <Input style={{ width: 260 }} />
          </Form.Item>

          <Form.Item
            label="fromSource"
            name="fromSource"
          >
            <Input style={{ width: 260 }} />
          </Form.Item>

          <Form.Item
            label="调用方法"
            name="invokeTypes"
            rules={[
              {
                required: true,
                message: '请选择调用方法',
              },
            ]}
          >
            <Checkbox.Group>
              {checkList.map(item => (
                <Checkbox value={item.value} key={item.value} onChange={checkBoxChange}>{item.label}</Checkbox>
              ))}
            </Checkbox.Group>
          </Form.Item>

          <Form.List name="interfaceUrls" initialValue={['']}>
            {(fields, { add, remove }) => (
              <>
                {
                  fields.map((field, index) => (
                    <Form.Item
                      label="接口地址"
                      key={field.key}
                    >
                      <Form.Item
                        {...field}
                        noStyle
                      >
                        <Input style={{ width: 260, marginRight: '13px' }} />
                      </Form.Item>
                      {showFlag && index === 0 ? (<a onClick={() => add()}>添加</a>) : null}
                      {index !== 0
                        ? (<a onClick={() => remove(field.name)}>删除</a>)
                        : null}
                    </Form.Item>
                  ))
                }
              </>
            )}
          </Form.List>

          <Form.Item
            label="用户路径"
            name="userPath"
            rules={[
              {
                required: true,
                message: '请输入用户路径',
              },
            ]}
          >
            <Input style={{ width: 260 }} />
          </Form.Item>
        </Form>

效果如下:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值