9.Ant Design Pro Form.List多层嵌套

一、问题的描述

根据业务需求,多层嵌套。

二、正常的预期是什么?

在这里插入图片描述

三、问题的解决方案

初始化的数据结构:

newFuncValist = [
    {
      dpList: [
        {
          dpName: null,
          identifier: null,
          value: null,
          newArr: [],
        },
      ],
      funcValue: null,
    }
]

多层次嵌套表单,拿到后台返回的数据之后,需要setFieldsValue。

 form?.setFieldsValue({funcValList: newFuncValist || []});

然后分别给Form.List配置initialValue 初始值。

根据数据结构,就对应层次结构,最外层的

 <Form.List
      name="funcValList"
      initialValue={[
        {
          dpList: [
            {
              dpName: null,
              identifier: null,
              value: null,
              newArr: [],
            },
          ],
          funcValue: null,
        },
      ]}
>

再往内一层的FormList 对应

<Form.List
    name={[field.name, 'dpList']}
    initialValue={[
      {
        dpName: null,
        identifier: null,
        value: null,
        newArr: [],
      },
    ]}
  >

全部代码如下

  <Form form={form} onFinish={handleFormSumbit} autoComplete="off">
        {parseType === 'object' && (
          <ProCard
            className={Styles.mb20}
            bordered
            style={{ width: '700px' }}
            title={intl.formatMessage({
              id: 'product.stepTwo.deviceFunction',
              defaultMessage: '设备功能响应',
            })}
          >
            <Form.List
              name="funcValList"
              initialValue={[
                {
                  dpList: [
                    {
                      dpName: null,
                      identifier: null,
                      value: null,
                      newArr: [],
                    },
                  ],
                  funcValue: null,
                },
              ]}
            >
              {(fields, { add, remove }) => (
                <>
                  <div className={Styles.cardCon} id="cardCon" ref={cardRef}>
                    {fields.map((field, index) => (
                      <ProCard key={field.key} style={{ marginBottom: 8 }} bordered gutter={12}>
                        {index !== 0 && (
                          <div className={Styles.removeIcon} onClick={() => remove(field.name)}>
                            <CloseOutlined style={{ fontSize: '18px' }} />
                          </div>
                        )}
                        <ProFormSelect
                          {...field}
                          label={intl.formatMessage({
                            id: 'product.stepTwo.function',
                            defaultMessage: '功能',
                          })}
                          name={[field.name, 'funcValue']}
                          colProps={{ md: 12, xl: 8 }}
                          rules={[
                            {
                              required: true,
                              message: intl.formatMessage({
                                id: 'product.stepTwo.selectFunc',
                                defaultMessage: '请选择功能',
                              }),
                            },
                          ]}
                          width="md"
                          options={intentStatusList}
                          placeholder={intl.formatMessage({
                            id: 'product.stepTwo.selectFunc',
                            defaultMessage: '请选择功能',
                          })}
                        />
                        <Form.Item noStyle shouldUpdate>
                          {() => (
                            <Form.Item
                              {...field}
                              noStyle
                              name={[field.name, 'name']}
                              fieldKey={[field.key, 'name']}
                            >
                              <Form.List
                                name={[field.name, 'dpList']}
                                initialValue={[
                                  {
                                    dpName: null,
                                    identifier: null,
                                    value: null,
                                    newArr: [],
                                  },
                                ]}
                              >
                                {(fieldsInside, { add: addInside, remove: removeInside }) => (
                                  <div>
                                    {fieldsInside.map((fieldInside, indexz) => (
                                      <div key={fieldInside.key} className={Styles.currentDpitem}>
                                        <Form.Item noStyle shouldUpdate>
                                          {() => (
                                            <ProForm.Group size={8}>
                                              <div style={{ display: 'flex', marginRight: '10px' }}>
                                                <ProFormSelect
                                                  {...fieldInside}
                                                  label={intl.formatMessage({
                                                    id: 'product.stepTwo.setFunc',
                                                    defaultMessage: '设置功能点',
                                                  })}
                                                  name={[fieldInside.name, 'identifier']}
                                                  width="sm"
                                                  rules={[
                                                    {
                                                      required: true,
                                                      message: intl.formatMessage({
                                                        id: 'product.stepTwo.selectLight',
                                                        defaultMessage: '请选择灯光模式',
                                                      }),
                                                    },
                                                  ]}
                                                  fieldProps={{
                                                    labelInValue: true,
                                                    onChange: (val) => {
                                                      changeLightMode(val, [field.name, indexz]);
                                                    },
                                                  }}
                                                  options={lightModeList}
                                                  placeholder={intl.formatMessage({
                                                    id: 'product.stepTwo.selectLight',
                                                    defaultMessage: '请选择灯光模式',
                                                  })}
                                                />
                                                <ProFormSelect
                                                  {...fieldInside}
                                                  label={intl.formatMessage({
                                                    id: 'product.stepTwo.beCome',
                                                    defaultMessage: '变成',
                                                  })}
                                                  name={[fieldInside.name, 'value']}
                                                  width="sm"
                                                  rules={[
                                                    {
                                                      required: true,
                                                      message: intl.formatMessage({
                                                        id: 'product.stepTwo.selectDazzle',
                                                        defaultMessage: '请选择炫彩模式',
                                                      }),
                                                    },
                                                  ]}
                                                  options={
                                                    (form.getFieldValue('funcValList')[field.name]
                                                      .dpList[indexz] &&
                                                      form.getFieldValue('funcValList')[field.name]
                                                        .dpList[indexz].newArr.length > 0 &&
                                                      form.getFieldValue('funcValList')[field.name]
                                                        .dpList[indexz].newArr) ||
                                                    []
                                                  }
                                                  placeholder={intl.formatMessage({
                                                    id: 'product.stepTwo.selectDazzle',
                                                    defaultMessage: '请选择炫彩模式',
                                                  })}
                                                />
                                              </div>
                                            </ProForm.Group>
                                          )}
                                        </Form.Item>
                                        <div>
                                          {fieldInside.name !== 0 && (
                                            <div
                                              className={Styles.delFunc}
                                              onClick={() => removeInside(fieldInside.name)}
                                            >
                                              <DeleteOutlined />
                                            </div>
                                          )}
                                        </div>
                                      </div>
                                    ))}
                                    {(!form.getFieldValue('funcValList')[field.name]?.dpList
                                      ?.length ||
                                      form.getFieldValue('funcValList')[field.name]?.dpList
                                        ?.length < colorfulModeList.length) && (
                                      <Form.Item>
                                        <Button
                                          icon={<PlusOutlined />}
                                          onClick={() => {
                                            addInside();
                                          }}
                                          type="primary"
                                        >
                                          {intl.formatMessage({
                                            id: 'product.stepTwo.addDp',
                                            defaultMessage: '添加DP点',
                                          })}
                                        </Button>
                                      </Form.Item>
                                    )}
                                  </div>
                                )}
                              </Form.List>
                            </Form.Item>
                          )}
                        </Form.Item>
                      </ProCard>
                    ))}
                  </div>
                  {(!form.getFieldValue('funcValList').length ||
                    form.getFieldValue('funcValList').length < intentStatusList.length) && (
                    <Form.Item>
                      <Button
                        onClick={() => {
                          add();
                          dealFlex();
                        }}
                        type="primary"
                      >
                        {intl.formatMessage({
                          id: 'product.schemeConfig.addFunc',
                          defaultMessage: '添加功能',
                        })}
                      </Button>
                    </Form.Item>
                  )}
                </>
              )}
            </Form.List>
          </ProCard>
        )}
      </Form>

OK,收工!如果可以实现记得点赞分享,谢谢老铁~

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端加油站

你遇到的坑将由我来踩

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值