0

我有一个使用 reactjs 和 reactstrap 的简单侧边栏。目前,我的侧边栏下拉菜单在点击时会覆盖较低的下拉菜单。如何更改此设置,以便在单击时侧边栏链接被向下推而不是被覆盖?

NavBarSide 组件:

import React from 'react';
import { Nav, Dropdown, DropdownItem, DropdownToggle, DropdownMenu } from 'reactstrap';

export class NavBarSide extends React.Component {
    constructor(props) {
        super(props);

        this.toggleError = this.toggleError.bind(this);
        this.togglePrice = this.togglePrice.bind(this);
        this.toggleVolume = this.toggleVolume.bind(this);

        this.state = {
            dropdownError: false,
            dropdownPrice: false,
            dropdownVolume: false
        };
    }

    toggleError(){
        this.setState({
            dropdownError: !this.state.dropdownError
        });
    }

    togglePrice(){
        this.setState({
            dropdownPrice: !this.state.dropdownPrice
        });
    }

    toggleVolume() {
        this.setState({
            dropdownVolume: !this.state.dropdownVolume
        });
    }
    render() {
        return (
            <Nav className="col-md-2 d-none d-md-block bg-light sidebar">
                <div className="sidebar-sticky">
                    <ul className="nav flex-column">
                        <Dropdown nav isOpen={this.state.dropdownError} toggle={this.toggleError}>
                            <DropdownToggle nav caret>Error</DropdownToggle>
                            <DropdownMenu>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/highest/error")}>Highest</DropdownItem>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/lowest/error")}>Lowest</DropdownItem>
                            </DropdownMenu>
                        </Dropdown>
                        <Dropdown nav isOpen={this.state.dropdownPrice} toggle={this.togglePrice}>
                            <DropdownToggle nav caret>Price</DropdownToggle>
                            <DropdownMenu>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/highest/price")}>Highest</DropdownItem>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/lowest/price")}>Lowest</DropdownItem>
                            </DropdownMenu>
                        </Dropdown>
                        <Dropdown nav isOpen={this.state.dropdownVolume} toggle={this.toggleVolume}>
                            <DropdownToggle nav caret>Volume</DropdownToggle>
                            <DropdownMenu>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/highest/volume")}>Highest</DropdownItem>
                                <DropdownItem href="#" onClick={() => this.props.clickHandler("/api/lowest/volume")}>Lowest</DropdownItem>
                            </DropdownMenu>
                        </Dropdown>
                    </ul>
                </div>
            </Nav>
        );
    }
}

当前功能:

在此处输入图像描述

在此处输入图像描述

我还附上了整个应用程序的相关 css 文件:

body {
    font-size: .875rem;
    background-color: #f7f7f7;
}

/*
 * Title bar
 */
.title-bar {
    padding-right: 15px;
}

/*
 * Connection status
 */
#connection-status {
    height: 20px;
    width: 20px;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    display: block;
}

#connection-status.connected {
    background-color: green;
}

#connection-status.disconnected {
    background-color: red;
}

/*
 * Sidebar
 */
.sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 100; /* Behind the navbar */
    padding: 0;
    box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}

.sidebar-sticky {
    position: sticky;
    top: 48px; /* Height of navbar */
    height: calc(100vh - 48px);
    padding-top: .5rem;
    overflow-x: hidden;
}

.sidebar .nav-link {
    font-weight: 500;
    color: #333;
}

.sidebar .nav-link{
    margin-right: 4px;
    color: #999;
}

.sidebar .nav-link.active {
    color: #007bff;
}

.sidebar .nav-link:hover,
.sidebar .nav-link.active{
    color: inherit;
}

.sidebar-heading {
    font-size: .75rem;
    text-transform: uppercase;
}

/*
 * Navbar
 */
.navbar-brand {
    padding-top: .75rem;
    padding-bottom: .75rem;
    font-size: 1rem;
    background-color: rgba(0, 0, 0, .25);
    box-shadow: inset -1px 0 0 rgba(0, 0, 0, .25);
}

.navbar .form-control {
    padding: .75rem 1rem;
    border-width: 0;
    border-radius: 0;
}

.form-control-dark {
    color: #fff;
    background-color: rgba(255, 255, 255, .1);
    border-color: rgba(255, 255, 255, .1);
}

.form-control-dark:focus {
    border-color: transparent;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, .25);
}

.full-screen{
    height: 100vh;
}

/*
 * Utilities
 */
.border-top { border-top: 1px solid #e5e5e5; }
.border-bottom { border-bottom: 1px solid #e5e5e5; }
4

1 回答 1

2

这应该可以工作,在查看示例时阅读道具,请参阅导航的垂直道具:

const Sidebar = () => {
  const [isOpen, setIsOpen] = useState(false);

  const toggle = () => setIsOpen(!isOpen);
  return (
    <section className='sidebar'>
      <Navbar color='faded' expand='md'>
        <NavbarToggler onClick={toggle} />
        <Collapse isOpen={isOpen} navbar>
          <Nav vertical navbar>
            <NavItem>
              <NavLink href='/components/'>Components</NavLink>
            </NavItem>
            <NavItem>
              <NavLink href='https://github.com/reactstrap/reactstrap'>
                Github
              </NavLink>
            </NavItem>
          </Nav>
        </Collapse>
      </Navbar>
    </section>
  );
};
于 2019-10-29T18:24:16.177 回答