简体   繁体   English

反应选择中的嵌套下拉列表

[英]Nested dropdown in react-select

I am using react-select to render the dropdown.我正在使用 react-select 来呈现下拉列表。 The options to render the dropdown looks like this呈现下拉菜单的选项如下所示

 [
    {   
        text:'Fruit',
        value:'mango'
        level: 0
    },
    {   
        text:'Seasonal',
        slug: 'saasonal',
        level: 1
    },
    {   
        text:'Orange',
        slug: 'orange',
        level: 2
    },
    {   
        text:'Mango',
        slug: 'mango',
        level: 2
    },
    {
        text: 'Winter',
        slug:'winter',
        level:1
    },
    {
        text: 'Plum',
        slug:'plum',
        level:2
    }

] ]

I would like the dropdown to be like我希望下拉菜单像

Fruit   
    Seasonal
        Orange
        Mango
    Winter
        Plum

I have tried adding a group to the react-select, but then the group heading is not clickable.我曾尝试向 react-select 添加一个组,但是该组标题不可点击。 I have also referred to the stackoverflow link我还提到了stackoverflow链接

[https://stackoverflow.com/questions/53119912/how-can-i-create-nested-option-groups-in-react-select-v2][1] [https://stackoverflow.com/questions/53119912/how-can-i-create-nested-option-groups-in-react-select-v2][1]
and tried creating a recursive dropdown in the same manner it is posted on the link above by passing my options as the data but was not successful.并尝试通过将我的选项作为数据传递,以与上面链接中发布的相同方式创建递归下拉列表,但没有成功。

The order of the within nested dropdown is based on the level.嵌套下拉列表的顺序基于级别。

react-select actually just got this feature builtin in July. react-select 实际上刚刚在 7 月份内置了这个功能。

Here's the pull request where they implemented it.这是他们实现它的拉取请求。

Here's a working example from the pull request page. 这是拉取请求页面中的一个工作示例。

I'm not sure if it has made its way into the master branch yet or not but you can always pull from the version listed in the package.json file in the sample above if the sample code doesn't work with the version you're using.我不确定它是否已经进入主分支,但如果示例代码不适用于您的版本,您始终可以从上面示例中的 package.json 文件中列出的版本中提取重新使用。

EDIT: Scratch that, you want group headings to be clickable.编辑:从头开始,您希望组标题可点击。 I don't think that was part of this commit.我不认为那是这次提交的一部分。

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

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