Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TreeView): console warning about uncontrolled inputs when checkbox swaps between determinate/indeterminate #6041

Merged
merged 1 commit into from
Jul 15, 2021

Conversation

mturley
Copy link
Collaborator

@mturley mturley commented Jul 15, 2021

This fixes a bug I overlooked when I implemented #5150 last year.

In order to show an indeterminate (partially-checked) checkbox in the tree view, we allow a null value for the checkProps.checked property on a tree view item. We then use a ref to set the indeterminate property on the DOM element if checkProps.checked === null. This was modeled after similar behavior in the regular Checkbox component.

However, an <input type="checkbox"> like these must always have a boolean (non-null) value for its checked attribute in order for React to recognize it as a controlled component. When this property goes from being a boolean to a null value or vice versa, we get this console warning:

Screen Shot 2021-07-15 at 3 50 46 PM

This can be reproduced in https://codesandbox.io/s/77px2 (taken unmodified from https://www.patternfly.org/v4/components/tree-view/#with-checkboxes) by simply checking the "Application 1" item and observing the console. The "Application launcher" parent checkbox goes from being false (controlled) to null (uncontrolled).

In the regular Checkbox component, this warning is avoided by overriding the checked value we pass to the actual input to be false even if the isChecked prop is null so that even when the checkbox is indeterminate, React sees its value as controlled. I neglected to do that in the tree view version of this feature, so this PR corrects that. I also noticed that the type for checkProps in TreeView was any, which is inconsistent with the matching prop on TreeViewListItem, so I fixed that by exporting the interface (and giving it a TreeView-specific name in case anyone would try to import it elsewhere).

As an aside, the fact that we can't express an indeterminate checkbox with a simple HTML attribute is bananas.

@mturley
Copy link
Collaborator Author

mturley commented Jul 15, 2021

Related issue downstream: kubev2v/forklift-ui#256

@patternfly-build
Copy link
Contributor

patternfly-build commented Jul 15, 2021

Copy link
Contributor

@redallen redallen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tricky

@redallen redallen merged commit 074c8ce into patternfly:main Jul 15, 2021
@patternfly-build
Copy link
Contributor

Your changes have been released in:

  • @patternfly/react-catalog-view-extension@4.12.17
  • @patternfly/react-code-editor@4.3.3
  • @patternfly/react-console@4.10.18
  • @patternfly/react-core@4.136.1
  • @patternfly/react-docs@5.19.21
  • @patternfly/react-inline-edit-extension@4.7.25
  • demo-app-ts@4.110.17
  • @patternfly/react-log-viewer@4.2.17
  • @patternfly/react-table@4.29.18
  • @patternfly/react-topology@4.9.23
  • @patternfly/react-virtualized-extension@4.8.55

Thanks for your contribution! 🎉

Copy link
Member

@dlabrecq dlabrecq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants