UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

176 lines (144 loc) 8.72 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _YYForm = require('./YYForm'); var _YYForm2 = _interopRequireDefault(_YYForm); var _FunctionUtil = require('../../utils/FunctionUtil'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 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; (0, _FunctionUtil.forIn)(rowData, function (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: formula, mathFormula: mathFormula, rowData: rowData, error: error }); } return defaultValue; } var FormWidget = function (_Component) { _inherits(FormWidget, _Component); function FormWidget() { var _ref; var _temp, _this2, _ret; _classCallCheck(this, FormWidget); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this2 = _possibleConstructorReturn(this, (_ref = FormWidget.__proto__ || Object.getPrototypeOf(FormWidget)).call.apply(_ref, [this].concat(args))), _this2), _this2._renderChildren = function (children) { var _this2$props$form = _this2.props.form, getFieldProps = _this2$props$form.getFieldProps, setFieldsValue = _this2$props$form.setFieldsValue, getFieldsValue = _this2$props$form.getFieldsValue; var _this = _this2; var colViews = []; var formulas = []; (0, _FunctionUtil.forEach)(children, function (child) { if (child.props.mathFormula) { (0, _FunctionUtil.forEach)(getFactorFromFormula(child.props.mathFormula), function (fieldName) { formulas.push({ key: fieldName.substring(1, fieldName.length - 1), formulaInfo: child.props.mathFormula, resultkey: child.props.uikey }); }); } }); (0, _FunctionUtil.forEach)(children, function (child, rowIndex) { if (!_react2.default.isValidElement(child)) { console.warn('FormWidget无法解析非React对象元素', child); } else { var resultKeySet = new Set(); var currentFieldFormulas = _.filter(formulas, function (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; } if (!child.props.onChangeWithMath) { child.props.onChange = function (value) { var mathResult = null; var fieldsValue = getFieldsValue(); var fieldsValurOrigin = _.cloneDeep(fieldsValue); (0, _FunctionUtil.forEach)(currentFieldFormulas, function (formulas) { fieldsValue[formulas.key] = value; mathResult = mathFormulaResult(formulas.formulaInfo, fieldsValue, 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 ((0, _FunctionUtil.isFunction)(_onChange)) { _onChange(value); } var resultKeyChildren = _.filter(children, function (child) { return Array.from(resultKeySet).includes(child.props.uikey); }); (0, _FunctionUtil.forEach)(resultKeyChildren, function (resultKeyChild) { if ((0, _FunctionUtil.isFunction)(resultKeyChild.props.onMathFormula)) { resultKeyChild.props.onMathFormula(fieldsValurOrigin[resultKeyChild.props.uikey], mathResult); } }); }; child.props.onChangeWithMath = true; } } colViews.push(_react2.default.cloneElement(child, { form: _this.props.form })); } }); return _react2.default.createElement( 'div', null, colViews ); }, _this2._renderChildrenNo = function (children) { var _this = _this2; var colViews = []; (0, _FunctionUtil.forEach)(children, function (child, rowIndex) { if (!_react2.default.isValidElement(child)) { console.warn('FormWidget无法解析非React对象元素', child); } else { colViews.push(_react2.default.cloneElement(child, { form: _this.props.form })); } }); return _react2.default.createElement( 'div', null, colViews ); }, _temp), _possibleConstructorReturn(_this2, _ret); } _createClass(FormWidget, [{ key: 'render', value: function render() { var children = this.props.children; return _react2.default.createElement( _YYForm2.default, this.props, this._renderChildrenNo(children) ); } }]); return FormWidget; }(_react.Component); module.exports = _YYForm2.default.create()(FormWidget);