UNPKG

@lyra/form-builder

Version:
129 lines (102 loc) 3.79 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _find2 = require('lodash/find'); var _find3 = _interopRequireDefault(_find2); var _get2 = require('lodash/get'); var _get3 = _interopRequireDefault(_get2); 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; }; exports.default = withPatchSubscriber; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _shallowEquals = require('shallow-equals'); var _shallowEquals2 = _interopRequireDefault(_shallowEquals); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /*:: import type {Patch} from '../utils/patches'*/ /*:: declare var __DEV__: boolean*/ function isSegmentEqual(segment1, segment2) { const segment1Type = typeof segment1; if (segment1Type !== typeof segment2) { return false; } if (segment1Type === 'object') { return (0, _shallowEquals2.default)(segment1, segment2); } return segment1 === segment2; } function startsWith(subjectPath, checkPath) { if (subjectPath === checkPath) { return true; } if (!Array.isArray(subjectPath) || !Array.isArray(checkPath)) { return false; } if (subjectPath.length < checkPath.length) { return false; } for (let i = 0, len = checkPath.length; i < len; i++) { if (!isSegmentEqual(checkPath[i], subjectPath[i])) { return false; } } return true; } function isAncestor(path1, path2) { return path1.length === 0 || startsWith(path2, path1) && !startsWith(path1, path2); } function shouldReset(path, patches) { return patches.some(patch => isAncestor(patch.path, path) && (patch.type === 'set' || patch.type === 'unset')); } function getValueAtPath(value, path) { return path.reduce((result, segment) => { if (typeof segment === 'object') { return (0, _find3.default)(result, segment); } return (0, _get3.default)(result, segment); }, value); } /*:: type SubscriberArg = { patches: Array<Patch>, shouldReset: boolean, snapshot: any }*/ /*:: type Subscriber = SubscriberArg => void*/ function withPatchSubscriber(ComposedComponent /*: any*/) { var _class, _temp2; return _temp2 = _class = class SubscribePatch extends _react2.default.Component { constructor(...args) { var _temp; return _temp = super(...args), this.subscribe = (subscriber /*: Subscriber*/) => { return this.context.formBuilder.onPatch(({ snapshot, patches }) => { const selfPath = this.context.getValuePath(); const filtered = patches.filter(patch => startsWith(patch.path, selfPath)).map(patch => _extends({}, patch, { path: patch.path.slice(selfPath.length) })); subscriber({ shouldReset: shouldReset(selfPath, patches), snapshot: getValueAtPath(snapshot, selfPath), patches: filtered }); }); }, this.setInput = input => { this._input = input; }, _temp; } focus() { if (this._input && this._input.focus) { this._input.focus(); } } render() { return _react2.default.createElement(ComposedComponent, _extends({ ref: this.setInput }, this.props, { subscribe: this.subscribe })); } }, _class.displayName = `withPatches(${ComposedComponent.displayName || ComposedComponent.name})`, _class.contextTypes = { getValuePath: _propTypes2.default.func, formBuilder: _propTypes2.default.any }, _temp2; }