source: imaps-frontend/node_modules/react-universal-interface/src/render.ts@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[d565449]1import {createElement as h, cloneElement, version} from 'react';
2
3const isReact16Plus = parseInt(version.substr(0, version.indexOf('.'))) > 15;
4const isFn = fn => typeof fn === 'function';
5
6const 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
67export default render;
Note: See TracBrowser for help on using the repository browser.