import React, { Component, useState, useRef } from 'react'; import { Button, Dialog, Search, Icon } from '@alifd/next'; import { FormFieldType, FormulaType } from '@/constants'; import classNames from 'classnames'; import Editor from './components/editor'; import formulaListAll from './formula-list'; import { useGetFields } from '@/hooks/use-get-fields'; import { FORMULA_DATATYPE_WHITElIST, EditorRefType } from '@/constants'; import { project } from '@alilc/lowcode-engine'; import './index.scss'; import { getFieldsByFormula } from '@/utils/formula'; type FieldItemType = { name: string; type: string; fieldId: string; parentId: string; id?: string; rs_id?: string; }; interface IFormulaSetterValue { selected: any; // 用于获取设计器实例 field: any; // componentType?: 'Form' | 'Table'; // 表单还是表格 value: string; columnFieldConfig: any; isSubForm?: boolean; onChange: (value: string | undefined) => void; } // 支持的组件 const SUPPORTED_COMPONENTS = ['RsSubForm', 'RsEditTable', 'RsLinkedSubFormTable']; // 适用于表单 表格 const FormulaSetter = (props: IFormulaSetterValue) => { const { selected, value, field, onChange } = props; const [visible, setVisible] = useState(false); const [formulaDesc, setFormulaDesc] = useState({}); const [searchValue, setSearchValue] = useState(''); const [formulaList, setFormulaList] = useState(formulaListAll); const { fieldList, getFieldList } = useGetFields({ ...props, dataTypeWhiteList: FORMULA_DATATYPE_WHITElIST, excludeSelf: true, }); const editorRef = useRef(null); // 查找页面根节点 const findPageParent = (component: any): any => { if (!component) return null; // 如果当前组件是 Page,直接返回 if (component.componentName === 'Page' || component.componentName === 'Block') { return component; } // 递归查找 parent if (component.parent) { return findPageParent(component.parent); } return null; }; const handleShowDialog = () => { getFieldList(); setVisible(true); setSearchValue(''); setFormulaList(formulaListAll); }; const handleClose = () => { setVisible(false); }; // 为 node 参数显式指定 any 类型,解决隐式 any 类型的问题 const getComponentName = (node: any) => { return node?.componentName; }; const handleConfirm = () => { // 修改后的获取值代码 const formulaValue = editorRef?.current?.getValue(); // 当前的字段id let currentFieldId = selected.schema.props.rs_id; const pageParent = findPageParent(selected); if (SUPPORTED_COMPONENTS.includes(getComponentName(selected))) { let columns = field?.getProps().getPropValue('columns') || []; const [, currentIndex] = field.path; currentFieldId = columns[currentIndex].dataIndex; } if (value !== formulaValue) { // 删除编辑前的公式的存在,但是在当前保存的公式中被删除的字段 if (value) { const formulaFieldList = getFieldsByFormula(value); formulaFieldList.forEach((item: any) => { let operaNode = pageParent; // 主表单的表单项挂在主表单上 const fieldItemNode = project?.currentDocument?.getNodeById(item.id); if (SUPPORTED_COMPONENTS.includes(getComponentName(fieldItemNode))) { // 表格或者子表单 挂在表格上 operaNode = fieldItemNode; } let originFormulaList = operaNode?.getPropValue('formulaList') || {}; if ( Array.isArray(originFormulaList[item.fieldId]) && originFormulaList[item.fieldId].length ) { const idx = originFormulaList[item.fieldId].findIndex( (t: any) => t.targetField === currentFieldId, ); if (idx !== -1) { originFormulaList[item.fieldId] = originFormulaList[item.fieldId].filter( (t: any) => t.targetField !== currentFieldId, ); operaNode?.setPropValue('formulaList', originFormulaList); } } }); } if (formulaValue) { const formulaFieldList = getFieldsByFormula(formulaValue); formulaFieldList.forEach((item: any) => { debugger; let operaNode = pageParent; // 主表单的表单项挂在主表单上 const fieldItemNode = project?.currentDocument?.getNodeById(item.id); if (SUPPORTED_COMPONENTS.includes(getComponentName(fieldItemNode))) { // 表格或者子表单 挂在表格上 operaNode = fieldItemNode; } let formulaList = operaNode?.getPropValue('formulaList') || {}; if (Array.isArray(formulaList[item.fieldId]) && formulaList[item.fieldId].length) { formulaList[item.fieldId] = formulaList[item.fieldId].filter( (t: any) => t.targetField !== currentFieldId, ); } else { formulaList[item.fieldId] = []; } formulaList[item.fieldId].push({ formula: formulaValue, rs_id: getComponentName(selected) === 'RsSubForm' || getComponentName(selected) === 'RsLinkedSubFormTable' ? selected.schema.props.rs_id : '', // 如果是子表单 记录下子表单的 rs_id targetField: currentFieldId, }); operaNode?.setPropValue('formulaList', formulaList); }); } } onChange && onChange(formulaValue); setVisible(false); }; const handleItemClick = (item: FieldItemType) => { editorRef?.current?.insertText( `${JSON.stringify({ fieldId: item.fieldId, parentId: item.parentId, id: item.id, name: item.name, rs_id: item.rs_id, })}`, ); }; const handleFuncItemClick = (item: any) => { if (item.fnType === FormulaType.OPERA) { editorRef?.current?.insertText(`${item.key}`, item.fnType); } else { editorRef?.current?.insertText(`${item.key}`, item.fnType); } }; const filterFieldList = (list: any[]): any[] => { if (!searchValue) return list; return list.filter((item) => { const name = item.name?.toLowerCase() || ''; const fieldId = item.fieldId?.toLowerCase() || ''; const keywords = searchValue.toLowerCase() || ''; return name.includes(keywords) || fieldId.includes(keywords); }); }; // 过滤函数列表 const filterFnList = (list: any[], keywords: string) => { if (!keywords) { setFormulaList(formulaListAll); return; } let newList = [...list]; newList = list .filter((item) => { const children = item.children.filter((t: any) => { const name = t.label?.toLowerCase() || ''; return name.includes(keywords.toLowerCase() || ''); }); return children.length > 0; }) .map((item) => { const children = item.children.filter((t: any) => { const name = t.label?.toLowerCase() || ''; return name.includes(keywords.toLowerCase() || ''); }); return { ...item, isHide: false, children, }; }); setFormulaList(newList); }; const renderFuncList = () => { return formulaList.map((item: any, idx: number) => { return (
{ const newFormulaList = formulaList.map((listItem, index) => { if (index === idx) { return { ...listItem, isHide: !listItem.isHide, }; } return listItem; }); setFormulaList(newFormulaList); }} > {item.label}
{!item.isHide && (
{item.children.map((t: any) => { return (
setFormulaDesc(t)} onClick={() => handleFuncItemClick(t)} > {t.label} {FormFieldType[t.type]?.text}
); })}
)}
); }); }; return (
{visible && ( 公式编辑
使用数学运算符编辑公式
} visible={visible} width={'80%'} onClose={() => handleClose()} onOk={() => handleConfirm()} onCancel={() => handleClose()} >
{selected.schema.props.label}
当前可选字段 setSearchValue(keywords)} />
{filterFieldList(fieldList).map((item) => { return (
handleItemClick(item)} > {item.name}({item.fieldId}) {FormFieldType[item.type]?.text}
); })}
函数列表 { filterFnList(formulaList, keywords); }} />
{renderFuncList()}
{formulaDesc?.label && (
{formulaDesc?.label}
)}
{formulaDesc?.desc?.desc && (
{formulaDesc?.desc?.desc}
)} {formulaDesc?.desc?.usage && (
用法:{formulaDesc?.desc?.usage}
)} {formulaDesc?.desc?.example && (
示例:{formulaDesc?.desc?.example}
)}
)} ); }; // 因为下面这个问题,setter必须使用class组件 export default class extends Component { static displayName = 'FormulaSetter'; render() { return ; } }