Skip to content

Issues with ReactControlledValuePropTypes #12477

@lukescott

Description

@lukescott

Do you want to request a feature or report a bug?

See below.

What is the current behavior?

  1. (Bug / Inconsistency)
<input type="radio" checked={false} />

No Warning.

  1. (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'.'

  1. (Bug?)
<select value="foo" readOnly={true}>...</select>

No warning.

What is the expected behavior?

  1. Passing a falsy value or checked attribute will not trigger a warning, but a truthy value does.

  2. 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.

  3. 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:

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:

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
) {

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions