[Checkbox] How to style FormControlLabel based on checked input?
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
I would like to ask you how is it possible to add a class or styles to a FormControlLabel based on the value of the component passed.
<FormControlLabel value={answer.text} control={<Radio color="primary" classes={{ checked: 'test' }}/>} label={answer.text} classes={{ checked: 'test' }}/>)}
Radio gets ātestā class but I want for the FormControlLabel to be able to get this class to style the whole row. So I need to expose the radio button checked property. I could do a new component and add it to the state but isnāt there a more elegant way? Example that I donāt like:
class WrapperLabel extends React.Component{
setControlValue(value){
this.setState({
value
})
}
render(){
return(
<FormControlLabel className={ `${this.state.value === answer.text ? 'test' : ''}`}}/>} value={answer.text} control={<Radio color="primary" className={ checked: 'test' }} onChange={(val) => this.setControlValue(value)}/>} label={answer.text} />)}
)
}
}
Please forgive any code errors I just wrote it without testing it, just wanted to express the idea
Tech | Version |
---|---|
Material-UI | 1.0.0-beta.38 |
React | 16.2.0 |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
Top Results From Across the Web
[Checkbox] How to style FormControlLabel based on checked ...
I would like to ask you how is it possible to add a class or styles to a FormControlLabel based on the value...
Read more >How to Style label on MUI Radio Button when checked
Basically just create a styled form control label and use "useRadioGroup " hook button and choose the colors for checked and uncheckedĀ ...
Read more >Material-UI Checkbox Tutorial and Examples - React School
Using the Material UI Checkbox is essentially the same as native React inputs. You have a checked prop, a boolean set to either...
Read more >FormControlLabel API - Material UI - MUI
API reference docs for the React FormControlLabel component. Learn about the props, CSS, and other APIs of this exported module.
Read more >How to use Material UI Checkbox - Refine Dev
The MUI Checkbox is a striking example of the Input components. ... <FormControlLabel control={<Checkbox defaultChecked />} label="Label" />
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
@Duskfall From my perspective, your example is fine š. However, there is an alternative that doesnāt require to complexify Material-UI core: the CSS direct sibling selector
.checked + .label
.https://codesandbox.io/s/n9623jxyn4?file=/demo.tsx:0-463
It uses the checkbox to apply the red color when checked and then target the label. This a based on a standard CSS selector logic.
@hhimanshu The code converts to the following CSS (with random class names):
This resource can help: https://material-ui.com/customization/overrides/#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet.