UNPKG

@deskpro/react-forms

Version:
345 lines (295 loc) 13.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Value = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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; }; }(); /** * @copyright 2015, Prometheus Research, LLC */ exports.suppressUpdate = suppressUpdate; exports.isValue = isValue; exports.create = create; var _memoizeDecorator = require('memoize-decorator'); var _memoizeDecorator2 = _interopRequireDefault(_memoizeDecorator); var _get = require('lodash/get'); var _get2 = _interopRequireDefault(_get); var _noop = require('lodash/noop'); var _noop2 = _interopRequireDefault(_noop); var _keyPath = require('./keyPath'); var _keyPath2 = _interopRequireDefault(_keyPath); var _update2 = require('./update'); var _Schema = require('./Schema'); var Schema = _interopRequireWildcard(_Schema); var _applyDecorator = require('./applyDecorator'); var _applyDecorator2 = _interopRequireDefault(_applyDecorator); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var suppressUpdateContextual = false; /** * Suppress any onChange notifications during the execution of the callback. */ function suppressUpdate(tx) { suppressUpdateContextual = true; try { return tx(); } finally { suppressUpdateContextual = false; } } function filterErrorListByKeyPath(errorList, keyPath) { var field = ['data'].concat(keyPath).join('.'); return errorList.filter(function (error) { return error.field === field; }); } function filterErrorListByKeyPathPrefix(errorList, keyPath) { if (keyPath.length === 0) { return errorList; } var field = ['data'].concat(keyPath).join('.'); var length = field.length; return errorList.filter(function (error) { return error.field === field || error.field.slice(0, length) === field && error.field[length] === '.'; }); } var Value = exports.Value = function () { function Value() { _classCallCheck(this, Value); } _createClass(Value, [{ key: 'select', value: function select(key) { var keyPath = (0, _keyPath2.default)(key); if (keyPath.length === 0) { return this; } else { return new ValueBranch(this.root, this.keyPath.concat(keyPath)); } } }, { key: 'createRoot', value: function createRoot(update) { var values = { schema: this.root.schema, value: this.root.value, onChange: this.root.onChange, params: this.root.params, errorList: this.root._errorList, externalErrorList: this.root._externalErrorList, validate: this.root.validate }; return new ValueRoot(_extends({}, values, update)); } }, { key: 'updateParams', value: function updateParams(params, suppressUpdate) { params = _extends({}, this.root.params, params); var nextRoot = this.createRoot({ params: params }); if (!suppressUpdate && !suppressUpdateContextual) { this.root.onChange(nextRoot, this.keyPath); } return nextRoot.select(this.keyPath); } }, { key: 'update', value: function update(valueUpdate, suppressUpdate) { var value = void 0; if (this.keyPath.length === 0) { value = valueUpdate; } else { value = (0, _update2.update)(this.root.value, this.keyPath, valueUpdate, this.root.schema); } var errorList = this.root.validate(this.root.schema, value); var nextRoot = this.createRoot({ value: value, errorList: errorList }); if (!suppressUpdate && !suppressUpdateContextual) { this.root.onChange(nextRoot, this.keyPath); } return nextRoot.select(this.keyPath); } }, { key: 'updateError', value: function updateError(error, suppressUpdate) { var field = ['data'].concat(this.keyPath).join('.'); var externalErrorList = void 0; if (Array.isArray(error)) { externalErrorList = error.map(function (error) { return _extends({}, error, { field: field }); }); } else { externalErrorList = [_extends({}, error, { field: field })]; } var nextRoot = this.createRoot({ externalErrorList: externalErrorList }); if (!suppressUpdate && !suppressUpdateContextual) { this.root.onChange(nextRoot, this.keyPath); } return nextRoot.select(this.keyPath); } }, { key: 'addError', value: function addError(error, suppressUpdate) { error = _extends({}, error, { field: ['data'].concat(this.keyPath).join('.') }); var externalErrorList = this.root._externalErrorList.concat(error); var nextRoot = this.createRoot({ externalErrorList: externalErrorList }); if (!suppressUpdate && !suppressUpdateContextual) { this.root.onChange(nextRoot, this.keyPath); } return nextRoot.select(this.keyPath); } }, { key: 'removeError', value: function removeError(error, suppressUpdate) { var idx = this.root._externalErrorList.indexOf(error); if (idx > -1) { var externalErrorList = this.root._externalErrorList.slice(0); externalErrorList.splice(idx, 1); var nextRoot = this.createRoot({ externalErrorList: externalErrorList }); if (!suppressUpdate && !suppressUpdateContextual) { this.root.onChange(nextRoot, this.keyPath); } return nextRoot.select(this.keyPath); } else { return this; } } }, { key: 'errorList', get: function get() { var validateErrorList = filterErrorListByKeyPath(this.root._errorList, this.keyPath); var externalErrorList = filterErrorListByKeyPath(this.root._externalErrorList, this.keyPath); return validateErrorList.concat(externalErrorList); } }, { key: 'completeErrorList', get: function get() { var validateErrorList = filterErrorListByKeyPathPrefix(this.root._errorList, this.keyPath); var externalErrorList = filterErrorListByKeyPathPrefix(this.root._externalErrorList, this.keyPath); return validateErrorList.concat(externalErrorList); } }]); return Value; }(); (0, _applyDecorator2.default)(Value.prototype, 'errorList', _memoizeDecorator2.default); (0, _applyDecorator2.default)(Value.prototype, 'completeErrorList', _memoizeDecorator2.default); var ValueRoot = function (_Value) { _inherits(ValueRoot, _Value); function ValueRoot(_ref) { var schema = _ref.schema, value = _ref.value, onChange = _ref.onChange, params = _ref.params, errorList = _ref.errorList, externalErrorList = _ref.externalErrorList, validate = _ref.validate; _classCallCheck(this, ValueRoot); var _this = _possibleConstructorReturn(this, (ValueRoot.__proto__ || Object.getPrototypeOf(ValueRoot)).call(this)); _this.parent = null; _this.keyPath = []; _this.schema = schema; _this.value = value; _this.onChange = onChange; _this.params = params; _this._errorList = errorList; _this._externalErrorList = externalErrorList; _this.validate = validate; return _this; } _createClass(ValueRoot, [{ key: 'setSchema', /** * Set schema. * * This method performs re-validation. */ value: function setSchema(schema) { var errorList = this.validate(schema, this.value); return this.createRoot({ schema: schema, errorList: errorList }); } }, { key: 'root', get: function get() { return this; } }]); return ValueRoot; }(Value); var ValueBranch = function (_Value2) { _inherits(ValueBranch, _Value2); function ValueBranch(root, keyPath) { _classCallCheck(this, ValueBranch); var _this2 = _possibleConstructorReturn(this, (ValueBranch.__proto__ || Object.getPrototypeOf(ValueBranch)).call(this)); _this2.root = root; _this2.keyPath = keyPath; return _this2; } _createClass(ValueBranch, [{ key: 'params', get: function get() { return this.root.params; } }, { key: 'schema', get: function get() { return Schema.select(this.root.schema, this.keyPath); } }, { key: 'value', get: function get() { return (0, _get2.default)(this.root.value, this.keyPath); } }, { key: 'parent', get: function get() { if (this.keyPath.length === 1) { return this.root; } else { var keyPath = this.keyPath.slice(); keyPath.pop(); return new ValueBranch(this.root, keyPath); } } }]); return ValueBranch; }(Value); (0, _applyDecorator2.default)(ValueBranch.prototype, 'schema', _memoizeDecorator2.default); (0, _applyDecorator2.default)(ValueBranch.prototype, 'value', _memoizeDecorator2.default); /** * Check if value is a form value. */ function isValue(maybeValue) { return maybeValue instanceof Value; } /** * Create a new root value. */ function create() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, schema = _ref2.schema, _ref2$value = _ref2.value, value = _ref2$value === undefined ? {} : _ref2$value, _ref2$onChange = _ref2.onChange, onChange = _ref2$onChange === undefined ? _noop2.default : _ref2$onChange, _ref2$params = _ref2.params, params = _ref2$params === undefined ? {} : _ref2$params, _ref2$errorList = _ref2.errorList, errorList = _ref2$errorList === undefined ? null : _ref2$errorList, _ref2$externalErrorLi = _ref2.externalErrorList, externalErrorList = _ref2$externalErrorLi === undefined ? [] : _ref2$externalErrorLi, _ref2$validate = _ref2.validate, validate = _ref2$validate === undefined ? function (schema, value) { return Schema.validate(schema, value); } : _ref2$validate; if (errorList === null) { errorList = validate(schema, value); } return new ValueRoot({ schema: schema, value: value, onChange: onChange, params: params, errorList: errorList, externalErrorList: externalErrorList, validate: validate }); }