Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dataset in VNodes. #39

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var toHTML = init([
modules.attributes,
modules.props,
modules.class,
modules.style
modules.style,
modules.dataset
])

module.exports = toHTML
13 changes: 13 additions & 0 deletions modules/dataset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

var forOwn = require('lodash.forown')
var escape = require('lodash.escape')

// data.dataset

module.exports = function datasetModule (vnode, attributes) {
var dataset = vnode.data.dataset || {}

forOwn(dataset, function (value, key) {
attributes.set(`data-${key}`, escape(value))
})
}
3 changes: 2 additions & 1 deletion modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
class: require('./class'),
props: require('./props'),
attributes: require('./attributes'),
style: require('./style')
style: require('./style'),
dataset: require('./dataset')
}
13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ test('Modules', function (t) {
modules.class,
modules.props,
modules.attributes,
modules.style
modules.style,
modules.dataset
])

// style
Expand Down Expand Up @@ -241,6 +242,16 @@ test('Modules', function (t) {
}, 'Text')
t.equal(renderToString(vnode), '<p class="something else">Text</p>', 'class 4')

// dataset

vnode = h('div', {
dataset: {
foo: 'bar',
answer: 42
}
}, '')
t.equal(renderToString(vnode), '<div data-foo="bar" data-answer="42"></div>', 'dataset 1')

// altogether "randomly"

vnode = h('h1#happy.regular', { props: { title: 'Cheers' } }, 'Happy Birthday')
Expand Down