react style: 二级菜单
1、样式
- @import "../../styles/varibles";
- .app-sidebar {
- overflow: hidden;
- width: 180px;
- > ul > li {
- position: relative;
- font-size: $font-lg;
- border-bottom: $border;
- border-right: $border;
- border-left: $border;
- &:first-child {
- border-top: $border;
- border-top-right-radius: $border-radius;
- border-top-left-radius: $border-radius;
- }
- &:last-child {
- border-bottom-right-radius: $border-radius;
- border-bottom-left-radius: $border-radius;
- }
- }
- .active {
- border-left: 3px solid $primary-color;
- background-color: $item-active-bg-color;
- a {
- font-weight: bold;
- }
- }
- .nav-item {
- .item-name {
- margin-right: 30px;
- height: 50px;
- line-height: 50px;
- }
- .anticon {
- position: absolute;
- height: 50px;
- line-height: 50px;
- left: 7px;
- font-size: $font-sm;
- color: $title-color;
- }
- }
- &.is-open {
- .anticon {
- color: $primary-color;
- }
- .nav-item-content {
- color: $title-color;
- font-weight: bold;
- }
- }
- &:hover {
- .anticon,
- .nav-item-content {
- color: $primary-color;
- }
- }
- &:active {
- .nav-item-content {
- color: $primary-color;
- font-weight: bold;
- }
- }
- .sub-menu {
- border-top: none;
- font-size: $font-sm;
- .item-name {
- height: 40px;
- line-height: 40px;
- }
- .child-nav-item.active {
- .item-name {
- color: $primary-color;
- font-weight: bold;
- }
- }
- }
- }
2、js文件
- import React from "react";
- import {withRouter} from "react-router-dom";
- import {Icon} from "antd";
- import _ from "lodash";
- const menus = [];
- const currentState = '';
- class Sidebar extends React.Component {
- componentDidMount() {
- const defaultNavItem = this.getDefaultNavItem();
- if (defaultNavItem === undefined) {
- this.props.history.replace('/forbidden');
- return;
- }
- this.setActiveNavItem(defaultNavItem);
- this.openNavItem(defaultNavItem);
- if (this.hasChildItems(defaultNavItem)) {
- this.setActiveChildNavItem(defaultNavItem.childItems);
- }
- }
- getDefaultNavItem() {
- const currentState = currentState;
- return _.find(menus, function (navItem) {
- if (navItem.state === currentState || _.some(navItem.childItems, {state: currentState})) {
- return navItem;
- }
- })
- }
- setActiveNavItem(navItem) {
- if (this.hasChildItems(navItem)) {
- this.clearParentActiveStatus();
- }else {
- this.clearActiveStatusWithChildItems();
- navItem.isActive = true;
- if (!!navItem.state) {
- this.props.history.replace(navItem.state);
- }
- }
- }
- setActiveChildNavItem(childNavItems) {
- const currentState = currentState;
- this.clearActiveStatusWithChildItems();
- if (_.isArray(childNavItems)) {
- childNavItems.forEach(function (navItem) {
- navItem.isActive = navItem.state === currentState;
- });
- }else {
- childNavItems.isActive = true;
- }
- }
- openNavItem(navItem) {
- navItem.isOpen = this.hasChildItems(navItem);
- this.forceUpdate();
- }
- onOpenNavItem(navItem) {
- if (this.hasChildItems(navItem)) {
- navItem.isOpen = !navItem.isOpen;
- }else {
- navItem.isOpen = false;
- }
- this.forceUpdate();
- }
- clearParentActiveStatus() {
- menus.forEach(function (navItem) {
- navItem.isActive = false;
- })
- }
- clearActiveStatusWithChildItems() {
- menus.forEach(function (navItem) {
- navItem.isActive = false;
- navItem.childItems.forEach(function (childItem) {
- childItem.isActive = false;
- })
- })
- }
- hasChildItems(navItem) {
- return !!navItem.childItems && navItem.childItems.length > 0;
- }
- menuIcon(navItem) {
- return <Icon type={navItem.isOpen ? 'caret-down' : 'caret-right'}/>
- }
- openOrActiveClass(navItem) {
- const basic = "nav-item";
- const openClass = navItem.isOpen ? "is-open" : "";
- const activeClass = navItem.isActive ? "active" : "";
- return basic + " " + openClass + " " + activeClass;
- }
- activeClass(navItem) {
- const basic = "child-nav-item";
- const activeClass = navItem.isActive ? "active" : "";
- return basic + " " + activeClass;
- }
- render() {
- return (
- <aside className="app-sidebar">
- <ul className="list-unstyled menu">
- {
- menus.map((navItem, index) => {
- return (
- <li key={'li_'+index} className={this.openOrActiveClass(navItem)}>
- <span key={'span' + index}
- className="item-name nav-item-content"
- onClick={() => {
- this.setActiveNavItem(navItem);
- this.onOpenNavItem(navItem)
- }}>
- {this.hasChildItems(navItem) ? this.menuIcon(navItem) : null}
- {navItem.name}
- </span>
- {
- navItem.isOpen ?
- <ul key={'subMenu_ul'} className="list-unstyled sub-menus">
- {
- navItem.childItems.map((childItem, itemIndex) => {
- return (
- <li key={'submenu_li_' + itemIndex}
- className={this.activeClass(childItem)}
- onClick={() => {
- this.setActiveChildNavItem(childItem);
- this.setActiveNavItem(childItem)
- }}>
- <a className="item-name">{childItem.name}</a>
- </li>
- )
- })
- }
- </ul> : null
- }
- </li>
- )
- })
- }
- </ul>
- </aside>
- )
- }
- }
- export default withRouter(Sidebar);
3、数据
- [
- {
- "description": "userCanGetMenus",
- "request": {
- "method": "GET",
- "uri": "/api/menus"
- },
- "response": {
- "status": 200,
- "json": {
- "id": "DEMO0000",
- "fatherId": "00000000",
- "state": "process",
- "name": "运营流程",
- "childItems": [
- {
- "id": "DEMO1000",
- "fatherId": "DEMO0000",
- "state": "/process.personal-task-pool",
- "name": "个人任务池",
- "childItems": []
- },
- {
- "id": "DEMO2000",
- "fatherId": "DEMO0000",
- "state": "/process.common-task-pool",
- "name": "公共任务池",
- "childItems": []
- },
- {
- "id": "DEMO1000",
- "fatherId": "DEMO0000",
- "state": "/process.launch",
- "name": "流程发起",
- "childItems": []
- },
- {
- "id": "DEMO1000",
- "fatherId": "DEMO0000",
- "state": "/process.search",
- "name": "流程查询",
- "childItems": []
- }
- ]
- }
- }
- }
- ]
react style: 二级菜单的更多相关文章
- react 侧栏二级菜单组件
侧边栏菜单组件 component 下新建menu文件,menu下建index.jsx和subitem.jsx index.jsx import React, { Component } from ' ...
- Jquery垂直下拉二级菜单
自己做了一个基于Jquery 的垂直下拉二级菜单功能,直接看图: Html的代码如下: <!DOCTYPE html> <html> <head> <meta ...
- JS实现的简单横向伸展二级菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS-鼠标经过显示二级菜单
在css处添加了border样式为了看得更清楚——源代码有一个程序漏洞,存在一个很烦人的大bug. <ul class="nav"> <li class=&quo ...
- 转:jQuery弹出二级菜单
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- html+css二级菜单制作!
二级菜单!!<!DOCTYPE html<html lang="e<head> <meta charset="UTF-8"> < ...
- js运动:多div变宽、二级菜单
定时器及运动函数. 多div变宽: <!-- Author: XiaoWen Create a file: 2016-12-13 09:36:30 Last modified: 2016-12- ...
- javascript 特效实现(3)—— 鼠标滑过显示二级菜单效果
1. 关键代码:使用 switch 或 if 判断语句,改变对应的二级菜单显示方式为 block 或 none function selectTabMenu(i){ switch(i){ case 7 ...
- jquery垂直展开折叠手风琴二级菜单
摘要:jquery实现垂直展开二级菜单 最近新开发一个简单项目,用到左侧两级的菜单.找找了手头的文件,竟然没有现成的代码,算了,去网上找找整理下吧. 注:jquery-1.8.3.min.js需要下载 ...
随机推荐
- 004-React入门概述
一.概述 参考地址:https://reactjs.org/docs/try-react.html 1.1.本地快速体验 <!DOCTYPE html> <html> < ...
- composer 常用包管理工具
名称 用途说明 说明地址 mashape/unirest-php 简单易用的HTTP请求库 官网地址 guzzlehttp/guzzle 功能强大的HTTP请求库 文档 hassankhan/conf ...
- NUnit TestFixtureSetup 和 TestFixtureTearDown
TestFixtureSetup 和 TestFixtureTearDown 在所有测试开始前(TestFixtureSetup)或结束后(TestFixtureTearDown)运行一 次.记住他只 ...
- url监控
#!/usr/bin/env python #coding:utf-8 import MySQLdb,requests import time from datetime import datetim ...
- 大数据生态,哪些框架需要全部启动,哪些只启动master,仅为汇总
主从,只需要在master节点启动 hadoop hbase 单机启动 hive 其他,需要启动每个节点 zookeeper kafka flume presto
- C# Json格式
using LitJson; //自定义Json类 JsonDataResult jsondata = new JsonDataResult() { Success = false }; HttpCo ...
- LVS持久化
在实际应用场景中,轮询调度并不都是适用的.有些情况下,需要我们把同一个会话的请求都调度给一个RS节点.这时候就需要LVS提供持久化的能力,能够实现会话保持. 一.LVS的持久化主要包括以下两个方面. ...
- GIT使用—创建一个版本库
一.GIT命令行 [root@localhost ~]# git usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] ...
- jQuery单选多选按钮选中美化特效
在线演示 本地下载
- Java结对编程四则运算一周小结
Java结对编程四则运算一周小结 需求分析 对于四则运算来说最主要的就是要计算出产生的式子(字符串的形式). 设计思路 总体可将这个项目分解为几个部分:产生式子,计算式子,判断对错并记录: 具体的思路 ...