-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathattributes-to-props.js
67 lines (54 loc) · 1.85 KB
/
attributes-to-props.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var reactProperty = require('react-property');
var utilities = require('./utilities');
var setStyleProp = utilities.setStyleProp;
var htmlProperties = reactProperty.html;
var svgProperties = reactProperty.svg;
var isCustomAttribute = reactProperty.isCustomAttribute;
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* Converts HTML/SVG DOM attributes to React props.
*
* @param {object} [attributes={}] - HTML/SVG DOM attributes.
* @return {object} - React props.
*/
function attributesToProps(attributes) {
attributes = attributes || {};
var attributeName;
var attributeNameLowerCased;
var attributeValue;
var property;
var props = {};
for (attributeName in attributes) {
attributeValue = attributes[attributeName];
// ARIA (aria-*) or custom data (data-*) attribute
if (isCustomAttribute(attributeName)) {
props[attributeName] = attributeValue;
continue;
}
// convert HTML attribute to React prop
attributeNameLowerCased = attributeName.toLowerCase();
if (hasOwnProperty.call(htmlProperties, attributeNameLowerCased)) {
property = htmlProperties[attributeNameLowerCased];
props[property.propertyName] =
property.hasBooleanValue ||
(property.hasOverloadedBooleanValue && !attributeValue)
? true
: attributeValue;
continue;
}
// convert SVG attribute to React prop
if (hasOwnProperty.call(svgProperties, attributeName)) {
property = svgProperties[attributeName];
props[property.propertyName] = attributeValue;
continue;
}
// preserve custom attribute if React >=16
if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) {
props[attributeName] = attributeValue;
}
}
// transform inline style to object
setStyleProp(attributes.style, props);
return props;
}
module.exports = attributesToProps;