javascript 你如何在 react-select v2 中创建 optgroups?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52503732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create optgroups in react-select v2?
提问by ouni
I want to have optgroups in my react-select list, but it doesn't seem to be documented anywhere. I have the following structure, which I pulled from a comment in https://github.com/JedWatson/react-select/issues/59:
我想在我的 react-select 列表中有 optgroups,但它似乎没有记录在任何地方。我有以下结构,这是我从https://github.com/JedWatson/react-select/issues/59 中的评论中提取的:
render() {
const options = [
{
label: "Group 1",
children: [
{ label: "Group 1, option 1", value: "value_1" },
{ label: "Group 1, option 2", value: "value_2" }
]
},
{ label: "A root option", value: "value_3" },
{ label: "Another root option", value: "value_4" }
];
return <Select options={options} />
}
I expect "Group 1" to be an optgroup, with options 1 and 2 as children. Instead, "Group 1" just appears as a regular option. Does anyone know what the correct key is, within "Group 1", to turn it into an optgroup?
我希望“组 1”是一个 optgroup,选项 1 和 2 是子项。相反,“第 1 组”只是作为常规选项出现。有谁知道在“第 1 组”中将其转换为 optgroup 的正确密钥是什么?
I've already tried "children", and "values", but to no effect.
我已经尝试过“儿童”和“价值观”,但没有效果。
回答by ouni
optionsis the magic key:
选项是魔法钥匙:
render() {
const options = [
{
label: "Group 1",
options: [
{ label: "Group 1, option 1", value: "value_1" },
{ label: "Group 1, option 2", value: "value_2" }
]
},
{ label: "A root option", value: "value_3" },
{ label: "Another root option", value: "value_4" }
];
return <Select options={options} />
}
This renders the way that I expect.
这呈现出我期望的方式。

