Skip to content

Commit b2ba851

Browse files
Use the React property config when converting attributes to props
- Update `attributeToProps` to use the React property config. - Refactor the helper and keep things functional (do not mutate the original attributes object). - Add tests that check that certain DOM attributes and properties are properly converted to React props.
1 parent e47d055 commit b2ba851

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

lib/attributes-to-props.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
* Module dependencies.
55
*/
66
var utilities = require('./utilities');
7+
var propertyConfig = require('./property-config');
78

89
/**
910
* Make attributes compatible with React props.
1011
*
11-
* @param {Object} attr - The attributes.
12-
* @return {Object} - The props.
12+
* @param {Object} attributes - The attributes.
13+
* @return {Object} - The props.
1314
*/
14-
function attributesToProps(attr) {
15-
attr = attr || {};
15+
function attributesToProps(attributes) {
16+
attributes = attributes || {};
17+
var props = {};
18+
var propertyName;
19+
var reactProperty;
1620

17-
if (attr.style) {
18-
attr.style = cssToJs(attr.style);
21+
// convert DOM attribute/property to React attribute/property
22+
for (propertyName in attributes) {
23+
propertyName = propertyName.toLowerCase();
24+
reactProperty = propertyConfig[propertyName]
25+
if (reactProperty) {
26+
props[reactProperty] = attributes[propertyName];
27+
}
28+
}
29+
30+
// convert inline style to object
31+
if (attributes.style) {
32+
props.style = cssToJs(attributes.style);
1933
}
2034

21-
return attr;
35+
return props;
2236
}
2337

2438
/**

test/attributes-to-props.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,62 @@ var attributesToProps = require('../lib/attributes-to-props');
1111
*/
1212
describe('attributes to props helper', function() {
1313

14+
it('converts DOM attributes to React props', function() {
15+
assert.deepEqual(
16+
attributesToProps({
17+
'class': 'ic',
18+
'for': 'tran',
19+
'http-equiv': 'refresh'
20+
}),
21+
{
22+
className: 'ic',
23+
htmlFor: 'tran',
24+
httpEquiv: 'refresh',
25+
}
26+
);
27+
});
28+
29+
it('converts DOM standard properties to React props', function() {
30+
assert.deepEqual(
31+
attributesToProps({
32+
allowfullscreen: true,
33+
charset: 'utf-8',
34+
tabindex: 1
35+
}),
36+
{
37+
allowFullScreen: true,
38+
charSet: 'utf-8',
39+
tabIndex: 1
40+
}
41+
);
42+
});
43+
44+
it('converts DOM RDFa properties to React props', function() {
45+
assert.deepEqual(
46+
attributesToProps({
47+
property: 'foo',
48+
'typeof': 'bar'
49+
}),
50+
{
51+
property: 'foo',
52+
'typeof': 'bar'
53+
}
54+
);
55+
});
56+
57+
it('converts DOM non-standard properties to React props', function() {
58+
assert.deepEqual(
59+
attributesToProps({
60+
itemscope: true,
61+
itemid: 1337
62+
}),
63+
{
64+
itemScope: true,
65+
itemID: 1337
66+
}
67+
);
68+
});
69+
1470
it('converts CSS style string to JS style object', function() {
1571
// proper css
1672
assert.deepEqual(

0 commit comments

Comments
 (0)