/* * @Author: yangzonglong * @Date: 2021-09-10 17:42:11 * @version: v1.0.0 * @Descripttion: * @LastEditors: yangzonglong * @LastEditTime: 2021-09-14 09:38:27 * @Auditor: */ import { Input, Switch, DatePicker } from 'antd'; import dayjs from 'dayjs'; import Select, { SelectProps } from './Select'; import InputNumber from './InputNumber'; import CheckboxGroup from './CheckboxGroup'; import ModalTableInput from './ModalTableInput'; import { FormItemProps } from './interface'; export const FormItemTypes = { INPUT: 'input', SWITCH: 'switch', SELECT: 'select', NUMBER: 'number', CHECKBOX_GROUP: 'checkboxGroup', DATE_PICKER: 'datePicker', RANGE_PICKER: 'rangePicker', MODAL_TABLE: 'modalTableInput' } export const FormControls: any = { [FormItemTypes.INPUT]: Input, [FormItemTypes.SWITCH]: Switch, [FormItemTypes.SELECT]: Select, [FormItemTypes.NUMBER]: InputNumber, [FormItemTypes.DATE_PICKER]: DatePicker, [FormItemTypes.RANGE_PICKER]: DatePicker.RangePicker, [FormItemTypes.CHECKBOX_GROUP]: CheckboxGroup, [FormItemTypes.MODAL_TABLE]: ModalTableInput } function parse(str: string) { try { return JSON.parse(str) } catch (error) { return {} } } let _cacheOriginalRequired: Record = {}, // 记录原始temp required _cacheOriginalGhost: Record = {}, // 记录原始temp ghost _cacheOriginalQuery: Record = {}; // 记录原始query /** 处理formItems模版字符串,ghost、required */ export function handFormItemsTempStr(formItems: FormItemProps[], allValues: Record) { let newFormItems: FormItemProps[] = []; formItems.forEach((item) => { let name = item.name as string, ghost = _cacheOriginalGhost[name] || item.ghost; if (typeof ghost === 'string') { _cacheOriginalGhost[name] = ghost; ghost = eval(evalTempStr(ghost, allValues, true)) } if (!ghost) { let newItem = { ...item }; let required = _cacheOriginalRequired[name] || newItem.required; if(required) newItem.rules = [{ required: true }]; if (typeof required === 'string' && !newItem.rules) { if (!_cacheOriginalRequired[name]) _cacheOriginalRequired[name] = required; required = eval(evalTempStr(required, allValues, true)); newItem.rules = [{ required: required as boolean }]; newItem.required = required as boolean; } if (newItem.type === FormItemTypes.SELECT) { const query = _cacheOriginalQuery[name] || (newItem?.options as SelectProps | undefined)?.query; if (typeof query === 'string' && query) { if (!_cacheOriginalQuery[name]) _cacheOriginalQuery[name] = query; const evalStr = evalTempStr(query, allValues, true); (newItem.options as SelectProps).query = typeof evalStr === 'string' ? parse(evalStr) : {}; } } newFormItems.push(newItem); } }) return newFormItems; } /** eval字符串,将模版字符替换成具体的值,type为true时会根据值的类型处理成相应的类型 */ export function evalTempStr(tempStr: string | undefined, context: Record, type?: boolean) { if (!tempStr) return ''; return tempStr.replace(new RegExp('\\${(.*?)\\}', 'g'), (item: string) => { const k = item.match(new RegExp('\\${(\\S*)\\}'))?.[1]; const v = context[k!]; if (type && (typeof v === 'string' || v?.format)) return `"${v}"`; return v }) } let _formExtraValues: Record = {}; // 存储组件扩展数据 export const setFormExtraValues = (data: Record) => _formExtraValues = data; export const getFormExtraValues = () => _formExtraValues; export function handInitialValues(formItems: FormItemProps[], values: T): T { const initialValues = { ...values }; formItems.forEach(item => { const name = item.name?.toString(); if(name && name.includes(',') && item.type === FormItemTypes.RANGE_PICKER) { const arr = name.split(','); if(initialValues[arr[0]] && initialValues[arr[1]]) { initialValues[name] = [ dayjs(initialValues[arr[0]]), dayjs(initialValues[arr[1]]) ]; } } }) return initialValues; } export function handFormValues(formItems: FormItemProps[], values: T): T { for (let name in values) { const cur = formItems.find(item => item.name === name); if(cur?.ghost && typeof _cacheOriginalGhost[name] === 'string') { const ghost = eval(evalTempStr(_cacheOriginalGhost[name], values, true)); if(ghost) delete values[name]; } if(name.includes(',')) { const arr = name.split(','); values[arr[0]] = values[name]?.[0]; values[arr[1]] = values[name]?.[1]; delete values[name]; } } return values }