antd 二级导航如何加选中的背景色
要给 Ant Design 的二级导航添加选中的背景色,可以使用 selectedKeys 属性来指定当前选中的菜单项。具体的步骤如下:
- 首先,在你的组件中引入
Menu和SubMenu组件。
import { Menu } from 'antd';
const { SubMenu } = Menu;
- 在组件的状态中添加一个
current属性来保存当前选中的菜单项的 key。
state = {
current: 'sub1',
};
- 在
Menu组件中使用selectedKeys属性将current的值传递给Menu组件。
<Menu
selectedKeys={[this.state.current]}
mode="horizontal"
onClick={this.handleClick}
>
{/* 在这里添加你的菜单项 */}
</Menu>
- 添加
handleClick方法来更新current的值。
handleClick = e => {
console.log('click ', e);
this.setState({
current: e.key,
});
};
- 在
SubMenu组件中使用className属性来添加选中的背景色。
<SubMenu
key="sub1"
title={
<span className="submenu-title-wrapper">
Navigation One - Submenu
</span>
}
className={this.state.current === 'sub1' ? 'ant-menu-submenu-selected' : ''}
>
{/* 在这里添加你的子菜单项 */}
</SubMenu>
- 最后,在 CSS 文件中添加选中的背景色。
.ant-menu-submenu-selected {
background-color: #1890ff;
}
这样就可以给 Ant Design 的二级导航添加选中的背景色了
原文地址: http://www.cveoy.top/t/topic/i1Y1 著作权归作者所有。请勿转载和采集!