This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
96 lines (77 loc) · 2.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var React = require('react');
var classSet = require('classnames');
var omit = require('just-omit');
var ReactDOM = require('react-dom-factories');
module.exports = r;
// Export the React.DOM html tags
for (var domTag in ReactDOM) {
if (ReactDOM.hasOwnProperty(domTag)) {
r[domTag] = createTagFn(domTag);
}
}
function r(component, properties, children) {
// A properties object is optional so shift arguments if missing
if (!children && isChildren(properties)) {
children = properties;
properties = {};
}
properties = properties || {};
if (properties.hasOwnProperty('isRendered') && !properties.isRendered) {
// React skips the component rendering if render() returns null.
return null;
}
processClasses(properties);
// Don't use an array if there's only one child
if (Array.isArray(children) && children.length === 1) {
children = children[0];
}
var props = omit(properties, ['classSet', 'isRendered']);
// When there's only one child, call createElement normally
// to achieve a minor performance gain
if (!Array.isArray(children)) {
return React.createElement(component, props, children);
}
// When many children, use apply to prevent unnecessary key warnings
var args = createArguments(component, props, children);
return React.createElement.apply(React, args);
}
// Wraps the classSet property value with `classnames` library
// and merge into className.
function processClasses(properties) {
var classSetConfig = properties.classSet;
if (!classSetConfig) {
return;
}
var className = properties.className;
if (className && typeof className === 'string') {
var names = className.match(/\S+/g);
if (!names) {
return;
}
for (var i = 0; i < names.length; i++) {
classSetConfig[names[i]] = true;
}
}
properties.className = classSet(classSetConfig);
}
// Creates an array of React.createElement arguments in a performant way
function createArguments(component, properties, children) {
var argLength = children.length + 2;
var args = new Array(argLength);
args[0] = component;
args[1] = properties;
for (var i = 0; i < children.length; i++) {
var argsIndex = i + 2;
args[argsIndex] = children[i];
}
return args;
}
function createTagFn(tagName) {
return function reactTag(properties, children) {
return r(tagName, properties, children);
};
}
function isChildren(x) {
return typeof x === 'string' || typeof x === 'number' || Array.isArray(x);
}