-
-
Save jsonnull/a4dde3ee6b66f840fdce to your computer and use it in GitHub Desktop.
Wrapper for virtual-dom/h to support compiled JSX (ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import h from 'virtual-dom/h' | |
export default function(tag, props, ...children) { | |
return h(tag, transformProps(props), children) | |
} | |
// Add any attribute -> property mapping in here that you may want | |
const attrMap = { | |
"class": "className" | |
} | |
function transformProps(props) { | |
if (props) { | |
Object.getOwnPropertyNames(props).forEach(key => { | |
const sub = attrMap[key] | |
if (sub) { | |
// Apply any attribute to property mappings | |
props[sub] = props[key] | |
delete props[key] | |
} else if (key.startsWith("data-")) { | |
// Move any data- attributes into the dataset property | |
props.dataset = props.dataset || {} | |
props.dataset[key.substr(5)] = props[key] | |
delete props[key] | |
} else if (key.indexOf("-") !== -1) { | |
// Move any other attrs contains a '-' into attributes property | |
props.attributes = props.attributes || {} | |
props.attributes[key] = props[key] | |
delete props[key] | |
} | |
}) | |
} | |
return props | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment