UNPKG

1.82 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3import React from 'react'; // memoization cache
4
5var cache = new Map();
6export var filterProps = function filterProps() {
7 var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
8 var blacklist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
9 return Object.entries(props).reduce(function (r, _ref) {
10 var _ref2 = _slicedToArray(_ref, 2),
11 k = _ref2[0],
12 v = _ref2[1];
13
14 if (!blacklist.includes(k)) {
15 r[k] = v;
16 }
17
18 return r;
19 }, {});
20};
21
22var fromRenderProp = function fromRenderProp(elementType) {
23 var customProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
24 var isComposite = typeof elementType === 'function'; // if `elementType` is a function, it's already a component
25
26 if (isComposite) {
27 return elementType;
28 } // sort and de-dupe `customProps`
29
30
31 var uniqueCustomProps = Array.from(new Set(_toConsumableArray(customProps).sort())); // hash arguments for memoization
32
33 var key = "".concat(elementType, "//").concat(uniqueCustomProps.join(',')); // only create a new component if not cached
34 // otherwise React will unmount on every render
35
36 if (!cache.has(key)) {
37 // create an SFC that renders a node of type `elementType`
38 // and filter any props that shouldn't be written to the DOM
39 var Component = function Component(props) {
40 return React.createElement(elementType, filterProps(props, uniqueCustomProps));
41 };
42
43 Component.displayName = "fromRenderProp(".concat(elementType, ")");
44 cache.set(key, Component);
45 }
46
47 return cache.get(key);
48};
49
50export default fromRenderProp;
51//# sourceMappingURL=fromRenderProp.js.map
\No newline at end of file