import React, { Component, useEffect, useState, useRef, useMemo, useCallback } from 'react'; import { Checkbox, Grid, Icon, Input, NumberPicker, Overlay, Switch, Button, Select, } from '@alifd/next'; import { IGetField, ValidationField } from '@/types'; // meta 中引入方式 // import { FuncEditorSetter } from '..'; import FuncEditorSetter from '../funceditor-setter'; import { useGetFields } from '@/hooks/use-get-fields'; import EditSvg from './editsvg'; import { ValidationType, REQUIRED_FUNCTION_TEMPLATE } from '@/constants'; import { isEmpty } from '@/utils/util'; import './index.less'; const { Popup } = Overlay; const { Row, Col } = Grid; interface ValidationSetterProps { type: ValidationType; value: ValidationInfoProps; onChange: (info: ValidationInfoProps) => void; selected?: any; field?: any; } /** * 返回值类型定义 */ interface ValidationInfoProps extends ValidationField { type: ValidationType; } const CUSTOM_FUNCTION_TEMPLATE = ` / * * value: 当前单元格值 * record: 当前行数据 * return boolean * 注意: true 表示校验通过,false 表示校验不通过 * 注意:参数不可删除和改动顺序 * / function validate(value, record) { return true; } `.trim(); const ValidationSetter = (props: ValidationSetterProps) => { const { type = ValidationType.REQUIRED, value: selectedValue, onChange, selected, field } = props; const componentName = selected?.componentName || ''; const isTableComponent = componentName.includes('Table') || componentName.includes('SubForm') || componentName.includes('QueryFilter'); const columnFields = useRef([]); if (isTableComponent) { const { extraProps } = field; const { currentField } = extraProps; const config = componentName.includes('QueryFilter') ? selected?.propsData?.items || [] : selected?.propsData?.columns || []; columnFields.current = config .filter((item: any) => item.dataIndex !== currentField) // 过滤当前字段 .map((item: any) => { return { label: item.title + (item.title ? `(${item.dataIndex || ''})` : item.dataIndex), value: item.dataIndex, disabled: item.dataIndex === currentField, }; }); } const { fieldList, getFieldList } = useGetFields({ componentType: undefined, ...props, } as IGetField); useEffect(() => { getFieldList(); }, [selected]); const formateOptions = (options: any[]) => { const { _id } = selected; return options .filter((item) => item.id !== _id) // 过滤当前字段 .map((item: any) => { return { label: item.name + (item.name ? `(${item.fieldId || ''})` : item.fieldId), value: item.fieldId, }; }); }; const { leftSpan, rightSpan, defaultValue, popupClassName } = useMemo(() => { const isCustomFunction = type === ValidationType.CUSTOM_FUNCTION; // 简化的弹窗类名映射 const popupClassMap = { customTable: 'rs-validation-tablefunc-popup', customOther: 'rs-validation-otherfunc-popup', normalTable: 'rs-validation-table-popup', normalOther: 'rs-validation-popup', }; const classKey = isCustomFunction || type === ValidationType.REQUIRED ? isTableComponent ? 'customTable' : 'customOther' : isTableComponent ? 'normalTable' : 'normalOther'; // 计算默认值,但不会覆盖已有的值 const getDefaultTemplate = () => { if (selectedValue?.value) return selectedValue.value; if (isCustomFunction) return CUSTOM_FUNCTION_TEMPLATE; if (type === ValidationType.REQUIRED) return REQUIRED_FUNCTION_TEMPLATE; return ''; }; return { leftSpan: isCustomFunction || type === ValidationType.REQUIRED ? 4 : 6, rightSpan: isCustomFunction || type === ValidationType.REQUIRED ? 20 : 18, defaultValue: getDefaultTemplate(), popupClassName: popupClassMap[classKey], }; }, [type, componentName, selectedValue]); const [info, setInfo] = useState({ type: ValidationType.REQUIRED, enabled: false, message: '请输入内容', value: defaultValue, }); const [show, setShow] = useState(false); // 记录是否是首次加载,首次加载才设置默认值 const [isInitialized, setIsInitialized] = useState(true); const dependencies = useRef([]); const message = useRef('请输入内容'); const timer = useRef(); // 在 useEffect 中添加初始化逻辑 useEffect(() => { if (!isInitialized) return; // 初始化 ref 值 dependencies.current = selectedValue?.dependencies || []; message.current = selectedValue?.message || '请输入内容'; setInfo({ type: selectedValue?.type || ValidationType.REQUIRED, enabled: selectedValue?.enabled || false, message: message.current, value: !isEmpty(selectedValue?.value) ? selectedValue?.value : type === ValidationType.REQUIRED ? REQUIRED_FUNCTION_TEMPLATE : defaultValue, }); }, [selectedValue]); const handleValueChange = useCallback( (field: string, value: number | string | boolean) => { const newInfo = { ...info, [field]: value }; // 统一处理特殊字段的ref更新 switch (field) { case 'dependencies': dependencies.current = Array.isArray(value) ? value : []; newInfo.dependencies = dependencies.current; break; case 'message': message.current = value as string; newInfo.message = message.current; break; default: // 确保dependencies和message始终同步 newInfo.dependencies = dependencies.current; newInfo.message = message.current; break; } setIsInitialized(false); setInfo(newInfo); // 清除之前的定时器并设置新的防抖 if (timer.current) { clearTimeout(timer.current); } timer.current = setTimeout(() => { onChange(newInfo); }, 350); }, [info, onChange], ); return (
setShow(!show)}> {EditSvg}
} triggerType="click" onVisibleChange={(visible: boolean, type: string | object) => { if (type === 'docClick') { setShow(visible); } }} >
setShow(false)} /> 启用 handleValueChange('enabled', value)} /> {type === ValidationType.MIN_VALUE && ( 最小值 handleValueChange('value', Number(value))} /> )} {type === ValidationType.MAX_VALUE && ( 最大值 handleValueChange('value', Number(value))} /> )} {type === ValidationType.MIN_LENGTH && ( 最小长度 handleValueChange('value', Number(value))} /> )} {type === ValidationType.MAX_LENGTH && ( 最大长度 handleValueChange('value', Number(value))} /> )} {type === ValidationType.REGEX && ( 正则 handleValueChange('value', value)} /> )} {type === ValidationType.REQUIRED && ( <> 依赖字段 } triggerType="hover" >
条件必输时必填,依赖字段的值发生变化时重新触发必输校验
{ handleValueChange('dependencies', value || []); }} style={{ width: '100%' }} hasClear placeholder={'请选择'} notFoundContent="暂无可用字段" popupContainer={(target) => target?.parentNode as HTMLElement} onVisibleChange={(visible) => { if (visible) { getFieldList(); } }} className="rs-field-validation-select-setter" />
自定义函数 } triggerType="hover" >
自定义校验方法必须返回 boolean 型结果
handleValueChange('value', value)} /> )} {(type === ValidationType.CUSTOM_FUNCTION || type === ValidationType.REQUIRED) && ( 错误提示 handleValueChange('message', value)} /> )}
handleValueChange('enabled', value)} /> ); }; export default class extends Component { render() { return ; } }