-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (122 loc) · 4.43 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @file 用于提供 react helper 直接用 js 写 UI
* @author Caiyu, Liang, Chengxing, Jun Wu
*/
var React = require('react');
var createElement = React.createElement.bind(React);
var reactHyper;
;(function (global) {
/* global __DEVELOPMENT__ */
/**
*
* @start-def: reactHyper: (tagOrComp, classNameOrProps, props, ...content) => ReactElement
* ///
* This is for matching the first argument of React.createElement
*
* e.g. 'div', 'span' | YouReactComponent
* ///
* tagOrComp: string | ReactClass
*
* // Could be string for a shortcut of props.className or the actual props object
* classNameOrProps: string | {...props}
*
* // if .classNameOrProps is set as string, this should be an object representing the props for createElement
* // otherwise, you can start placing ReactElement here.
* props: {} | ReactElement
*/
reactHyper = function (tagOrComp, classNameOrProps, props /* , ...ReactElements */) {
// for development env, we validate the call to the hyper helper function
if (global.__DEVELOPMENT__ || (typeof __DEVELOPMENT__ !== 'undefined' && __DEVELOPMENT__)) {
if ((classNameOrProps && classNameOrProps.$$typeof)
|| (typeof classNameOrProps === 'string'
&& (props && props.$$typeof || (typeof props !== 'object' && props)))
) {
console.error('[ERROR] helper is not called in a right way. Usage: `h.div(\'class-name\', {props}, ...)` \n' +
'OR `h.div({props}, ...)` \n' +
'OR `h(YourComponent, \'classname\', {props}, ...)`\n' +
'OR `h(YourComponent, {props}, ...) \n' +
'NOTE: the `{props}` is always required', tagOrComp);
}
}
if (typeof classNameOrProps === 'object') {
return createElement.apply(null, arguments);
}
else {
props = props || {};
props.className = props.className || classNameOrProps;
var args;
if (arguments.length > 3) {
args = [tagOrComp, props].concat(Array.prototype.slice.call(arguments, 3));
}
else {
args = [tagOrComp, props];
}
return createElement.apply(null, args);
}
};
/**
* These are shortcuts for commonly used html tags, e.g. `h.div({props}, ...)` equals to `h('div', {props}, ...)`
*
* @def: .tag: (classNameOrProps, props, ...ReactElements) => ReactElement
*/
[
// doc meta
'link', 'meta', 'style', 'title',
// section root
'body',
// content section
'address', 'article', 'aside', 'footer', 'header',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'hgroup',
'nav', 'section',
// text content
'blockquote', 'dd', 'dir', 'dt', 'div', 'dl',
'figcaption', 'figure', 'hr',
'ul', 'ol', 'li',
'p',
'main',
'pre',
// inline text
'span',
'a', 'abbr',
'bdi', 'bdo', 'br',
'cite', 'code', 'data', 'dfn', 'em',
'b', 'i', 's',
'kdb',
'mark', 'q',
'rp', 'rt', 'rtc',
'samp',
'small',
'strong', 'sub', 'time',
'tt', 'u', 'var', 'wbr',
// image and multimedia
'area', 'audio', 'img', 'map', 'track', 'video',
// embedded content
'applet', 'embed', 'iframe', 'noembed', 'object', 'param', 'picture', 'source',
// scripting
'canvas', 'noscript', 'script',
// demarcating edits
'del', 'ins',
// table
'caption', 'col', 'colgroup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr',
// forms
'button', 'datalist', 'fieldset', 'form', 'input', 'label', 'legend', 'meter', 'optgroup',
'option', 'output', 'progress', 'select', 'textarea',
// interactive elements
'details', 'dialog', 'menu', 'menuitem', 'summary',
// web components
'content', 'elements', 'shadow', 'slot', 'template'
].forEach(function (tag) {
reactHyper[tag] = reactHyper.bind(null, tag);
});
})(
typeof window !== 'undefined' ? window :
typeof global !== 'undefined' ? global : {}
);
/**
* Just a way to please some babel loader ;)
*
* @def: .default: reactHyper
*/
reactHyper.default = reactHyper;
module.exports = reactHyper;