reactjs - 更改 MuiCheckbox 材质 UI 中的刻度颜色
解决方案
下面是一种似乎有效的方法。该方法的要点是创建一个框(通过:after
伪元素),该框略小于检查的图标,并具有所需的颜色作为背景颜色。然后将该框放在“已选中”图标的后面。
import React from "react";
import { withStyles } from "@material-ui/core/styles";
import FormGroup from "@material-ui/core/FormGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
const CheckboxWithGreenCheck = withStyles({
root: {
"&$checked": {
"& .MuiIconButton-label": {
position: "relative",
zIndex: 0
},
"& .MuiIconButton-label:after": {
content: '""',
left: 4,
top: 4,
height: 15,
width: 15,
position: "absolute",
backgroundColor: "lightgreen",
zIndex: -1
}
}
},
checked: {}
})(Checkbox);
export default function CheckboxLabels() {
const [state, setState] = React.useState({
checkedA: true,
checkedB: false
});
const handleChange = name => event => {
setState({ ...state, [name]: event.target.checked });
};
return (
<FormGroup>
<FormControlLabel
control={
<CheckboxWithGreenCheck
checked={state.checkedA}
onChange={handleChange("checkedA")}
value="checkedA"
color="primary"
/>
}
label="Custom check color"
/>
</FormGroup>
);
}
另一种方法是创建一个自定义图标,其中包含支票所需的颜色,然后通过checkedIcon
属性使用它,如演示中的自定义图标示例中所示。
推荐阅读
- cakephp - 如何在 Cakephp 2 中为 XMLHttpRequest 编写 url
- java - 小部件中的自定义 TextClock
- python - 如何删除列表中的特定值?
- php - 此路由不支持 POST 方法。支持的方法:GET、HEAD。- 拉拉维尔
- typescript - 打字稿中类型的破坏
- amazon-web-services - AWS 蓝绿部署。在部署过程中,来自生产的新数据(数据库)会发生什么?
- python - 使用 tf.gather 在 keras 中自定义正则化
- reactjs - Next.js:为什么在 redux-saga 中使用 (store as sagaStore).sagaTask... 和 toPromise() 和 next-redux-wrapper?
- spring-boot - 创建名为“jmsConnectionFactory”的 bean 时出错 - NullPointerException - Spring Boot Kotlin
- php - 在functions.php中获取相关组的ACF字段值(通过Post Object)