1 | import {createElement as h, cloneElement, version} from 'react';
|
---|
2 |
|
---|
3 | const isReact16Plus = parseInt(version.substr(0, version.indexOf('.'))) > 15;
|
---|
4 | const isFn = fn => typeof fn === 'function';
|
---|
5 |
|
---|
6 | const render = (props, data, ...more) => {
|
---|
7 | if (process.env.NODE_ENV !== 'production') {
|
---|
8 | if (typeof props !== 'object') {
|
---|
9 | throw new TypeError('renderChildren(props, data) first argument must be a props object.');
|
---|
10 | }
|
---|
11 |
|
---|
12 | const {children, render} = props;
|
---|
13 |
|
---|
14 | if (isFn(children) && isFn(render)) {
|
---|
15 | console.warn(
|
---|
16 | 'Both "render" and "children" are specified for in a universal interface component. ' +
|
---|
17 | 'Children will be used.'
|
---|
18 | );
|
---|
19 | console.trace();
|
---|
20 | }
|
---|
21 |
|
---|
22 | if (typeof data !== 'object') {
|
---|
23 | console.warn(
|
---|
24 | 'Universal component interface normally expects data to be an object, ' +
|
---|
25 | `"${typeof data}" received.`
|
---|
26 | );
|
---|
27 | console.trace();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | const {render, children = render, component, comp = component} = props;
|
---|
32 |
|
---|
33 | if (isFn(children)) return children(data, ...more);
|
---|
34 |
|
---|
35 | if (comp) {
|
---|
36 | return h(comp, data);
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (children instanceof Array)
|
---|
40 | return isReact16Plus ? children : h('div', null, ...children);
|
---|
41 |
|
---|
42 | if (children && (children instanceof Object)) {
|
---|
43 | if (process.env.NODE_ENV !== 'production') {
|
---|
44 | if (!children.type || ((typeof children.type !== 'string') && (typeof children.type !== 'function') && (typeof children.type !== 'symbol'))) {
|
---|
45 | console.warn(
|
---|
46 | 'Universal component interface received object as children, ' +
|
---|
47 | 'expected React element, but received unexpected React "type".'
|
---|
48 | );
|
---|
49 | console.trace();
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (typeof children.type === 'string')
|
---|
53 | return children;
|
---|
54 |
|
---|
55 | return cloneElement(children, Object.assign({}, children.props, data));
|
---|
56 | } else {
|
---|
57 | if (typeof children.type === 'string')
|
---|
58 | return children;
|
---|
59 |
|
---|
60 | return cloneElement(children, Object.assign({}, children.props, data));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | return children || null;
|
---|
65 | };
|
---|
66 |
|
---|
67 | export default render;
|
---|