• 【form校验】3.0项目多层list嵌套


    请添加图片描述

    
    const { required, phoneOrMobile } = CjmForm.rules;
    export default function detail() {
      const { query } = getRouterInfo(location);
    
      const formRef = useRef(null);
    
      const [crumbList, setCrumbList] = useState([
        {
          url: "/wenling/Reviewer",
          name: "审核人员",
        },
        {
          name: query.type == "look" ? "查看" : "编辑",
        },
      ]);
      const [form, setForm] = useState({
        auditNodeConfig: [
          {
            nextAuditNode: "农办审批",
            userList: [
              {
                batchAudit: false,
                phone: "",
              },
            ],
          },
          {
            nextAuditNode: "农水局审批",
            userList: [
              {
                batchAudit: false,
                phone: "",
              },
            ],
          },
          {
            nextAuditNode: "同步财政局审核结果",
            userList: [
              {
                batchAudit: false,
                phone: "",
              },
            ],
          },
        ],
      });
      const [loading, setLoading] = useState(false);
      const [spinning, setSpinning] = useState(false);
    
      useEffect(() => {
        getConfigInfo();
      }, []);
    
      async function getConfigInfo() {
        setSpinning(true);
        const res = await configLook({ confId: query.confId });
        if (res.results && res.state && res.state == 200) {
          setForm(transformData(res.results));
        } else {
          if (query.processCode == "10001002") {
            setForm({
              auditNodeConfig: [
                {
                  nextAuditNode: "农办审批",
                  userList: [
                    {
                      batchAudit: false,
                      phone: "",
                    },
                  ],
                },
              ],
            });
          }
        }
        setSpinning(false);
      }
    
      const rules = useMemo(() => {
        let newRule = {};
        form["auditNodeConfig"].forEach((item, key) => {
          item["userList"].forEach((record, recordKey) => {
            newRule[`auditNodeConfig.${key}.userList.${recordKey}.phone`] = [
              required,
              phoneOrMobile,
            ];
            newRule[`auditNodeConfig.${key}.userList.${recordKey}.userId`] = [
              required,
            ];
          });
        });
        return newRule;
      }, [form]);
    
      const modalForm = useMemo(() => {
        let newForm = {};
        form["auditNodeConfig"].forEach((item, key) => {
          item["userList"].forEach((record, recordKey) => {
            newForm[`auditNodeConfig.${key}.userList.${recordKey}.phone`] =
              record["phone"];
            newForm[`auditNodeConfig.${key}.userList.${recordKey}.userId`] =
              record["userId"];
          });
        });
        return newForm;
      }, [form]);
    
      const getValue = (eventOrvalue) => {
        const type = typeof eventOrvalue;
        if (
          type === "object" &&
          eventOrvalue !== null &&
          eventOrvalue.target &&
          eventOrvalue.target.value
        ) {
          return eventOrvalue.target.value;
        }
        return eventOrvalue;
      };
    
      const setFormData = (fieldName, index, recordKey) => {
        return (eventOrvalue) => {
          let value = getValue(eventOrvalue);
          if (fieldName == "batchAudit") {
            value = eventOrvalue.target.checked;
          }
          form["auditNodeConfig"][index]["userList"][recordKey][fieldName] = value;
          setForm(Object.assign({}, form));
        };
      };
    
      const addHandle = (index) => {
        const listLen = get(form, `auditNodeConfig[${index}].userList`, []).length;
        if (listLen >= 4) {
          Message.warning("最多添加4个审批人员!");
        } else {
          form["auditNodeConfig"][index]["userList"].push({
            batchAudit: false,
            phone: "",
          });
          setForm(Object.assign({}, form));
        }
      };
    
      const saveHandle = async () => {
        setLoading(true);
        const valid = await formRef.current.validate();
        if (valid) {
          let data = { ...form };
          data["confId"] = query.confId;
          data["processCode"] = query.processCode;
          const res = await configEdit(transformTree(data));
          if (res.state && res.state == 200) {
            Message.success("操作成功");
            historyBack();
          } else {
            Message.error("操作失败,请重试!");
          }
        }
        setLoading(false);
      };
    
      const delHandle = (index, recordKey) => {
        form["auditNodeConfig"][index]["userList"].splice(recordKey, 1);
        setForm(Object.assign({}, form));
      };
    
      return (
        <div className={styles.reviewerBox}>
          <Spin spinning={spinning}>
            <CrumbBar list={crumbList} />
            <CjmForm
              labelWidth="180px"
              ref={formRef}
              model={modalForm}
              rules={rules}
            >
              <CjmForm.Item label="审批类型:">
                <CjmInput
                  value={query.processName}
                  disabled={true}
                  style={{ width: "380px" }}
                />
              </CjmForm.Item>
              {form &&
                form.auditNodeConfig.map((item, index) => {
                  return (
                    <div className={styles.auditItem} key={index}>
                      <h2 style={{ marginLeft: "24px" }}>
                        {item["nextAuditNode"]}
                      </h2>
                      {item["userList"] &&
                        item["userList"].map((record, recordKey) => {
                          return (
                            <div key={recordKey} className={styles.formItem}>
                              <CjmForm.Item
                                label="审批人名称:"
                                prop={`auditNodeConfig.${index}.userList.${recordKey}.userId`}
                              >
                                <RemoteSelect
                                  style={{ width: "380px" }}
                                  disabled={query.type == "look" ? true : false}
                                  labelKey="userName"
                                  valueKey="userId"
                                  value={record.userId}
                                  label={record.userName}
                                  remoteUrl={`${api.reviewer.departmentId}?departmentId=${query.optDeptId}&disableFlag=1`}
                                  onChange={{
                                    userName: setFormData(
                                      "userName",
                                      index,
                                      recordKey
                                    ),
                                    userId: setFormData("userId", index, recordKey),
                                    departmentId: setFormData(
                                      "deptId",
                                      index,
                                      recordKey
                                    ),
                                    mobileId: setFormData(
                                      "phone",
                                      index,
                                      recordKey
                                    ),
                                  }}
                                />
                              </CjmForm.Item>
                              <CjmForm.Item
                                label="审批人手机号:"
                                prop={`auditNodeConfig.${index}.userList.${recordKey}.phone`}
                              >
                                <CjmInput
                                  disabled={query.type == "look" ? true : false}
                                  style={{ width: "380px" }}
                                  value={record.phone}
                                  onChange={setFormData("phone", index, recordKey)}
                                />
                              </CjmForm.Item>
                              <CjmForm.Item label="审批操作:" prop="batchAudit">
                                <Checkbox
                                  disabled={query.type == "look" ? true : false}
                                  checked={record.batchAudit}
                                  onChange={setFormData(
                                    "batchAudit",
                                    index,
                                    recordKey
                                  )}
                                >
                                  批量通过
                                </Checkbox>
                                <span style={{ marginLeft: "24px" }}>
                                  默认单个审核,设置批量审核后允许该节点可批量审核
                                </span>
                              </CjmForm.Item>
                              <Divider className={styles.line} />
                              {recordKey != 0 && (
                                <img
                                  className={styles.close}
                                  src={deleteImg}
                                  onClick={() => delHandle(index, recordKey)}
                                />
                              )}
                            </div>
                          );
                        })}
                      {query.type !== "look" && index == 0 && (
                        <Button
                          type="primary"
                          style={{ marginLeft: "224px" }}
                          onClick={() => addHandle(index)}
                        >
                          +新增(最多添加4个审批人员)
                        </Button>
                      )}
                    </div>
                  );
                })}
            </CjmForm>
            <Button className={styles.btnLeave} onClick={() => historyBack()}>
              返回
            </Button>
            {query.type !== "look" && (
              <Button
                type="primary"
                className={styles.btnSave}
                loading={loading}
                onClick={saveHandle}
              >
                保存
              </Button>
            )}
          </Spin>
        </div>
      );
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
  • 相关阅读:
    【蓝桥杯单片机】六、 PCF8591- DAC和DAC
    激光SLAM后端优化总结之图优化
    交换机基础知识之安全配置
    数字时代的自我呈现:探索个人形象打造的创新工具——FaceChain深度学习模型工具
    小程序源码:纯头像微信小程序源码下载,多分类头像自动采集无需服务器和域名-多玩法安装简单
    外国固定资产管理系统功能有哪些
    Java核心篇,二十三种设计模式(十五),行为型——解析器模式
    嵌入式Linux 开发经验:注册一个 misc 设备
    【JavaEE初阶】计算机是如何工作的
    [pytorch] 2D + 3D EfficientNet代码 实现,改写
  • 原文地址:https://blog.csdn.net/qq_60178319/article/details/134248868