英文:
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}
/>
答案1
得分: 1
通过使用useRadioGroup()钩子,我们可以更好地控制单选按钮组的外观。
受文档中的一个示例的启发,我找到了解决你问题的方法。
实时演示:
代码:
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:
Code:
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>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论