This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
forked from mlmorg/react-hyperscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (46 loc) · 1.83 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
'use strict';
var React = require('react');
var parseTag = require('./parse-tag');
module.exports = h;
function h(componentOrTag, properties, children) {
// if only one argument which is an array, wrap items with React.Fragment
if (arguments.length === 1 && Array.isArray(componentOrTag)) {
return h(React.Fragment, null, componentOrTag);
} else if (!children && isChildren(properties)) {
// If a child array or text node are passed as the second argument, shift them
children = properties;
properties = {};
} else if (arguments.length === 2) {
// If no children were passed, we don't want to pass "undefined"
// and potentially overwrite the `children` prop
children = [];
}
properties = properties ? Object.assign({}, properties) : {};
// Supported nested dataset attributes
if (properties.dataset) {
Object.keys(properties.dataset).forEach(function unnest(attrName) {
var dashedAttr = attrName.replace(/([a-z])([A-Z])/, function dash(match) {
return match[0] + '-' + match[1].toLowerCase();
});
properties['data-' + dashedAttr] = properties.dataset[attrName];
});
properties.dataset = undefined;
}
// Support nested attributes
if (properties.attributes) {
Object.keys(properties.attributes).forEach(function unnest(attrName) {
properties[attrName] = properties.attributes[attrName];
});
properties.attributes = undefined;
}
// When a selector, parse the tag name and fill out the properties object
if (typeof componentOrTag === 'string') {
componentOrTag = parseTag(componentOrTag, properties);
}
// Create the element
var args = [componentOrTag, properties].concat(children);
return React.createElement.apply(React, args);
}
function isChildren(x) {
return typeof x === 'string' || typeof x === 'number' || Array.isArray(x);
}