Dav*_*ave 4 tabs reactjs material-ui
我正在构建一个带有 materialui-tabs 主题的 React 16.13.0 应用程序,https: //material-ui.com/api/tab/ 。我在我的组件中创建了这些样式......
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),
},
}));
Run Code Online (Sandbox Code Playgroud)
这些应用于
<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>
Run Code Online (Sandbox Code Playgroud)
我正在尝试更改所选选项卡的背景颜色。基于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;
}
Run Code Online (Sandbox Code Playgroud)
然而,尽管我在我的主题中列出了这一点,但颜色显示为红色,尽管我在我的风格中指定了
如何覆盖所选选项卡的边框颜色?
你能试试这个对我有用的解决方案吗?我假设您想覆盖底部边框指示器颜色。
<Tabs value={0} TabIndicatorProps={{ style: { background: "#hex-color" } }}>
<Tab className={clasess.tab} label="Home" />
<Tab className={clasess.tab} label="Services" />
</Tabs>
Run Code Online (Sandbox Code Playgroud)
从 MUI v5 开始,可以使用createTheme()]API 轻松完成,如下所示。
您需要使用如下内容覆盖样式:
let theme = useTheme();
theme = createTheme(theme, {
components: {
MuiTab: {
styleOverrides: {
root:{
"&.Mui-selected": {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
borderRadius: "25px"
}
}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
该主题可以传递给一个<ThemeProvider />组件,该组件将包装选项卡所在的部分。
笔记:
useTheme(),createTheme()并且<ThemeProvider />所有内容都需要从 导入@mui/material/styles,而不是从 导入@emotion/react。@mui/lab提供了文档<TabContext />推荐使用的、<TabList />和<TabPanel />组件,尽管您仍然可以使用.<Tabs />小智 6
您现在可以使用 TabIndicatorProps 使用当前版本的 MUI (4.10.02) 来设置活动指示器的样式。文档可在此处获取。
有两种方法可以做到这一点:
方法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;
Run Code Online (Sandbox Code Playgroud)
方法2:使用类名:{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;
Run Code Online (Sandbox Code Playgroud)
您还可以在这里查看我的沙箱。希望这可以帮助!