UNPKG

react-elegant-ui

Version:

Elegant UI components, made by BEM best practices for react

88 lines 3.68 kB
var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import React from 'react'; import { getPrivatePropsFromComposeUnits, isMatchHOCProps, isMatchProps, isHOCObject, getObjectHash, getPropsFromHOCOptions } from './utils'; import { getDisplayName } from '../getDisplayName'; export function compose() { var wrappers = []; for (var _i = 0; _i < arguments.length; _i++) { wrappers[_i] = arguments[_i]; } return function (Component) { var componentName = getDisplayName(Component); // Reverse array to wrap from last to first and make call stack A,B,C instead C,B,A var WrappedComponent = wrappers.reverse().reduce(function (BaseComponent, wrapper) { var WrappedComponent = wrapper(BaseComponent); // Just wrap if it simple HOC if (!isHOCObject(wrapper)) { return WrappedComponent; } var requirements = wrapper.__hocOptions.matchProps; var matchOnlyProps = wrapper.__hocOptions.matchOnlyProps; // Return simple wrapper if not have requirements to props if (requirements === undefined || Object.keys(requirements).length === 0) { return WrappedComponent; } // Add display name var requirementsHash = getObjectHash(requirements); WrappedComponent.displayName = "compose(".concat(componentName, ")[").concat(requirementsHash, "]"); // Return manager HOC return function (props) { // Check props to match if (isMatchProps(props, requirements)) { var filtredProps_1 = __assign({}, props); // Remove match-only props if (matchOnlyProps !== undefined) { matchOnlyProps.forEach(function (prop) { delete filtredProps_1[prop]; }); } return /*#__PURE__*/React.createElement(WrappedComponent, __assign({}, filtredProps_1)); } else { return /*#__PURE__*/React.createElement(BaseComponent, __assign({}, props)); } }; }, Component); // List a private props of HOCs. All private props of not match HOCs will removed // Use object instead arr for performance var privatePropsDict = getPrivatePropsFromComposeUnits(wrappers); var HOC = function (props) { var ignoredProps = __assign({}, privatePropsDict); // Remove from ignore list a props of matched HOCs wrappers.forEach(function (wrapper) { // Skip simple HOCs if (!isHOCObject(wrapper)) { return; } var options = wrapper.__hocOptions; // Skip HOCs who not match if (!isMatchHOCProps(props, options.matchProps)) { return; } // Remove private props of matched HOCs from ignore list getPropsFromHOCOptions(options).forEach(function (name) { delete ignoredProps[name]; }); }); var localProps = __assign({}, props); // Remove a private props of not matched HOCs // Do it here but not in big wrapper cuz need check all HOCs to match // Otherwise one HOC may unmatch and remove its props, but next HOC // will not match due to depends one of it Object.keys(ignoredProps).forEach(function (name) { delete localProps[name]; }); return /*#__PURE__*/React.createElement(WrappedComponent, __assign({}, localProps)); }; HOC.displayName = "compose(".concat(componentName, ")"); return HOC; }; }