UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

389 lines (323 loc) 12.5 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import { __decorate } from "tslib"; import React from 'react'; import { action, computed, isArrayLike, observable, runInAction } from 'mobx'; import isString from 'lodash/isString'; import omitBy from 'lodash/omitBy'; import isUndefined from 'lodash/isUndefined'; import { getConfig } from '../../../es/configure'; import Validity from './Validity'; import ValidationResult from './ValidationResult'; import validationRules from './rules'; import valueMissing from './rules/valueMissing'; import getReactNodeText from '../_util/getReactNodeText'; import { getIf } from '../data-set/utils'; var Validator = /*#__PURE__*/ function () { function Validator(field, control) { var _this = this; _classCallCheck(this, Validator); runInAction(function () { _this.field = field; _this.control = control; }); } _createClass(Validator, [{ key: "reset", value: function reset() { this.clearErrors(); var uniqueRefFields = this.uniqueRefFields; if (uniqueRefFields.length) { uniqueRefFields.forEach(function (uniqueRefField) { var validator = uniqueRefField.validator; if (validator) { validator.clearErrors(); } }); } } }, { key: "report", value: function () { var _report = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(ret) { var _this$props, name, dataSet, record, _console, validationMessage, value, reportMessage, dsName, id; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _this$props = this.props, name = _this$props.name, dataSet = _this$props.dataSet, record = _this$props.record; if (!(process.env.NODE_ENV !== 'production' && typeof console !== 'undefined')) { _context.next = 16; break; } validationMessage = ret.validationMessage, value = ret.value; if (!isString(validationMessage)) { _context.next = 7; break; } _context.t0 = validationMessage; _context.next = 10; break; case 7: _context.next = 9; return getReactNodeText(React.createElement("span", null, validationMessage)); case 9: _context.t0 = _context.sent; case 10: _context.t1 = _context.t0; reportMessage = ['validation:', _context.t1]; if (dataSet) { dsName = dataSet.name, id = dataSet.id; if (dsName || id) { reportMessage.push("\n[dataSet<".concat(dsName || id, ">]:"), dataSet); } else { reportMessage.push('\n[dataSet]:', dataSet); } } if (record) { if (dataSet) { reportMessage.push("\n[record<".concat(dataSet.indexOf(record), ">]:"), record); } else { reportMessage.push("\n[record]:", record); } reportMessage.push("\n[field<".concat(name, ">]:"), record.getField(name)); } else { reportMessage.push('[field]:', name); } reportMessage.push('\n[value]:', value); (_console = console).warn.apply(_console, reportMessage); case 16: case "end": return _context.stop(); } } }, _callee, this); })); function report(_x) { return _report.apply(this, arguments); } return report; }() }, { key: "clearErrors", value: function clearErrors() { this.innerValidationResults = undefined; } }, { key: "addError", value: function addError(result) { var innerValidationResults = getIf(this, 'innerValidationResults', []); innerValidationResults.push(result); this.report(result); } }, { key: "execute", value: function () { var _execute = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee2(rules, value) { var _this2 = this; var props, method, results; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: props = this.props; method = rules.shift(); if (!method) { _context2.next = 10; break; } _context2.next = 5; return Promise.all(value.map(function (item) { return method(item, props); })); case 5: results = _context2.sent; results.forEach(function (result) { if (result instanceof ValidationResult) { _this2.addError(result); var index = value.indexOf(result.value); if (index !== -1) { value.splice(index, 1); } } }); if (!value.length) { _context2.next = 10; break; } _context2.next = 10; return this.execute(rules, value); case 10: case "end": return _context2.stop(); } } }, _callee2, this); })); function execute(_x2, _x3) { return _execute.apply(this, arguments); } return execute; }() }, { key: "checkValidity", value: function () { var _checkValidity = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee3() { var value, valueMiss, multiple, _args3 = arguments; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: value = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : null; valueMiss = valueMissing(value, this.props); this.clearErrors(); if (!(valueMiss !== true)) { _context3.next = 7; break; } this.addError(valueMiss); _context3.next = 10; break; case 7: multiple = this.props.multiple; _context3.next = 10; return this.execute(validationRules.slice(), multiple && isArrayLike(value) ? value.slice() : [value]); case 10: return _context3.abrupt("return", this.validity.valid); case 11: case "end": return _context3.stop(); } } }, _callee3, this); })); function checkValidity() { return _checkValidity.apply(this, arguments); } return checkValidity; }() }, { key: "props", get: function get() { var control = this.control, field = this.field; var controlProps = control && omitBy(control.getValidatorProps(), isUndefined); var fieldProps = field && field.getValidatorProps(); return _objectSpread({}, fieldProps, {}, controlProps, { defaultValidationMessages: _objectSpread({}, controlProps && controlProps.defaultValidationMessages, {}, getConfig('defaultValidationMessages'), {}, fieldProps && fieldProps.defaultValidationMessages) }); } }, { key: "uniqueRefFields", get: function get() { var _this$props2 = this.props, name = _this$props2.name, unique = _this$props2.unique, record = _this$props2.record; if (record && isString(unique)) { return _toConsumableArray(record.fields.values()).filter(function (field) { return field.name !== name && field.get('unique') === unique && !field.get('multiple') && !field.get('range'); }); } return []; } // @computed // private get bindingFieldWithValidationResult(): Field | undefined { // const { name, record, type } = this.props; // if (record && name && type === FieldType.object) { // return findBindField(name, record.fields, field => !field.isValid()); // } // return undefined; // } }, { key: "uniqueRefValidationResult", get: function get() { var validationResult; var uniqueRefFields = this.uniqueRefFields, innerValidationResults = this.innerValidationResults; if (uniqueRefFields.length && (!innerValidationResults || innerValidationResults.every(function (result) { return result.ruleName !== 'uniqueError'; }))) { uniqueRefFields.some(function (uniqueRefField) { var validator = uniqueRefField.validator; if (validator) { var validatorInnerValidationResults = validator.innerValidationResults; validationResult = validatorInnerValidationResults && validatorInnerValidationResults.find(function (result) { return result.ruleName === 'uniqueError'; }); } return !!validationResult; }); } return validationResult; } }, { key: "validationResults", get: function get() { var uniqueRefValidationResult = this.uniqueRefValidationResult; if (uniqueRefValidationResult) { return [uniqueRefValidationResult]; } var innerValidationResults = this.innerValidationResults; if (innerValidationResults && innerValidationResults.length) { return innerValidationResults; } // const { bindingFieldWithValidationResult } = this; // if (bindingFieldWithValidationResult) { // return bindingFieldWithValidationResult.getValidationErrorValues(); // } return []; } }, { key: "currentValidationResult", get: function get() { var validationResults = this.validationResults; return validationResults.length ? validationResults[0] : undefined; } }, { key: "validity", get: function get() { var currentValidationResult = this.currentValidationResult; return new Validity(currentValidationResult ? _defineProperty({}, currentValidationResult.ruleName, true) : undefined); } }, { key: "injectionOptions", get: function get() { var currentValidationResult = this.currentValidationResult; return currentValidationResult && currentValidationResult.injectionOptions || {}; } }, { key: "validationMessage", get: function get() { var currentValidationResult = this.currentValidationResult; return currentValidationResult && currentValidationResult.validationMessage; } }]); return Validator; }(); export { Validator as default }; __decorate([observable], Validator.prototype, "field", void 0); __decorate([observable], Validator.prototype, "control", void 0); __decorate([observable], Validator.prototype, "innerValidationResults", void 0); __decorate([computed], Validator.prototype, "props", null); __decorate([computed], Validator.prototype, "uniqueRefValidationResult", null); __decorate([computed], Validator.prototype, "validity", null); __decorate([action], Validator.prototype, "reset", null); __decorate([action], Validator.prototype, "report", null); __decorate([action], Validator.prototype, "clearErrors", null); __decorate([action], Validator.prototype, "addError", null); //# sourceMappingURL=Validator.js.map