-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathelement.js
51 lines (44 loc) · 1.6 KB
/
element.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
/**
* A function used by Babel to transpile JSX code into jQuery elements
*/
function buildElement(type, props, ...children) {
// We need to require jQuery inside of this method because `require()`
// will work different after mocha's magic "before" method runs.
//
// This allows us to use the jQuery instance provided by our jsdom
// instance.
let $ = require("jquery");
// Is our element a TextNode?
if (props === undefined) {
// Type will be the text content, which can simply be returned here
return type;
// Is our element a Plugin?
} else if (type.render) {
// Render the plugin with the passed-in click handler
return type.render(props && props.onClick);
// Otherwise, build the element with jQuery
} else {
let $el = $("<" + type + ">");
// Iterate through props
if (props !== null) {
for (let propName in props) {
// onClick gets turned into a jQuery event handler
// TODO: Handle props like onHover, onFocus, etc.
if (propName === "onClick") {
let handler = props[propName];
$el.click(handler);
} else {
let value = props[propName];
$el.prop(propName, value);
}
}
}
// Recurse through the children and append each resulting element to
// the parent
children.forEach((child) => {
$el.append(buildElement(child));
});
return $el;
}
}
module.exports = buildElement;