Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #23 from react-d3/custom-tag
Browse files Browse the repository at this point in the history
support customized attibutes and aria-* attributes
  • Loading branch information
Olical committed Oct 31, 2015
2 parents 066e0f4 + 898f1a2 commit 0e89619
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ Element.prototype.attributeNameMappings = {
}

Element.prototype.attributeToPropName = function (name) {
return this.attributeNameMappings[name] || camelCase(name)
if (name.match(/^data-/)) {
return name
} else if (name.match(/^aria-/)) {
return name
} else {
return this.attributeNameMappings[name] || camelCase(name)
}
}

Element.prototype.setAttribute = function (name, value) {
Expand Down
28 changes: 28 additions & 0 deletions test/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ test('pre-built React elements are rendered into the tree', function (t) {
t.equal(tree.props.children[0].props.foo, 'bar')
})

test('React elements customize data-* attributes are rendered into the tree', function (t) {
var el = mk().node()
var sub = mk()
.attr('data-foo', 'bar')
.node()
.toReact()

el.appendChild(sub)
var tree = el.toReact()

t.plan(1)
t.equal(tree.props.children[0].props['data-foo'], 'bar')
})

test('React elements aria-* attributes are rendered into the tree', function (t) {
var el = mk().node()
var sub = mk()
.attr('aria-hidden', 'true')
.node()
.toReact()

el.appendChild(sub)
var tree = el.toReact()

t.plan(1)
t.equal(tree.props.children[0].props['aria-hidden'], 'true')
})

test('toReact does not mutate the state', function (t) {
var el = mk().node()
t.plan(2)
Expand Down

0 comments on commit 0e89619

Please sign in to comment.