-
Notifications
You must be signed in to change notification settings - Fork 50.6k
Description
Do you want to request a feature or report a bug?
See below.
What is the current behavior?
- (Bug / Inconsistency)
<input type="radio" checked={false} />No Warning.
- (Feature Request)
<input type="radio" checked={true} onChange={undefined} />Warning: Failed prop type: You provided a 'checked' prop to a form field without an 'onChange' handler. This will render a read-only field. If the field should be mutable use 'defaultChecked'. Otherwise, set either 'onChange' or 'readOnly'.'
- (Bug?)
<select value="foo" readOnly={true}>...</select>No warning.
What is the expected behavior?
-
Passing a falsy
valueorcheckedattribute will not trigger a warning, but a truthy value does. -
If onChange is passed as undefined (or null?) this should be considered as an acknowledgement and silence the warning. I have a case where I split the render method from the component and use it as a preview. When the component is interactive I use the component. When doing a preview I pass undefined as my change handler. A warning is shown to tell me I "forgot" it, but I intended it to be this way.
-
Going along with 2, I can pass
readOnly={!handleChange}, but according to DefinitelyTyped this isn't a valid attribute for select. I can't use disabled because it changes the appearance of the field.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.2.0
Chrome 65
Other
A proposed "fix" would be to change:
react/packages/react-dom/src/shared/ReactControlledValuePropTypes.js
Lines 27 to 33 in 37e4329
| if ( | |
| !props[propName] || | |
| hasReadOnlyValue[props.type] || | |
| props.onChange || | |
| props.readOnly || | |
| props.disabled | |
| ) { |
to:
if (
!(propName in props) || // Fixes 1
hasReadOnlyValue[props.type] ||
"onChange" in props || // Fixes 2
props.readOnly ||
props.disabled
) { And:
react/packages/react-dom/src/shared/ReactControlledValuePropTypes.js
Lines 44 to 49 in 37e4329
| if ( | |
| !props[propName] || | |
| props.onChange || | |
| props.readOnly || | |
| props.disabled | |
| ) { |
to:
if (
!(propName in props[propName]) || // Fixes 1
"onChange" in props || // Fixes 2
props.readOnly ||
props.disabled
) {