Skip to content

Parsing <input> "checked" and "value" #321

@christianfredh

Description

@christianfredh

Parsing <input type="checkbox" checked /> gives this warning:
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.

Parsning <input type="text" value="initial value" /> gives this warning:
You provided a value prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultValue. Otherwise, set either onChange or readOnly.

We made an attempt to fix in the replace method:

if (node.type === 'tag' && node.name === 'input') {
	  if (node.attribs.type === 'text') {
	    node.attribs.defaultValue = node.attribs.value
	    delete node.attribs['value']
	  }
	  
	  if (node.attribs.type === 'checkbox') {
	    node.attribs.defaultChecked = node.attribs.checked
	    delete node.attribs['checked']
	  }
}

This seems to work, but not sure if it's the best. Maybe it would be good for support for this case in the package? I see there is something similar dom-to-react for textarea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions