UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

126 lines (125 loc) 6.3 kB
import React, {Component} from 'react'; import YYForm from './YYForm'; import {isFunction,forIn,forEach} from '../../utils/FunctionUtil'; var formatUtils = require('../../utils/formatUtils'); //匹配出计算公式中的字段 function getFactorFromFormula(mathFormula) { var r = new RegExp("\\{(.+?)\\}", "igm") return mathFormula.match(r); } // 运行计算公式 function mathFormulaResult(mathFormula, rowData, defaultValue) { var formula = '' + mathFormula; forIn(rowData, (value, key) => { formula = formula.replace(new RegExp('{' + escape(key) + '}', 'gm'), value ? escape(value) : 0); }); try { var result = eval(formula); return isNaN(result) ? null : result; } catch (error) { console.error('[公式计算出错]', {formula, mathFormula, rowData, error}); } return defaultValue; } class FormWidget extends Component { _renderChildren = (children) => { var {getFieldProps, setFieldsValue, getFieldsValue} = this.props.form; var _this = this; var colViews = []; //记录下配置的计算公式-------add by wuhao---------- var formulas = [] forEach(children, (child) => { if (child.props.mathFormula) { forEach(getFactorFromFormula(child.props.mathFormula), function (fieldName) { // formulas[fieldName.substring(1, fieldName.length - 1)] = formulaInfo formulas.push({ key: fieldName.substring(1, fieldName.length - 1), formulaInfo: child.props.mathFormula, resultkey: child.props.uikey }) }) } }) //-------add by wuhao----------end forEach(children, function (child, rowIndex) { if (!React.isValidElement(child)) { console.warn('FormWidget无法解析非React对象元素', child); } else { //--------add by wuhao---------- var resultKeySet = new Set() var currentFieldFormulas = _.filter(formulas, (formulaField) => { resultKeySet.add(formulaField.resultkey) //返回计算公式 return formulaField.key == child.props.uikey }) if (currentFieldFormulas && currentFieldFormulas.length) { var _onChange = null if (child.props.onChange) { _onChange = child.props.onChange } //防止重复绑定onChange导致多次执行的问题 if (!child.props.onChangeWithMath) { child.props.onChange = value => { var mathResult = null var fieldsValue = getFieldsValue() var fieldsValurOrigin = _.cloneDeep(fieldsValue)//计算前的表单值 forEach(currentFieldFormulas, (formulas) => { //跟公式中的某一个计算列匹配时进行计算 fieldsValue[formulas.key] = value mathResult = mathFormulaResult(formulas.formulaInfo, fieldsValue, 0) //数字类型默认格式化显示千分位,注意cellText=0的情况 if (typeof mathResult === 'number' || mathResult) { if (child.props.fieldtype == 'currency') {//精确数值 var numStrs = (mathResult + "").split("."); var decimal = numStrs.length == 2 ? numStrs[1].length : 0;//保留原始小数位 if (child.props.decimal >= 0) decimal = child.props.decimal;//保留列定义的小数位 mathResult = formatUtils.formatNumber(mathResult, decimal, child.props.comma === false ? 0 : 3);//格式化千分位 } } fieldsValue[formulas.resultkey] = mathResult setFieldsValue(fieldsValue) }) if (isFunction(_onChange)) { _onChange(value) } //执行结果字段上绑定的 onMathFormula 方法 var resultKeyChildren = _.filter(children, (child) => { return Array.from(resultKeySet).includes(child.props.uikey) }) forEach(resultKeyChildren, (resultKeyChild) => { if (isFunction(resultKeyChild.props.onMathFormula)) { resultKeyChild.props.onMathFormula(fieldsValurOrigin[resultKeyChild.props.uikey], mathResult) } }) } //将onChange保存下 child.props.onChangeWithMath = true; } } //--------add by wuhao----------end colViews.push(React.cloneElement(child, {form: _this.props.form})); } }); return <div>{colViews}</div>; } _renderChildrenNo = (children) => { var _this = this; var colViews = []; forEach(children, function (child, rowIndex) { if (!React.isValidElement(child)) { console.warn('FormWidget无法解析非React对象元素', child); } else { colViews.push(React.cloneElement(child, {form: _this.props.form})); } }); return <div>{colViews}</div>; } render() { var { children } = this.props; return ( <YYForm {...this.props}> {this._renderChildrenNo(children)} </YYForm>); } } module.exports = YYForm.create()(FormWidget);