我正在使用和Material进行造型设计。我在工具栏上有一个<复选框/>。工具栏的背景色是蓝色(#295ED9)。
当选中复选框时,我设法更改了复选框的颜色。当复选框未选中时,我还将复选框的颜色更改为白色。
问题:当复选框未选中时,I无法更改复选框的背景颜色/填充颜色。它总是和它所在的工具栏相同的颜色(蓝色- #295ED9)。
我尝试将< svg >和< path >的背景颜色、颜色和填充属性更改为白色。
当我将< svg > fill属性更改为white时,我可以得到最好的结果。不幸的是,这不会使复选框在未选中时填充白色背景。
JSX
<div className="hasBalance">
<FormControlLabel
control={<Checkbox color="primary"/>}
label="Has Balance"
/>
</div>SCSS
.hasBalance {
grid-area: balance;
.MuiSvgIcon-root {
fill: white;
}
}铬检测工具中的元素
<div class="hasBalance">
<label class="MuiFormControlLabel-root">
<span class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-143 MuiCheckbox-root MuiCheckbox-colorPrimary MuiIconButton-colorPrimary" aria-disabled="false">
<span class="MuiIconButton-label">
<input class="PrivateSwitchBase-input-146" type="checkbox" data-indeterminate="false" value="">
<svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
<path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"></path>
</svg>
</span>
<span class="MuiTouchRipple-root"></span>
</span>
<span class="MuiTypography-root MuiFormControlLabel-label MuiTypography-body1">Has Balance</span>
</label>
</div>目标是将未选中的资料界面的背景颜色/填充颜色更改为白色。谢谢。
发布于 2019-11-07 21:47:25
下面是一种似乎行之有效的方法。它使用“:后置”伪元素将白色背景放置在SVG后面。
import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "@material-ui/core/styles";
import Checkbox from "@material-ui/core/Checkbox";
const WhiteBackgroundCheckbox = withStyles(theme => ({
root: {
color: "red",
"& .MuiIconButton-label": {
position: "relative",
zIndex: 0
},
"&:not($checked) .MuiIconButton-label:after": {
content: '""',
left: 4,
top: 4,
height: 15,
width: 15,
position: "absolute",
backgroundColor: "white",
zIndex: -1
}
},
checked: {}
}))(Checkbox);
function App() {
return (
<div style={{ backgroundColor: "blue" }}>
<WhiteBackgroundCheckbox />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
发布于 2022-07-28 11:22:26
MUIv5
接受的答案在MUIv5中不起作用。MuiIconButton-label不再存在于复选框中,它改变了组件的结构。
对于那些与v5做斗争的人来说,我的解决方案有点麻烦:
import { Components } from '@mui/material'
const MuiCheckbox: Components['MuiCheckbox'] = {
styleOverrides: {
root: {
'& .MuiSvgIcon-root': {
zIndex: 1,
},
'& .PrivateSwitchBase-input': {
width: 'auto',
height: 'auto',
top: 'auto',
left: 'auto',
opacity: '1',
visibility: 'hidden', // optional
'&::before': {
content: '""',
position: 'absolute',
background: 'white',
height: "100%",
width: "100%",
visibility: "visible" // optional
},
}
},
},
}它依赖于这样一个事实:如果我们重置<input>元素的大小和位置属性,那么它的尺寸和位置就刚好正确。这样,解决方案就能抵抗应用于组件根元素的各种垫片和大小道具。AFAIK不应该破坏任何东西,但我在MUI方面的经验并不长。
请注意,我在主题的配置中将此解决方案定义为全局styleOverride,并且我希望这个自定义背景也会影响选中的滴答,并与Ryan对您的场景的答案进行匹配。
可见性属性是可选的,它们只是确保输入元素保持隐藏(我在其上设置了不透明度1,默认值为0)。如果不想要的话,请确保删除和。
https://stackoverflow.com/questions/58756193
复制相似问题