简体   繁体   English

更改未选中 Material-UI 复选框的背景颜色

[英]Change background-color of an unchecked Material-UI checkbox

I am working with React-Redux and Material-UI for styling.我正在使用 React-Redux 和 Material-UI 进行造型。 I have a < Checkbox /> that is on a toolbar.我在工具栏上有一个 < Checkbox />。 The toolbar's background-color is blue (#295ED9).工具栏的背景颜色为蓝色 (#295ED9)。

I've managed to change the color of the checkbox, when it is checked.当它被选中时,我已经设法改变了复选框的颜色。 I have also changed the color of the outline of the checkbox to white, when it is unchecked.当未选中时,我还将复选框轮廓的颜色更改为白色。

Problem: I cannot change the background-color / fill-color of the checkbox when it is unchecked.问题:未选中复选框时,我无法更改复选框的背景颜色/填充颜色。 It is always the same color as the toolbar it is on (blue - #295ED9).它始终与它所在的工具栏颜色相同(蓝色 - #295ED9)。

I have tried changing the background-color, color, and fill attributes of the < svg > and the < path > to white.我尝试将 < svg > 和 < path > 的背景颜色、颜色和填充属性更改为白色。

The best result I can get is when I change the < svg > fill attribute to white.我能得到的最好结果是当我将 < svg > 填充属性更改为白色时。 Unfortunately, this does not make the checkbox fill with a white background when unchecked.不幸的是,这不会使复选框在未选中时填充为白色背景。

JSX JSX

<div className="hasBalance">
  <FormControlLabel
    control={<Checkbox color="primary"/>}
    label="Has Balance"
  />
</div>

SCSS SCSS

.hasBalance {
  grid-area: balance;

  .MuiSvgIcon-root {
    fill: white;
  }
}

Elements in Chrome Inspect Tool Chrome 检查工具中的元素

<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>

The goal is to change the background-color / fill-color of the unchecked Material-UI checkbox to white.目标是将未选中的 Material-UI 复选框的背景颜色/填充颜色更改为白色。 Thank you.谢谢你。

Below is an approach that seems to work.下面是一种似乎有效的方法。 This uses the ":after" pseudo-element to place a white background behind the SVG.这使用 ":after" 伪元素在 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);

编辑复选框背景颜色

Related question: Change the tick color in MuiCheckbox material UI相关问题: 更改 MuiCheckbox 材质 UI 中的刻度颜色

dot is missing in your hasBalance class您的 hasBalance class 中缺少点

.hasBalance {
  grid-area: balance;

  .MuiSvgIcon-root {
    fill: white;
  }
}

MUIv5 MUIv5

Accepted answer doesn't work in MUIv5.接受的答案在 MUIv5 中不起作用。 MuiIconButton-label doesn't exist in Checkbox anymore and it changed the structure of the component. MuiIconButton-label 不再存在于 Checkbox 中,它改变了组件的结构。

For anyone struggling with v5, here's my kinda-hacky solution:对于任何在 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
        },
      }
    },
  },
}

It relies on the fact that the <input> element assumes just the right dimensions and position if we reset its size and positioning attributes.它依赖于<input>元素假设正确的尺寸和 position 的事实,如果我们重置它的大小和定位属性。 That way the solution is resistant to various paddings and size props applied to the root element of the component.这样,该解决方案就可以抵抗应用于组件根元素的各种填充和大小道具。 AFAIK it shouldn't break anything, but my experience with MUI is not that long. AFAIK 它不应该破坏任何东西,但我对 MUI 的体验并不长。

Note that I have this solution defined as a global styleOverride in my theme's config and I want this custom background to affect checked tick too - mix and match with Ryan's answer for your scenario.请注意,我在我的主题配置中将此解决方案定义为全局 styleOverride,并且我希望此自定义背景也能影响选中的刻度 - 与 Ryan 针对您的场景的答案混合搭配。

Visibility attributes are optional, they just make sure the input element stays hidden (I set opacity 1 on it, default is 0).可见性属性是可选的,它们只是确保输入元素保持隐藏(我将不透明度设置为 1,默认为 0)。 Just make sure you remove both if you don't want them.如果您不想要它们,请确保将它们都删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM