fix(TreeView): console warning about uncontrolled inputs when checkbox swaps between determinate/indeterminate #6041
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thecheckProps.checked
property on a tree view item. We then use a ref to set theindeterminate
property on the DOM element ifcheckProps.checked === null
. This was modeled after similar behavior in the regularCheckbox
component.However, an
<input type="checkbox">
like these must always have a boolean (non-null) value for itschecked
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: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 befalse
even if theisChecked
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 forcheckProps
in TreeView wasany
, 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.