简体   繁体   English

React AntDesign Menu Sub Item... 让它像组件一样工作

[英]React AntDesign Menu Sub Item ... make it work like component

For the AntD menu... we utilise <Menu> , <Menu.Item> , <SubMenu> .对于 AntD 菜单...我们使用<Menu><Menu.Item><SubMenu>

But I don't want to use these for navigation but rather for representation.但我不想将这些用于导航,而是用于表示。 I want to display the attributes of an object using a dropdown as such.我想使用下拉菜单来显示 object 的属性。

For eg.例如。 Apple -> red, fruit;苹果 -> 红色,水果; Cucumber -> green, vegetable; Cucumber -> 绿色,蔬菜; would be displayed as a menu with Apple and Cucumber as the Submenu headings, and the dropdowns for each would be red, fruit and green, vegetable respectively.将显示为一个菜单,其中 Apple 和 Cucumber 作为子菜单标题,每个下拉菜单分别为红色、水果和绿色、蔬菜。

But I don't want to predefine attributes and Submenu headings.但我不想预定义属性和子菜单标题。 If it was a component (cards for example), I could've made the component render per object, so that if there were 10 objects, 10 cards (for example) would be rendered.如果它是一个组件(例如卡片),我可以让组件按照 object 进行渲染,这样如果有 10 个对象,就会渲染 10 个卡片(例如)。

Is it possible to do the same for <SubMenu> and <Menu.Item> where I send the data and it first looks at the key 'Name' and renders as a Submenu Heading and renders attributes individually as Menu Items within the Submenu?是否可以对发送数据的<SubMenu><Menu.Item>执行相同操作,它首先查看键“名称”并呈现为子菜单标题并将属性单独呈现为子菜单中的菜单项?

Are there any alternatives I can make use of?有没有我可以使用的替代方案?


Not sure If my question is very clear... I'm happy to clarify anything if confused.不确定我的问题是否很清楚......如果感到困惑,我很乐意澄清任何事情。

Not sure if this is what you want不确定这是否是您想要的

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Menu } from "antd";

const { SubMenu } = Menu;

const data = [
  {
    Name: "Apple",
    Colour: "red",
    Type: "fruit"
  },
  {
    Colour: "green",
    Type: "vegetable",
    Name: "Cucumber",
    Season: "spring"
  },
  {
    Name: "Book",
    Title: "hello",
    Author: "nick"
  }
];

const Sider = () => {
  const [openKeys, setOpenKeys] = React.useState(["sub1"]);

  const onOpenChange = (keys) => {
    const latestOpenKey = keys.find((key) => openKeys.indexOf(key) === -1);
    if (data.indexOf(latestOpenKey) === -1) {
      setOpenKeys(keys);
    } else {
      setOpenKeys(latestOpenKey ? [latestOpenKey] : []);
    }
  };

  return (
    <Menu mode="inline" onOpenChange={onOpenChange} style={{ width: 256 }}>
      {data.map((each) => (
        <SubMenu key={each.Name} title={each.Name}>
          {Object.entries(each).map(
            ([key, value]) =>
              key !== "Name" && (
                <Menu.Item key={each.Name + "-" + key}>{value}</Menu.Item>
              )
          )}
        </SubMenu>
      ))}
    </Menu>
  );
};

ReactDOM.render(<Sider />, document.getElementById("container"));

CodeSandbox Demo CodeSandbox演示

Let me know if this works for you让我知道这是否适合您

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

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