如何覆盖material-ui的标签选择颜色?

时间:2020-04-23 18:27:35

标签: reactjs tabs material-ui

我正在构建一个具有Materialui-tabs主题https://material-ui.com/api/tab/的React 16.13.0应用程序。我已经在组件中创建了这些样式...

const theme = createMuiTheme({
  overrides: {
    MuiTab: {
      root: {
        "&.MuiTab-root": {
          backgroundColor: "black",
          border: 0,
          borderBottom: "2px solid",
          "&:hover": {
            border: 0,
            borderBottom: "2px solid",
          },
        },
        "&.Mui-selected": {
          backgroundColor: "none",
          borderBottom: "2px solid #373985",
          borderColor: "#373985",
        }
      }
    }
  }
});

const useStyles = makeStyles((theme) => ({
  root: {
    width: "100%",
    flexGrow: 1,
    color: "#3739B5",
    backgroundColor: "white",
  },
  viewButtons: {
    marginTop: theme.spacing(2),
    marginBottom: theme.spacing(1),
  },
}));

这些适用于

  <ThemeProvider theme={theme}>
  <AppBar position="static">
    <Tabs
      classes={classes}
      value={value}
      variant="fullWidth"
      centered
      onChange={handleChange}
      aria-label="volunteer dashboard tabs"
    >
      <Tab label={proposedLabel} {...a11yProps(2)} />
      <Tab label={planningLabel} {...a11yProps(1)} />
      <Tab label={inProgressLabel} {...a11yProps(0)} />
      <Tab label={completedLabel} {...a11yProps(3)} />
    </Tabs>
  </AppBar>
  </ThemeProvider>

我正在尝试更改所选标签的背景颜色。根据devtools进行检查,该类被列为

.PrivateTabIndicator-colorSecondary-267 {
        
    background-color: #f50057;
}

.PrivateTabIndicator-root-265 {
            width: 100%;
    
        bottom: 0;
    
        height: 2px;
    
        position: absolute;
    
        transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
}

但是,尽管我已经在主题中列出了该颜色,但是尽管我在样式中指定了颜色,但该颜色还是显示为红色

enter image description here

如何覆盖所选标签的边框颜色?

3 个答案:

答案 0 :(得分:5)

您可以尝试此解决方案为我工作吗?我假设您要覆盖底部边框指示器的颜色。

    <Tabs value={0} TabIndicatorProps={{ style: { background: "#hex-color" } }}>
         <Tab className={clasess.tab} label="Home" />
         <Tab className={clasess.tab} label="Services" />
    </Tabs>

答案 1 :(得分:1)

您现在可以使用TabIndicatorProps使用当前版本的MUI(4.10.02)设置活动指示器的样式。 here可用的文档。

有两种方法可以做到这一点:

方法1:使用样式:{}

import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";

const TabsIndicator = () => {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <React.Fragment>
       <Tabs
         value={value}
         onChange={handleChange}
         TabIndicatorProps={{
           style: { background: "cyan", height: "10px", top: "35px" }
         }}
       >
         <Tab label="TEST1" value={0} />
         <Tab label="TEST2" value={1} />
         <Tab label="TEST3" value={2} />
         <Tab label="TEST4" value={3} />
       </Tabs>
    </React.Fragment>
  );
};

export default TabsIndicator;

方法2:使用className:{classes}

import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";

const useStyles = makeStyles(theme => ({
  indicator: {
    backgroundColor: "green",
    height: "10px",
    top: "45px"
  }
}));

const TabsIndicator = () => {
  const classes = useStyles();

  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <React.Fragment>
        <Tabs
          value={value}
          onChange={handleChange}
          TabIndicatorProps={{ className: classes.indicator }}
        >
          <Tab label="TEST1" value={0} />
          <Tab label="TEST2" value={1} />
          <Tab label="TEST3" value={2} />
          <Tab label="TEST4" value={3} />
        </Tabs>
    </React.Fragment>
  );
};

export default TabsIndicator;

您还可以签出我的sandbox here。希望这会有所帮助!

答案 2 :(得分:1)

尝试一下!

indicator: {
    backgroundColor : 'your favorite color',
},


<Tabs classes={{ indicator: classes.indicator }}>
   <Tab>....
</Tabs>