如何在 Material UI 中设置 FormControlLabel 选中时的背景颜色?

huangapple go评论82阅读模式
英文:

How do I set the background color of a FormControlLabel if it's selected in Material UI?

问题

if I have a RadioGroup and want to make the background color of the checked radio button to another color, how do I do this in Material UI? Can't find anything in the documentation.

如果我有一个 RadioGroup 并想要将选中的单选按钮的背景颜色更改为另一种颜色,在Material UI中应该如何操作?文档中找不到相关信息。

If I try the code below, only the radio button itself gets background color black, not the entire label.

如果我尝试下面的代码,只有单选按钮本身的背景颜色会变成黑色,而不是整个标签。

英文:

if I have a RadioGroup and want to make the background color of the checked radio button to another color, how do I do this in Material UI? Can't find anything in the documentation.

<FormControlLabel
  key={key}
  value={value}
  control={<Radio />}
  label={label}
/>

If I try the code below, only the radio button itself gets background color black, not the entire label.

<FormControlLabel
      key={obj.value}
      value={obj.value}
      control={
        <Radio
          sx={{
            '&.Mui-checked': {
              backgroundColor: 'black',
            },
          }}
        />
      }
      label={obj.label}
    />

如何在 Material UI 中设置 FormControlLabel 选中时的背景颜色?

答案1

得分: 1

通过使用useRadioGroup()钩子,我们可以更好地控制单选按钮组的外观。

受文档中的一个示例的启发,我找到了解决你问题的方法。

实时演示:

如何在 Material UI 中设置 FormControlLabel 选中时的背景颜色?

代码:

import * as React from "react";
import RadioGroup, { useRadioGroup } from "@mui/material/RadioGroup";
import FormControlLabel, {
  FormControlLabelProps
} from "@mui/material/FormControlLabel";
import Radio from "@mui/material/Radio";

function MyFormControlLabel(props: FormControlLabelProps) {
  const radioGroup = useRadioGroup();

  let checked = false;

  if (radioGroup) {
    checked = radioGroup.value === props.value;
  }

  if (checked)
    return (
      <FormControlLabel
        sx={{
          backgroundColor: (theme) => theme.palette.primary.light
        }}
        checked={checked}
        {...props}
      />
    );

  return <FormControlLabel checked={checked} {...props} />;
}

export default function UseRadioGroup() {
  return (
    <RadioGroup name="use-radio-group" defaultValue="first">
      <MyFormControlLabel value="first" label="First" control={<Radio />} />
      <MyFormControlLabel value="second" label="Second" control={<Radio />} />
    </RadioGroup>
  );
}
英文:

Actually, I found some useful information in the docs.

By using the useRadioGroup() hook we get more control over the appearance of the radio group.

Inspired by an example from the documentation, I found this solution to your question.

Live demo:

如何在 Material UI 中设置 FormControlLabel 选中时的背景颜色?

Code:

import * as React from &quot;react&quot;;
import RadioGroup, { useRadioGroup } from &quot;@mui/material/RadioGroup&quot;;
import FormControlLabel, {
  FormControlLabelProps
} from &quot;@mui/material/FormControlLabel&quot;;
import Radio from &quot;@mui/material/Radio&quot;;

function MyFormControlLabel(props: FormControlLabelProps) {
  const radioGroup = useRadioGroup();

  let checked = false;

  if (radioGroup) {
    checked = radioGroup.value === props.value;
  }

  if (checked)
    return (
      &lt;FormControlLabel
        sx={{
          backgroundColor: (theme) =&gt; theme.palette.primary.light
        }}
        checked={checked}
        {...props}
      /&gt;
    );

  return &lt;FormControlLabel checked={checked} {...props} /&gt;;
}

export default function UseRadioGroup() {
  return (
    &lt;RadioGroup name=&quot;use-radio-group&quot; defaultValue=&quot;first&quot;&gt;
      &lt;MyFormControlLabel value=&quot;first&quot; label=&quot;First&quot; control={&lt;Radio /&gt;} /&gt;
      &lt;MyFormControlLabel value=&quot;second&quot; label=&quot;Second&quot; control={&lt;Radio /&gt;} /&gt;
    &lt;/RadioGroup&gt;
  );
}

huangapple
  • 本文由 发表于 2023年3月9日 22:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定