1 | import React from 'react';
|
2 |
|
3 |
|
4 | const cache = new Map();
|
5 |
|
6 | export const filterProps = (props = {}, blacklist = []) =>
|
7 | Object.entries(props).reduce((r, [k, v]) => {
|
8 | if (!blacklist.includes(k)) {
|
9 | r[k] = v;
|
10 | }
|
11 | return r;
|
12 | }, {});
|
13 |
|
14 | const fromRenderProp = (elementType, customProps = []) => {
|
15 | const isComposite = typeof elementType === 'function';
|
16 |
|
17 |
|
18 | if (isComposite) {
|
19 | return elementType;
|
20 | }
|
21 |
|
22 |
|
23 | const uniqueCustomProps = Array.from(new Set([...customProps].sort()));
|
24 |
|
25 |
|
26 | const key = `${elementType}//${uniqueCustomProps.join(',')}`;
|
27 |
|
28 |
|
29 |
|
30 | if (!cache.has(key)) {
|
31 |
|
32 |
|
33 | const Component = props =>
|
34 | React.createElement(
|
35 | elementType,
|
36 | filterProps(props, uniqueCustomProps)
|
37 | );
|
38 |
|
39 | Component.displayName = `fromRenderProp(${elementType})`;
|
40 | cache.set(key, Component);
|
41 | }
|
42 |
|
43 | return cache.get(key);
|
44 | };
|
45 |
|
46 | export default fromRenderProp;
|