简体   繁体   English

如何向我的 React 应用程序添加下拉菜单?

[英]How to add a dropdown menu to my React app?

I'm building a react app using facebook's:我正在使用 facebook 构建一个 react 应用程序:

https://github.com/facebookincubator/create-react-app https://github.com/facebookincubator/create-react-app

along w SASS: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-a-css-preprocessor-sass-less-etc沿着 W SASS: https : //github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-a-css-preprocessor-sass-less-etc

I'm at a point now where I need to add a dropdown menu to the header.我现在需要在标题中添加一个下拉菜单。 Similar to the header icons on StackOverflow in the top right that open and close on click.类似于 StackOverflow 上右上角的标题图标,点击打开和关闭。

I know this sounds like a dumb question but what is the right way to do this?我知道这听起来像一个愚蠢的问题,但正确的方法是什么? Do I need to add a UI Framework like a bootstrap for something like this?我是否需要为这样的东西添加一个像引导程序一样的 UI 框架? I have no need for all the bootstrap theming etc...我不需要所有的引导程序主题等......

Thank you - and please be kind to the question given I'm a solo developer and could really use some help building a solid foundation on my app.谢谢 - 请善待这个问题,因为我是一名独立开发人员,真的可以帮助我在我的应用程序上建立坚实的基础。

Thanks谢谢

Yes you can do this easily with just React:是的,您可以仅使用 React 轻松完成此操作:

 class Hello extends React.Component { render() { return <div className="nav"> <Link /> <Link /> <Link /> </div>; } } class Link extends React.Component { state = { open: false } handleClick = () => { this.setState({ open: !this.state.open }); } render () { const { open } = this.state; return ( <div className="link"> <span onClick={this.handleClick}>Click Me</span> <div className={`menu ${open ? 'open' : ''}`}> <ul> <li>Test 1</li> <li>Test 2</li> <li>Test 3</li> </ul> </div> </div> ) } } ReactDOM.render( <Hello name="World" />, document.getElementById('container') );
 .nav { display: flex; border-bottom: 1px solid gray; background: white; padding: 0 10px; } .link { width: 100px; border-right: 1px solid gray; padding: 10px; text-align: center; position: relative; cursor: pointer; } .menu { opacity: 0; position: absolute; top: 40px; // same as your nav height left: 0; background: #ededed; border: 1px solid gray; padding: 10px; text-align: center; transition: all 1000ms ease; } .menu.open { opacity: 1; } ul { margin: 0; padding: 0; list-style: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"></div>

you can use react-select like this :你可以像这样使用react-select

var Select = require('react-select');

var options = [
  { value: 'one', label: 'One' },
  { value: 'two', label: 'Two' }
];

function logChange(val) {
  console.log("Selected: " + JSON.stringify(val));
}

<Select
  name="form-field-name"
  value="one"
  options={options}
  onChange={logChange}
/>

https://github.com/JedWatson/react-select https://github.com/JedWatson/react-select

also this library :还有这个图书馆:

https://www.npmjs.com/package/react-dropdown https://www.npmjs.com/package/react-dropdown

Seems like your project is still in his infancy.看来你的项目还处于起步阶段。 And that you willing to incorporate a library to your project.并且您愿意将库合并到您的项目中。 So I would definitely recommend you to choose a library right now.所以我肯定会建议你现在就选择一个图书馆。

With React you could create your own menu without much effort.使用 React,您可以毫不费力地创建自己的菜单。 But you will also need other components for the rest of your app.但是您还需要其他组件用于应用程序的其余部分。 And the quality of your menu (and other components) will most likely be greater if you choose a library used by many (rather than your own).如果您选择许多人使用的库(而不是您自己的库),那么您的菜单(和其他组件)的质量很可能会更高。 For "quality" I mean: UX design, HTML standards, API reusability, number of defects, etc.对于“质量”,我的意思是:用户体验设计、HTML 标准、API 可重用性、缺陷数量等。

If you think your app will be small and won't need an entire UI Framework, you might want to search for an isolated component for menu.如果您认为您的应用程序很小并且不需要整个 UI 框架,您可能需要为菜单搜索一个独立的组件。 Here is a list of navigation components (including the number of github stars of each project): https://devarchy.com/react/topic/navigation这里是导航组件列表(包括每个项目的github星数): https : //devarchy.com/react/topic/navigation

But in most cases I would choose an entire UI Framework instead: https://devarchy.com/react/topic/ui-framework但在大多数情况下,我会选择整个 UI 框架: https : //devarchy.com/react/topic/ui-framework

And here are some demos of the menu/nav/app-bar of some popular UI Frameworks:以下是一些流行的 UI 框架的菜单/导航/应用栏的一些演示:

Custom dropdown自定义下拉菜单

Dropdown.js下拉菜单

import React, { useState } from "react";
import { mdiMenuDown } from "@mdi/js";
import Icon from "@mdi/react";
export default function DropDown({ placeholder, content }) {
const [active, setactive] = useState(0);
const [value, setvalue] = useState(0);
return (
<div className={active ? "dropdown_wrapper active" : "dropdown_wrapper"}>
  <span
    onClick={() => {
      setactive(active ? 0 : 1);
    }}
  >
    {value ? value : placeholder} <Icon path={mdiMenuDown} />
  </span>
  <div className="drop_down">
    <ul>
      {content &&
        content.map((item, key) => {
          return (
            <li
              onClick={() => {
                setvalue(item);
                setactive(0);
              }}
              key={key}
            >
              {item}
            </li>
          );
        })}
    </ul>
  </div>
</div>
);}

dropdown.css下拉列表.css

.dropdown_wrapper {
  position: relative;
  margin-left: 100px;
  cursor: pointer;
}

.dropdown_wrapper span {
  padding: 12px;
  border: 1px solid #a8aaac;
  border-radius: 6px;
  background-color: #ffffff;
  display: flex;
  color: #3d3e3f;
  font-size: 16px;
  letter-spacing: 0;
  line-height: 26px;
  justify-content: space-between;
  text-transform: capitalize;
}
.dropdown_wrapper span svg {
  width: 20px;
  margin-left: 80px;
  fill: #fbb800;
  transition: 0.5s ease all;
}
.dropdown_wrapper.active span svg {
  transform: rotate(180deg);
  transition: 0.5s ease all;
}
.dropdown_wrapper .drop_down {
  background-color: #fff;
  box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
  position: absolute;
  top: 53px;
  width: 100%;
  z-index: 9;
  border-radius: 6px;
  border: 1px solid #a8aaac;
  height: 0px;
  opacity: 0;
  overflow: hidden;
  transition: 0.5s ease all;
}
.dropdown_wrapper.active .drop_down {
  height: fit-content;
  opacity: 1;
  transition: 0.5s ease all;
}
.dropdown_wrapper .drop_down ul {
  list-style: none;
  padding-left: 0;
  margin: 0;
  padding: 10px;
}
.dropdown_wrapper .drop_down ul li {
  padding: 10px 0px;
  color: #3d3e3f;
  font-size: 16px;
  letter-spacing: 0;
  line-height: 26px;
  text-transform: capitalize;
  cursor: pointer;
  font-weight: 300;
}
.dropdown_wrapper .drop_down ul li:hover {
  color: #faab1e;
  transition: 0.5s ease all;
}

parent.js父.js

<DropDown placeholder="select a type" content={["breakfast", "lunch", "dinner", "Snacks"]} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM