decca
Version:
Render interfaces using pure functions and virtual DOM, kinda
45 lines (44 loc) • 1.73 kB
JavaScript
;
var h = require('virtual-dom/h');
var fix_props_1 = require('./fix_props');
var widget_1 = require('./widget');
/*
* A rendering pass.
* This closure is responsible for:
*
* - keeping aware of `context` to be passed down to Components
*
* build = buildPass(...)
* build(el) // render a component/node
*/
function buildPass(context, dispatch) {
/*
* Builds from a vnode (`element()` output) to a virtual hyperscript element.
* The `context` and `dispatch` is passed down recursively.
* https://github.com/Matt-Esch/virtual-dom/blob/master/virtual-hyperscript/README.md
*/
return function build(el) {
if (typeof el === 'string')
return el;
if (typeof el === 'number')
return '' + el;
if (typeof el === 'undefined' || el === null)
return;
if (Array.isArray(el))
return el.map(build);
var tag = el.tag, props = el.props, children = el.children;
if (typeof tag === 'object') {
// Defer to Widget if it's a component
if (!tag.render)
throw new Error('no render() in component');
return new widget_1["default"]({ component: tag, props: props, children: children }, { context: context, dispatch: dispatch }, build);
}
else if (typeof tag === 'function') {
// Pure components
return new widget_1["default"]({ component: { render: tag }, props: props, children: children }, { context: context, dispatch: dispatch }, build);
}
return h(tag, fix_props_1["default"](props), children.map(build));
};
}
exports.__esModule = true;
exports["default"] = buildPass;