Repository navigation
Conversation
…ed or reordered ButtonGroup and ToggleButtonGroup wrapped each child in a context provider keyed by its index, so when a child before it was added or removed, a child's wrapper key changed and React remounted it: it lost focus and its state. Key the wrapper by the child's own key, which React.Children.toArray always sets.
Deploy previewBundle size
PerformanceNo regressions in 32 benchmarks · ran 5m 07s · details Check out the code infra dashboard for more information about this PR. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Each child in
ButtonGroupandToggleButtonGroupis wrapped in a context provider withkey={index}:material-ui/packages/mui-material/src/ButtonGroup/ButtonGroup.js
Lines 337 to 346 in e268347
When a child before it comes or goes (like
{showUndo && <Button>Undo</Button>}), the keys of the later children shift, so React unmounts and remounts them. A focused button drops focus tobodyand anything stateful inside it resets.This uses
key={child.key}instead.getValidReactChildrengoes throughReact.Children.toArray, so every child already has a unique key. Position classes are still computed from the index on each render, so first/middle/last update as before.Same one-line change in both components. Duplicate child keys now reach React as duplicate wrapper keys and it warns, which is what
React.Children.mapdid before #38989 switched to the index.I added a test per component,
should keep a button mounted when the button before it is removed. Onmasterboth fail:They pass with the fix. ButtonGroup.test.js 29 passed, ToggleButtonGroup.test.js 37 passed, and both specs pass in the chromium browser project (76 passed).