Skip to content

Commit 653beb7

Browse files
Merge pull request #410 from thoughtbot/fix-submit-and-reset-value
fix: make reset and submit input types retain value attribs
2 parents 7d33b2b + b9613f4 commit 653beb7

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/attributes-to-props.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ var utilities = require('./utilities');
1010
module.exports = function attributesToProps(attributes) {
1111
attributes = attributes || {};
1212

13+
var valueOnlyInputs = {
14+
reset: true,
15+
submit: true
16+
};
17+
1318
var attributeName;
1419
var attributeNameLowerCased;
1520
var attributeValue;
1621
var propName;
1722
var propertyInfo;
1823
var props = {};
24+
var inputIsValueOnly = attributes.type && valueOnlyInputs[attributes.type];
1925

2026
for (attributeName in attributes) {
2127
attributeValue = attributes[attributeName];
@@ -35,7 +41,10 @@ module.exports = function attributesToProps(attributes) {
3541

3642
// convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
3743
// https://reactjs.org/docs/uncontrolled-components.html
38-
if (propName === 'checked' || propName === 'value') {
44+
if (
45+
(propName === 'checked' || propName === 'value') &&
46+
!inputIsValueOnly
47+
) {
3948
propName = getPropName('default' + attributeNameLowerCased);
4049
}
4150

test/attributes-to-props.test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ describe('attributesToProps with HTML attribute', () => {
108108
[{ checked: '' }, { defaultChecked: true }],
109109
[{ checked: 'checked' }, { defaultChecked: true }],
110110
[{ value: '' }, { defaultValue: '' }],
111-
[{ value: 'foo' }, { defaultValue: 'foo' }]
111+
[{ value: 'foo' }, { defaultValue: 'foo' }],
112+
[
113+
{ value: 'foo', type: 'submit' },
114+
{ value: 'foo', type: 'submit' }
115+
],
116+
[
117+
{ value: 'foo', type: 'reset' },
118+
{ value: 'foo', type: 'reset' }
119+
]
112120
])(
113121
'converts form attribute to uncontrolled component property',
114122
(attributes, props) => {

0 commit comments

Comments
 (0)