【发布时间】:2017-05-11 00:06:23
【问题描述】:
我在我的项目中使用MUI,我在div 中有一个黑色背景的Checkbox。但它看起来不太好,因为Checkbox 也是黑色的。如何将Checkbox 的颜色从黑色更改为另一种颜色?
【问题讨论】:
标签: reactjs material-ui
我在我的项目中使用MUI,我在div 中有一个黑色背景的Checkbox。但它看起来不太好,因为Checkbox 也是黑色的。如何将Checkbox 的颜色从黑色更改为另一种颜色?
【问题讨论】:
标签: reactjs material-ui
可能是两种方法。
CheckBox 有属性 labelStyle 和 iconStyle。
我想你可以从调整它们开始:
<Checkbox
label="Custom icon"
labelStyle={{color: 'white'}}
iconStyle={{color: 'white'}}
/>
我不确定这是否足够,所以您可能需要与其他“风格”props of Checkbox 一起玩。检查名称中包含“样式”的所有内容。
您可以设置一些只会影响复选框的主题设置:
您可以使用storybook-addon-material-ui 演示page 创建您的主题并下载它。
【讨论】:
您需要使用iconStyle,但是由于复选框图标是SVG图像,您需要使用fill而不是color来设置颜色:
https://jsfiddle.net/27Lmaz48/1/
<Checkbox label='My checkbox'
labelStyle={{color: 'white'}}
iconStyle={{fill: 'white'}}
/>
【讨论】:
对我来说,我只需要更改表头复选框,这对我有用
.thsvg svg{
fill: white !important;
}
<TableHeader
className="thsvg"
displaySelectAll={true}
adjustForCheckbox={true}
enableSelectAll={true}>
<TableRow>
<TableHeaderColumn>Name</TableHeaderColumn>
</TableRow>
</TableHeader>
【讨论】:
这是一个老问题,但对于那些使用材料 1.2 的人来说。
iconStyle 不适合我。
但是,我通过覆盖现有主题并将“复选框”组件包装到一个新主题来实现这一点。
import { withStyles } from '@material-ui/core/styles';
import Checkbox from '@material-ui/core/Checkbox';
const checkBoxStyles = theme => ({
root: {
'&$checked': {
color: '#3D70B2',
},
},
checked: {},
})
const CustomCheckbox = withStyles(checkBoxStyles)(Checkbox);
现在您可以在渲染函数中使用“CustomCheckbox”组件了。
当它被选中时,颜色应该是你指定的颜色。
【讨论】:
checkBox 具有名为 color 的属性,它的值可以是 default 或 primary 或 secondary,例如:
<Checkbox
onChange={doSth}
value="checkedIncomplete"
color="primary"
/>
您可以通过重写其类 Css 以简单的方式更改主要颜色和次要颜色,主要颜色为:.MuiCheckbox-colorPrimary,次要颜色为:.MuiCheckbox-colorSecondary
所以你可以在 Css 中编辑:.MuiCheckbox-colorPrimary { color : 'yourColor'}
【讨论】:
对我来说,这是通过添加根和检查分类来解决的
const styles = () => ({
root: {
"&$checked": {
color: "rgba(0, 0, 0, 0.54)"
}
},
checked: {}
})
并在复选框的类中传递它
<Checkbox
checked={checked}
classes={{
root: classes.root,
checked: classes.checked
}}
onChange={handleCheckBox}
/>
希望这对其他人有所帮助
【讨论】:
这在 material-ui 4.1 上对我有用。
在Checkbox 上定义color 属性,值为="primary"
<Checkbox color="primary"...>
用原色覆盖 material-ui 的样式。将此条目添加到一个 css 文件中,该文件将导入到呈现 Checkbox 的 javascript 代码中。
.MuiCheckbox-colorPrimary.Mui-checked {
color: #e82997 !important;
}
【讨论】:
您可以为选中和未选中条件分配自定义图标,然后设置复选框图标的样式
例如:
<Checkbox checkedIcon={<CustomIcon style={{color:'red'}}/>} icon={<CustomIcon/>} inputProps={{ 'aria-label': 'decorative checkbox' }} {...props} />
【讨论】:
对于稍后来寻求答案的任何人,
如果 labelStyle 和 iconStyle 不适合你
并且您希望以后能够更改颜色尝试以下操作:
const useStyles = makeStyles<Theme, {color: string}, "root">({
root: {
color: (props: {color: string}) => props.color,
},
})
export default function ColoredCheckbox() {
const styles = useStyles({color: "#whatevercoloryouwant"})
return <Checkbox color="default" className={styles.root} />
}
【讨论】:
你可以使用material ui theme。
const theme = createMuiTheme({
overrides: {
MuiCheckbox: {
colorSecondary: {
color: '#custom color',
'&$checked': {
color: '#custom color',
},
},
},
},
});
【讨论】:
colorPrimary。
在 MUI v5 中,有 2 种首选方法可以更改 Checkbox 颜色:
sx道具这对于一次性样式很有用,可以快速设置但仅适用于特定的Checkbox:
import Checkbox, { checkboxClasses } from "@mui/material/Checkbox";
<Checkbox
{...props}
sx={{
[`&, &.${checkboxClasses.checked}`]: {
color: 'magenta',
},
}}
/>
color道具您可以查看this 的答案以获取更多详细信息。基本上某些组件的color 属性(例如Button、Checkbox、Radio、...)必须是来自Palette 对象的颜色之一,可以根据自己的喜好进行扩展:
import { pink, yellow } from "@mui/material/colors";
import Checkbox, { checkboxClasses } from "@mui/material/Checkbox";
import { createTheme, ThemeProvider } from "@mui/material/styles";
const { palette } = createTheme();
const theme = createTheme({
palette: {
pinky: palette.augmentColor({ color: pink }),
summer: palette.augmentColor({ color: yellow })
}
});
<ThemeProvider theme={theme}>
{/* pre-defined color */}
<Checkbox />
<Checkbox color="secondary" />
<Checkbox color="success" />
<Checkbox color="default" />
{/* custom color */}
<Checkbox color="pinky" />
<Checkbox color="summer" />
<Checkbox
</ThemeProvider>
【讨论】: