reactjs - 使用 Material-UI 的标签组件时,如何自定义选中标签的样式?
问题描述
我正在尝试在我的 React 应用程序中使用 Material-ui Tab 组件——https: //material-ui.com/api/tabs/。我已经像这样设置了我的标签栏
<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>
我的问题是,如何在选择选项卡时自定义选项卡的样式?文档列出了“指标”类,所以我使用这些样式
root2: {
width: "100%",
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
root3: {
paddingRight: theme.spacing(1),
flexGrow: 1,
width: "100%",
},
viewButtons: {
marginTop: theme.spacing(2),
marginBottom: theme.spacing(1),
},
indicator: {
border: 0,
borderBottom: "2px solid",
"&:hover": {
border: 0,
borderBottom: "2px solid",
},
}
但是,这个类没有生效。为了正确设置所选选项卡的样式,使用什么合适的类?
解决方案
活动指示器实际上是选项卡组件的一部分,而不是选项卡之一。如果您检查内部,您会看到一个selected
道具(将是.Mui-selected
)。
您应该使用带有 MuiThemeProvider 的 createMuiTheme 来设置样式:
const theme = createMuiTheme({
overrides: {
MuiTab: {
root: {
"&.Mui-selected": {
background: "red"
}
}
}
}
});
这是一个工作示例:https ://codesandbox.io/s/mui-theme-style-selected-tab-pe03k?file=/demo.js
推荐阅读
- python - 防止我的 python 脚本同时执行两次
- macos - 使用 AppleScript Objc 设置扩展属性
- python - cuDF 中的 .data 函数不返回
- google-sheets - 如何使用 Google 表格中另一个单元格公式的值填充 QUERY 函数的“范围”部分?
- c - 如何将随机字符串分配给二维数组
- c++ - 为什么在 unique_ptr 中有一个 release()?有什么用处?
- javascript - Javascript 如何存储方法
- python - 为什么我会收到 ValueError:Series 的真值不明确?
- wordpress - 将 MariaDB 5.5.64 升级到 MariaDB 10 有必要吗?
- linux - 从正则表达式中获取文件列表