Skip to content

Commit

Permalink
feat(inferno): update to support inferno@0.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed Apr 7, 2016
1 parent 56c9158 commit 3f09303
Show file tree
Hide file tree
Showing 3 changed files with 471 additions and 78 deletions.
101 changes: 97 additions & 4 deletions index.js
Expand Up @@ -3,6 +3,26 @@
var parseTag = require('virtual-dom/virtual-hyperscript/parse-tag');
var Inferno = require('inferno');

var forEach = require('lodash/forEach');
var includes = require('lodash/includes');
var isFunction = require('lodash/isFunction');

var hooks = [
'onCreated',
'onAttached',
'onWillDetach',
'onWillUpdate',
'onDidUpdate'
];
var componentHooks = [
'onComponentWillMount',
'onComponentDidMount',
'onComponentWillUnmount',
'onComponentShouldUpdate',
'onComponentWillUpdate',
'onComponentDidUpdate'
];

module.exports = function (tag, properties, children) {
// If a child array or text node are passed as the second argument, shift them
if (!children && isChildren(properties)) {
Expand All @@ -20,17 +40,90 @@ module.exports = function (tag, properties, children) {
// Create the element
if (process.env.NODE_ENV !== 'production') {
var args = [tag, properties].concat(children);
return Inferno.TemplateFactory.createElement.apply(Inferno.TemplateFactory, args);
return Inferno.createElement.apply(Inferno, args);
}

var extracted = extractFromProps(properties, tag);

return {
dom: null,
tag: tag,
attrs: properties,
children: [].concat(children)
key: extracted.key,
attrs: extracted.props,
events: extracted.events,
hooks: extracted.hooks,
className: extracted.className,
style: extracted.style,
children: children,
instance: null
};
};

module.exports.t = Inferno.createTemplate;
function extractFromProps(props, tag) {
var key = null;
var events = null;
var hooks = null;
var className = null;
var style = null;
var newProps = null;

forEach(props, function (v, k) {
if (k === 'className') {
className = v;
} else if (k === 'style') {
style = v;
} else if (k === 'key') {
key = v;
} else if (isHook(k) && !isFunction(tag)) {
if (!hooks) {
hooks = {};
}

hooks[k.substring(2).toLowerCase()] = v;
} else if (isEvent(k) && !isFunction(tag)) {
if (!events) {
events = {};
}

events[k.substring(2).toLowerCase()] = v;
} else if (isComponentHook(k) && isFunction(tag)) {
if (!hooks) {
hooks = {};
}

hooks['c' + k.substring(3)] = v;
} else if (isFunction(tag)) {
newProps = v;
} else {
if (!newProps) {
newProps = {};
}

newProps[k] = v;
}
});

return {
key: key,
events: events,
hooks: hooks,
className: className,
style: style,
props: newProps
};
}

function isHook(attr) {
return includes(hooks, attr);
}

function isComponentHook(attr) {
return includes(componentHooks, attr);
}

function isEvent(attr) {
return attr[0] === 'o' && attr[1] === 'n' && attr.length > 3;
}

function isChildren(x) {
return typeof x === 'string' || typeof x === 'number' || (x && x.constructor === Array);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -26,14 +26,15 @@
],
"homepage": "https://github.com/terinjokes/inferno-hyperscript#readme",
"dependencies": {
"inferno": "^0.5.22",
"inferno": "^0.6.4",
"lodash": "^4.8.2",
"virtual-dom": "^2.1.1"
},
"devDependencies": {
"assume": "^1.3.1",
"ava": "^0.13.0",
"coveralls": "^2.11.6",
"inferno-server": "^0.5.18",
"inferno-server": "^0.6.4",
"nyc": "^6.0.0",
"semantic-release": "^4.3.5",
"xo": "^0.13.0"
Expand Down

0 comments on commit 3f09303

Please sign in to comment.