question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
oliviertassinaricommented, Jun 20, 2021

Example that I donā€™t like:

@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.

import * as React from "react";
import { Checkbox, FormControlLabel } from "@material-ui/core";

export default function CheckboxLabels() {
  return (
    <FormControlLabel
      control={
        <Checkbox
          value="checkedA"
          sx={{
            "&.Mui-checked": {
              "&, & + .MuiFormControlLabel-label": {
                color: "red"
              }
            }
          }}
        />
      }
      label="Secondary"
    />
  );
}

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.

Capture dā€™eĢcran 2021-06-20 aĢ€ 19 26 52
5reactions
oliviertassinaricommented, Jun 20, 2021

@hhimanshu The code converts to the following CSS (with random class names):

.checked, .checked + .labelĀ {
  color: blue;
}

This resource can help: https://material-ui.com/customization/overrides/#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found