gpt4 book ai didi

reactjs - React.js Material-UI 中的 BottomNavigation 样式

转载 作者:行者123 更新时间:2023-12-03 13:42:41 26 4
gpt4 key购买 nike

如何将所选链接(本例中为主页)的图标和文本颜色更改为红色,并将非事件链接(本例中为类(class)和作者)的图标和文本颜色更改为绿色? The docs are very thin.

enter image description here

class MyBottomNavigation extends Component {

render() {
return (
<Paper zDepth={1}>
<BottomNavigation selectedIndex={this.state.selectedIndex}>

<BottomNavigationItem
label="Home"
icon={recentsIcon}
/>

<BottomNavigationItem
label="Course"
icon={favoritesIcon}
/>

<BottomNavigationItem
label="Authors"
icon={nearbyIcon}
/>
</BottomNavigation>
</Paper>
)
}
}

export default MyBottomNavigation

最佳答案

大多数 Material-UI 组件都有三个独立的信息源:

API 文档中的每个组件都记录了您可以通过 classes 属性传入的类,以覆盖组件不同方面的样式。

在本例中,我们关心的组件是 BottomNavigationAction。在 CSS您将找到 API 文档的一部分:

root Styles applied to the root element.

selected Styles applied to the root element if selected.

看到这个你可能首先尝试:

const styles = {
root: {
color: "green"
},
selected: {
color: "red"
}
};

这几乎就成功了。非事件操作为绿色,但所选操作具有红色文本,但图标颜色不受影响。当样式不能完全按照您的预期工作时,下一个要查看的地方是源代码,以了解样式在组件中是如何完成的。

下面是 BottomNavigationAction 样式的简化版本(我只包含与控制这两种颜色相关的部分):

export const styles = theme => ({
/* Styles applied to the root element. */
root: {
color: theme.palette.text.secondary,
'&$selected': {
color: theme.palette.primary.main,
},
},
/* Styles applied to the root element if selected. */
selected: {},
});

如果我们根据其结构来建模我们的覆盖,我们就会取得成功。如果将 withStyles 与 MUI v4 一起使用(下面是 v5 示例),最终结果如下所示:

import React from "react";
import Paper from "@material-ui/core/Paper";
import BottomNavigation from "@material-ui/core/BottomNavigation";
import BottomNavigationAction from "@material-ui/core/BottomNavigationAction";
import RestoreIcon from "@material-ui/icons/Restore";
import FavoriteIcon from "@material-ui/icons/Favorite";
import LocationOnIcon from "@material-ui/icons/LocationOn";
import { withStyles } from "@material-ui/core/styles";

const styles = {
root: {
color: "green",
"&$selected": {
color: "red"
}
},
selected: {}
};

class MyBottomNavigation extends React.Component {
render() {
const actionClasses = this.props.classes;
return (
<Paper>
<BottomNavigation value={1} showLabels={true}>
<BottomNavigationAction
classes={actionClasses}
label="Home"
icon={<RestoreIcon />}
/>

<BottomNavigationAction
classes={actionClasses}
label="Course"
icon={<FavoriteIcon />}
/>

<BottomNavigationAction
classes={actionClasses}
label="Authors"
icon={<LocationOnIcon />}
/>
</BottomNavigation>
</Paper>
);
}
}
export default withStyles(styles)(MyBottomNavigation);

Edit wq02759kk

下面是使用 styled 而不是 withStyles 的 MUI v5 的等效示例:

import React from "react";
import Paper from "@mui/material/Paper";
import BottomNavigation from "@mui/material/BottomNavigation";
import MuiBottomNavigationAction from "@mui/material/BottomNavigationAction";
import RestoreIcon from "@mui/icons-material/Restore";
import FavoriteIcon from "@mui/icons-material/Favorite";
import LocationOnIcon from "@mui/icons-material/LocationOn";
import { styled } from "@mui/material/styles";

const BottomNavigationAction = styled(MuiBottomNavigationAction)(`
color: green;
&.Mui-selected {
color: red;
}
`);

class MyBottomNavigation extends React.Component {
render() {
return (
<Paper>
<BottomNavigation value={1} showLabels={true}>
<BottomNavigationAction label="Home" icon={<RestoreIcon />} />

<BottomNavigationAction label="Course" icon={<FavoriteIcon />} />

<BottomNavigationAction label="Authors" icon={<LocationOnIcon />} />
</BottomNavigation>
</Paper>
);
}
}
export default MyBottomNavigation;

Edit BottomNavigationAction color

以下是 Stack Overflow 中我回答过的有关其他 MUI 组件的一些类似问题的其他资源:

关于reactjs - React.js Material-UI 中的 BottomNavigation 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com