使用带有react-bootstrap的Nav组件的Accordion SideBar菜单

时间:2016-10-11 09:42:38

标签: javascript css twitter-bootstrap reactjs react-bootstrap

我开始使用react-bootstrap构建我的UI,现在我有一个重要的任务是使用标准bootstrap组件创建SideBar Accordion Menu。我找到了使用panel-grouptable的{​​{3}}示例,但我认为这不是最佳方式,因为nav在导航组件方面更自然。

我希望我的SideBar菜单代码如下所示(react-bootstrap组件):

<Nav bsStyle="pills" stacked>
    <NavItem title="Item 1">
        <Nav bsStyle="pills" stacked>
            <NavItem title="Sub Item 1">Sub Item 1</NavItem>
            <NavItem title="Sub Item 2">Sub Item 2</NavItem>
            <NavItem title="Sub Item 3">Sub Item 3</NavItem>
        </Nav>
    </NavItem>
    <NavItem title="Item 2">Item 2</NavItem>
    <NavItem title="Item 3">Item 3</NavItem>
</Nav>

react-bootstrapbootstrap的任何建议都会有用。

1 个答案:

答案 0 :(得分:9)

我找到了纯nav解决方案。所以也许它可以帮助别人。解决方案是使用标准的“react-bootstrap”组件,例如Navbar,并结合bootstrap css自定义。

以下示例代码:

<强> SideBarMenu.jsx

import React from 'react';
import styles from './sideBarMenu.css';
import {Nav, NavItem, Navbar, NavDropdown, MenuItem, Glyphicon} from 'react-bootstrap';

export default React.createClass( {

    render: function() {
        return <div id="sidebar-menu" className={styles.sideBarMenuContainer}>
            <Navbar fluid className={styles.sidebar} inverse >

                <Navbar.Header>
                    <Navbar.Brand>
                        <a href="/">User Name</a>
                    </Navbar.Brand>
                    <Navbar.Toggle />
                </Navbar.Header>

                <Navbar.Collapse>
                    <Navbar.Text className={styles.userMenu}>
                        <Navbar.Link href="#"><Glyphicon glyph="home"/></Navbar.Link>
                        <Navbar.Link href="#"><Glyphicon glyph="log-out"/></Navbar.Link>
                    </Navbar.Text>
                    <Nav>
                        <NavDropdown eventKey={1} title="Item 1">
                            <MenuItem eventKey={1.1} href="#">Item 1.1</MenuItem>
                        </NavDropdown>
                        <NavItem eventKey={2}>Item 2</NavItem>
                        <NavItem eventKey={3}>Item 3</NavItem>
                    </Nav>
                </Navbar.Collapse>

            </Navbar>
        </div>;
    }
});

<强> sideBarMenu.css

.sideBarMenuContainer {
    position: fixed;
    height: 100%;
}

.userMenu {
    width: 100%;
    text-align: center;
    margin-right: 0px;
    margin-left: 0px;    
    margin-top: 0px;
}

nav.sidebar :global(.navbar-brand) :global(.glyphicon) {
    margin-right: 0px;
}


/*Remove rounded coners*/
nav.sidebar.:global(navbar) {
    border-radius: 0px;
}

nav.sidebar {
    -webkit-transition: margin 200ms ease-out;
    -moz-transition: margin 200ms ease-out;
    -o-transition: margin 200ms ease-out;
    transition: margin 200ms ease-out;
}

/* .....NavBar: Icon only with coloring/layout.....*/

/*small/medium side display*/
@media ( min-width : 768px) {


    /*Center Brand*/
    nav.sidebar.:global(navbar).sidebar>.container :global(.navbar-brand), :global(.navbar)>:global(.container-fluid) :global(.navbar-brand)
        {
        margin-left: 0px;
    }
    /*Center Brand*/
    nav.sidebar :global(.navbar-brand), nav.sidebar :global(.navbar-header) {
        text-align: center;
        width: 100%;
        margin-left: 0px;
    }

    /*Center Icons*/
    nav.sidebar a {
        padding-right: 13px;
    }

    /*adds border top to first nav box */
    nav.sidebar :global(.navbar-nav)>li:first-child {
        border-top: 1px #e5e5e5 solid;
    }

    /*adds border to bottom nav boxes*/
    nav.sidebar :global(.navbar-nav)>li {
        border-bottom: 1px #e5e5e5 solid;
    }

    /* Colors/style dropdown box*/
    nav.sidebar :global(.navbar-nav) :global(.open) :global(.dropdown-menu) {
        position: static;
        float: none;
        width: auto;
        margin-top: 0;
        background-color: transparent;
        border: 0;
        -webkit-box-shadow: none;
        box-shadow: none;
    }

    /*allows nav box to use 100% width*/
    nav.sidebar :global(.navbar-collapse), nav.sidebar :global(.container-fluid) {
        padding: 0 0px 0 0px;
    }

    /*colors dropdown box text */
    :global(.navbar-inverse) :global(.navbar-nav) :global(.open) :global(.dropdown-menu)>li>a {
        color: #777;
    }

    /*gives sidebar width/height*/
    nav.sidebar {
        width: 200px;
        height: 100%;
        margin-left: -160px;
        float: left;
        z-index: 8000;
        margin-bottom: 0px;
    }

    /*give sidebar 100% width;*/
    nav.sidebar li {
        width: 100%;
    }

    /* Move nav to full on mouse over*/
    nav.sidebar:hover {
        margin-left: 0px;
    }
    /*for hiden things when navbar hidden*/
    :global(.forAnimate) {
        opacity: 0;
    }
}

/* .....NavBar: Fully showing nav bar..... */
@media ( min-width : 1330px) {

    /*Show all nav*/
    nav.sidebar {
        margin-left: 0px;
        float: left;
    }
    /*Show hidden items on nav*/
    nav.sidebar :global(.forAnimate) {
        opacity: 1;
    }
}

nav.sidebar :global(.navbar-nav) :global(.open) :global(.dropdown-menu)>li>a:hover, nav.sidebar :global(.navbar-nav) :global(.open) :global(.dropdown-menu)>li>a:focus
    {
    color: #CCC;
    background-color: transparent;
}

nav:hover :global(.forAnimate) {
    opacity: 1;
}

section {
    padding-left: 15px;
}

请注意,我将webpackcss-loader模块范围结合使用,因此:global表示bootstrap css类名。

It will look like this