1
1
var reactProperty = require ( 'react-property' ) ;
2
2
var utilities = require ( './utilities' ) ;
3
3
4
+ // https://reactjs.org/docs/uncontrolled-components.html
5
+ // https://developer.mozilla.org/docs/Web/HTML/Attributes
6
+ var UNCONTROLLED_COMPONENT_ATTRIBUTES = [ 'checked' , 'value' ] ;
7
+ var UNCONTROLLED_COMPONENT_NAMES = [ 'input' , 'select' , 'textarea' ] ;
8
+
9
+ var VALUE_ONLY_INPUTS = {
10
+ reset : true ,
11
+ submit : true
12
+ } ;
13
+
4
14
/**
5
15
* Converts HTML/SVG DOM attributes to React props.
6
16
*
@@ -11,18 +21,13 @@ var utilities = require('./utilities');
11
21
module . exports = function attributesToProps ( attributes , nodeName ) {
12
22
attributes = attributes || { } ;
13
23
14
- var valueOnlyInputs = {
15
- reset : true ,
16
- submit : true
17
- } ;
18
-
19
24
var attributeName ;
20
25
var attributeNameLowerCased ;
21
26
var attributeValue ;
22
27
var propName ;
23
28
var propertyInfo ;
24
29
var props = { } ;
25
- var inputIsValueOnly = attributes . type && valueOnlyInputs [ attributes . type ] ;
30
+ var inputIsValueOnly = attributes . type && VALUE_ONLY_INPUTS [ attributes . type ] ;
26
31
27
32
for ( attributeName in attributes ) {
28
33
attributeValue = attributes [ attributeName ] ;
@@ -41,10 +46,9 @@ module.exports = function attributesToProps(attributes, nodeName) {
41
46
propertyInfo = reactProperty . getPropertyInfo ( propName ) ;
42
47
43
48
// convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
44
- // https://reactjs.org/docs/uncontrolled-components.html
45
49
if (
46
- ( propName === 'checked' || propName === 'value' ) &&
47
- nodeName !== 'option' &&
50
+ UNCONTROLLED_COMPONENT_ATTRIBUTES . indexOf ( propName ) !== - 1 &&
51
+ UNCONTROLLED_COMPONENT_NAMES . indexOf ( nodeName ) !== - 1 &&
48
52
! inputIsValueOnly
49
53
) {
50
54
propName = getPropName ( 'default' + attributeNameLowerCased ) ;
0 commit comments