选中时为FormControlLabel根设置样式

时间:2019-11-29 07:06:02

标签: reactjs radio-button material-ui css-in-js

使用Material-UI主题,如何设置环绕复选框组件的单选按钮标签的样式?我是否只能在makestyles组件上使用FormControlLabel

此图像正是我要实现的(即,深色背景是选中的选项,白色背景是未选中的选项)

enter image description here

import React from "react";
import ThemeProvider from "@material-ui/styles/ThemeProvider";
import { createMuiTheme } from "@material-ui/core/styles";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormControl from "@material-ui/core/FormControl";

const muiTheme = createMuiTheme({});

const theme = {
  ...muiTheme,
  overrides: {
    MuiFormControlLabel: {
      root: {
        marginLeft: 0,
        marginRight: 0,
        backgroundColor: "#F0F3E9",
        color: "#283F3D",
        height: 68,
        marginBottom: "5px",

        // my guess this is wrong syntax
        "&$checked": {
          backgroundColor: "red",
          color: "blue"
        }
      }
    }
  }
};

function CheckboxLabels() {
  return (
    <ThemeProvider theme={theme}>
      <FormControl component="fieldset" fullWidth>
        <RadioGroup aria-label="preferred postal address">
          <FormControlLabel
            value="HomeAddress"
            control={<Radio icon="" checkedIcon="" />}
            label="Home Address"
          />
          <FormControlLabel
            value="WorkAddress"
            control={<Radio icon="" checkedIcon="" />}
            label="Work Address"
          />
        </RadioGroup>
      </FormControl>
    </ThemeProvider>
  );
}

export default CheckboxLabels;

https://codesandbox.io/s/material-demo-lj92b

0 个答案:

没有答案