ToggleButtonGroup does not keep track of selected item, when value is an object
See original GitHub issue- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
When using state that is an object (not a string or number), ToggleButtonGroup does not track the selected item correctly. Perhaps the comparison should be a callback, passable via props?
Expected Behavior 🤔
Values should only be added to the list once, and items should be removed when clicked while selected.
Perhaps the comparison could be determined via a callback passed via props.
Steps to Reproduce 🕹
https://codesandbox.io/s/material-demo-gun9b
This is an example that does not use the exclusive
prop.
- Click the buttons
- Observe in the console that the state list keeps growing with each click.
- Also observe the buttons do not display as selected. (Overriding the ToggleButton
selected
prop to compare objects fixes this, but does not fix the state list growing in size.)
Context 🔦
I am attempting to keep track of state using the ToggleButtonGroup and ToggleButton components that is more complex than just a number or string.
Your Environment 🌎
Tech | Version |
---|---|
Material-UI (lab) | v4.0.0-alpha.48 |
React | 16.13.1 |
Browser | Chrome (latest) |
TypeScript | No |
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
ToggleButtonGroup does not keep track of selected item ...
When using state that is an object (not a string or number), ToggleButtonGroup does not track the selected item correctly.
Read more >ToggleButton group: Ensuring one item is always selected in a ...
I am using a WPF ListBox with ToggleButtons. The problem is the user can click the Left Alignment button twice. The second click...
Read more >ToggleButtonGroup API - Material UI - MUI
API reference docs for the React ToggleButtonGroup component. ... If true , only allow one of the child ToggleButton values to be selected....
Read more >MATLAB uibuttongroup - MathWorks
Create button group to manage radio buttons and toggle buttons ... It does not execute if a radio or toggle button Value property...
Read more >ToggleButton (JavaFX 8) - Oracle Help Center
Indicates whether this toggle button is selected. This can be manipulated programmatically. Parameters: value - true to make this Toggle selected.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found a workaround, but it does not involve letting ToggleButtonGroup manage state. It makes this more like an uncontrolled component I suppose.
Then all state management is done in handleItemClick().
Is this issue fixed? It is marked Closed but it still exists. I’m using mui/material 5.2.6 with React 17.0.2 . In my case, I’m needing to save the button state in the parent when the component is unmounted and supply the initial state when user navigates back to the component. It only works if the object value is JSON stringified (note the JSON.stringify is the value attribute of ToggleButton) . Although this hack works, it is not ideal since the object array is used elsewhere in my app. I recreated in CodeSandbox , https://codesandbox.io/s/dazzling-lake-dnwir?file=/src/ToggleButtons.js