Antd Form.List支持选中Select值后更新相关联Select选项

效果如下图,

import React from 'react'
import { Form, Button, Select } from 'antd'
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
// 第一个下拉框数据
const options = [
  { value: 1, label: 'A' },
  { value: 2, label: 'B' },
  { value: 3, label: 'C' }
]

// 第二个下拉框数据
const obj = {
  A: [
    { value: 1, label: 'A1' },
    { value: 1, label: 'A2' },
    { value: 1, label: 'A3' }
  ],
  B: [
    { value: 1, label: 'B1' },
    { value: 1, label: 'B2' },
    { value: 1, label: 'B3' }
  ],
  C: [
    { value: 1, label: 'C1' },
    { value: 1, label: 'C2' },
    { value: 1, label: 'C3' }
  ]
}

const FormComponent = () => {
  const [form] = Form.useForm()
  return (
    <Form form={form}>
      <Form.Item shouldUpdate={true} noStyle={true}>
        {() => (
          <Form.List name="list">
            {(fields, { add, remove }) => {
              return (
                <div className="operation">
                  {fields.map((field, index) => (
                    <div className="input-item" key={`${field.key}${index}`}>
                      <Form.Item
                        {...field}
                        name={[field.name, 'distalEquipment']}
                        label="上联设备"
                        rules={[{ required: true }]}
                      >
                        <Select labelInValue={true} options={options} />
                      </Form.Item>
                      <Form.Item
                        {...field}
                        name={[field.name, 'distalEquipmentInterface']}
                        label="上联口"
                        rules={[{ required: true }]}
                      >
                        <Select
                          options={
                            obj?.[
                              form.getFieldValue([
                                'list',
                                field.name,
                                'distalEquipment'
                              ])?.label
                            ] || []
                          }
                        />
                      </Form.Item>
                      {field.name !== 0 && (
                        <MinusCircleOutlined
                          className="delete-btn"
                          onClick={() => remove(field.name)}
                        />
                      )}
                    </div>
                  ))}
                  <Form.Item>
                    <Button
                      type="dashed"
                      onClick={() => add()}
                      block={true}
                      icon={<PlusOutlined />}
                    />
                  </Form.Item>
                </div>
              )
            }}
          </Form.List>
        )}
      </Form.Item>
    </Form>
  )
}
export default FormComponent
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
antd Form.List 中,一个 Form.Item 可以通过使用 getFieldDecorator 方法来获取自身的值,而获取其他 Form.Item 的值则需要通过 Form.List 的方法进行。例如,可以通过调用 form.getFieldValue 方法来获取其他 Form.Item 的值。具体实现步骤如下: 1. 在 Form.List 中,给每个 Form.Item 一个唯一的 name 属性,例如 name={`items[${index}].name`},其中 index 为当前 Form.Item 的索引。 2. 在 getFieldDecorator 方法中,使用 getFieldValue 方法来获取其他 Form.Item 的值。示例代码如下: ```jsx {getFieldDecorator(`items[${index}].name`, { initialValue: item.name, rules: [{ required: true, message: 'Please input name' }], })( <Input placeholder="Enter name" /> )} {getFieldDecorator(`items[${index}].age`, { initialValue: item.age, rules: [{ required: true, message: 'Please input age' }], })( <InputNumber placeholder="Enter age" min={0} /> )} {getFieldDecorator(`items[${index}].email`, { initialValue: item.email, rules: [{ required: true, message: 'Please input email' }], })( <Input placeholder="Enter email" /> )} ``` 3. 在需要获取其他 Form.Item 值的地方,调用 form.getFieldValue 方法。示例代码如下: ```jsx const handleSubmit = e => { e.preventDefault(); form.validateFields((err, values) => { if (!err) { // 获取其他 Form.Item 值 const names = form.getFieldValue('items').map(item => item.name); console.log(names); // 提交表单 console.log('Received values of form: ', values); } }); }; ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值