import React, { useState, useEffect, Component } from 'react'; import { Button, Dialog, Card, Select, Grid, Field, Input } from '@alifd/next'; import { FORMULA_TABLE_TYPE } from '@/constants'; import { useGetFields } from '@/hooks/use-get-fields'; import { project } from '@alilc/lowcode-engine'; import RelationTree from '../components/RelationTree/index'; import './style.scss'; const { Row, Col } = Grid; const { Option } = Select; // 转化为嵌套的数组结构 const convertToNestedArray = (flatData: Record) => { // 创建结果对象 const result: any = {}; // 遍历所有扁平化的键值对 Object.entries(flatData).forEach(([path, value]) => { const parts = path.split('.'); // 递归设置嵌套值 let current: any = result; for (let i = 0; i < parts.length - 1; i++) { const part = parts[i]; if (part === 'groups') { if (!current[part]) current[part] = []; current = current[part]; } else if (part === 'children') { if (!current[part]) current[part] = []; current = current[part]; } else { const index = parseInt(part); if (!current[index]) current[index] = {}; current = current[index]; } } // 设置最终的值 current[parts[parts.length - 1]] = value; }); // 返回 groups 数组 return result.groups || []; }; const convertToFlatObject = (nestedArray: any) => { const result: any = {}; // 递归处理对象 function flattenObject(obj: any, prefix = '') { // 处理数组 if (Array.isArray(obj)) { obj.forEach((item, index) => { flattenObject(item, prefix ? `${prefix}.${index}` : `${index}`); }); return; } // 处理对象 if (obj && typeof obj === 'object') { Object.entries(obj).forEach(([key, value]) => { const newPrefix = prefix ? `${prefix}.${key}` : key; if (value && typeof value === 'object') { flattenObject(value, newPrefix); } else { result[newPrefix] = value; } }); return; } // 处理基本类型 result[prefix] = obj; } // 添加 groups 前缀并开始处理 flattenObject(nestedArray, 'groups'); return result; }; const initConditionsValue = [ { ops: '&&', children: [{ key: '', operator: '', value: '' }], }, ]; const opertors = [ { label: '等于', value: '=' }, { label: '不等于', value: '!=' }, { label: '大于', value: '>' }, { label: '大于等于', value: '>=' }, { label: '小于', value: '<' }, { label: '小于等于', value: '<=' }, ]; interface LinkdataSetterProps { defaultValue: string; } const LinkdataSetter = (props: any) => { const { selected, value = {}, field: currentColumn } = props; const field = Field.useField(); const { componentName, id } = selected; const [visible, setVisible] = useState(false); const [mainFormData, setMainFormData] = useState([]); // 存储主表数据 const [selectedField, setSelectedField] = useState({}); // 当前字段 const [relatedField, setRelatedField] = useState({}); // 关联字段 // const [linkdata, setLinkdata] = useState([]); // 关联关系 const [conditionFields, setConditionFields] = useState([]); // 条件配置的可选字段:主表字段 + 当前子表单字段 const { getFieldList } = useGetFields({ selected, componentType: FORMULA_TABLE_TYPE, field: currentColumn, } as any); const onOpen = () => { setVisible(true); }; const onClose = () => { setVisible(false); }; const getSelectFields = () => { // 当前列的数据 const newSelectedField = { dataIndex: currentColumn?.parent?.getPropValue('dataIndex'), label: currentColumn?.parent?.getPropValue('title'), componentName, componentId: id, rs_id: selected.getPropValue('rs_id'), }; setSelectedField(newSelectedField); // 联动字段 const mainFormFields = getFieldList('Form') || []; setMainFormData(mainFormFields); // 条件配置的可选字段:主表字段 + 子表单列 const subFormColsList = (getFieldList() || []).map((item: any) => { return { label: `${item.rs_id || ''}.${item.name || ''}`, value: `${item.rs_id || ''}.${item.fieldId || ''}`, }; }); const mainFormList = mainFormFields.map((item: any) => { return { label: item.name || '', value: item.fieldId || '', }; }); setConditionFields([...mainFormList, ...subFormColsList]); }; useEffect(() => { // eslint-disable-next-line @typescript-eslint/no-shadow const { relatedField = {}, selectedField = {} } = value; setRelatedField(relatedField); setSelectedField(selectedField); }, []); useEffect(() => { if (visible) { getSelectFields(); field.setValues({ groups: value?.relatedField?.conditions || initConditionsValue, ...convertToFlatObject(value?.relatedField?.conditions || initConditionsValue), }); field.validate(); } }, [visible]); // 选择关联字段 // eslint-disable-next-line @typescript-eslint/no-shadow const chooseRelatedField = (value: any, actionType: string) => { if (value && actionType) { let relatedComp: any = {}; mainFormData.forEach((selCom: any) => { if (selCom.id === value) { relatedComp = selCom; } }); const newRelatedField = { value: relatedComp.fieldId, label: relatedComp.name, rs_id: relatedComp.fieldId, componentName: relatedComp.componentName, componentId: relatedComp.id, pageMainFormId: relatedComp.parentId, }; // const newLinkdata = [{ relatedField: newRelatedField, selectedField }]; // setLinkdata(newLinkdata); setRelatedField(newRelatedField); } else { setRelatedField({}); } }; // 点击确定按钮 const confirmLinkdata = (conditions: any) => { const relatedCompId = relatedField.pageMainFormId; const relatedComp = project?.currentDocument?.getNodeById(relatedCompId); // if (relatedComp) { // 更新主表字段关联关系为 linkdata const dupLinkdata: any[] = []; let preLinkdata = []; if (relatedComp) { preLinkdata = relatedComp.getPropValue('linkData') || []; // 先删除之前重复的关联关系 if (preLinkdata.length) { preLinkdata.forEach((item: any) => { if (item?.selectedField?.dataIndex !== selectedField.dataIndex) { dupLinkdata.push(item); } }); } } const newLinkData = dupLinkdata.concat({ relatedField: { ...relatedField, conditions }, selectedField, }); relatedComp && relatedComp.setPropValue('linkData', newLinkData); // 更新主表的条件监听字段 const preLinkConData: Record = {}; newLinkData.forEach((item: any) => { // eslint-disable-next-line @typescript-eslint/no-shadow const { relatedField: curRelatedField = {}, selectedField } = item; const { conditions: curConditions = [] } = curRelatedField; const { dataIndex } = selectedField; const { rs_id = '' } = curRelatedField; const fieldList: any[] = []; curConditions.forEach((group: any) => { const { children } = group; if (!children || !children.length) return true; children.forEach((i: any) => { const { key, value: val } = i; if (!fieldList.includes(key)) { let arr = Array.isArray(preLinkConData[key]) ? preLinkConData[key] : []; arr = arr.filter((con: Record) => { return con?.key !== rs_id; }); arr.push({ key: rs_id, selectedKey: dataIndex, conditions: curConditions }); preLinkConData[key] = arr; } if (val !== '_IS_NULL_' && !fieldList.includes(val)) { let arr = Array.isArray(preLinkConData[val]) ? preLinkConData[val] : []; arr = arr.filter((con: Record) => { return con?.key !== rs_id; }); arr.push({ key: rs_id, selectedKey: dataIndex, conditions: curConditions }); preLinkConData[val] = arr; } }); }); }); if (relatedComp) { relatedComp.setPropValue('linkConData', preLinkConData); } selected?.setPropValue('linkConData', preLinkConData); // } // 更新子表字段关联关系为 linkdata props.onChange({ relatedField: { ...relatedField, conditions }, selectedField, }); setVisible(false); }; const handleSubmit = () => { field.validate((errors: any, values: any) => { if (errors) { // console.log('验证失败:', errors); return; } const { groups, ...rest } = values; const conditions = convertToNestedArray(rest); confirmLinkdata(conditions); }); }; return (
当前表单字段 联动表单字段 {/* */} 的联动显示为 的对应值
); }; LinkdataSetter.defaultProps = { defaultValue: 'linkdata', }; // 导出一个默认的类,继承自Component类,并传入LinkdataSetterProps作为参数 export default class extends Component { // 渲染函数 render() { // 返回LinkdataSetter组件,并传入this.props作为参数 return ; } }