React-Select 分组选项:如何设置和提取组标题的值并将其附加到选项

发布于 2025-01-19 10:43:26 字数 1308 浏览 4 评论 0原文

我一直在使用React选择组件,并且一直在使用分组选项来显示颜色和风味的组以及它们各自的选项可供选择:

import React, { CSSProperties } from "react";

import Select from "react-select";

const colorOptions = [
  "Red",
  "Blue",
  "Green",
  "Yellow",
  "Orange",
  "Purple",
  "Brown"
].map((o) => ({
  value: o.replace(" ", "_").toLowerCase(),
  label: o
}));

const flavorOptions = [
  "Vanilla", 
  "Chocolate", 
  "Strawberry", 
  "Cookies"
].map((o) => ({
    value: o.replace(" ", "_").toLowerCase(),
    label: o
  }));

const groupedOptions = [
  {
    label: "Colors",
    options: colorOptions
  },
  {
    label: "Flavors",
    options: flavorOptions
  }
];

export default () => (
  <Select options={groupedOptions} onChange={(e) => console.log(e)} />
);

通过这样做,我们有以下分组的数据结构:

label: "Flavors"
options: Array(4)
    0: {label: "Vanilla", value: "vanilla"}
    1: {label: "Chocolate", value: "chocolate"}
    2: {label: "Strawberry", value: "strawberry"}
    3: {label: "Cookies", value: "cookies"}

但是,只要React选择组件的控制台日志激活所选值的ONCHANGE,我们只提供此{value:“ vanilla”,label:“ vanilla”}

我想知道最好是最好的将上grouplabel(颜色或口味)连接到此对象的方法,或者能够拔出此值并将其重新映射到如下:

{ 
  label: "Flavors"
  options: {
  value: "vanilla",
  label: "Vanilla"
  }
}

I've been working with the React-Select component and have been using grouped options to display the groups of color and flavor along with their respective options to select from:

import React, { CSSProperties } from "react";

import Select from "react-select";

const colorOptions = [
  "Red",
  "Blue",
  "Green",
  "Yellow",
  "Orange",
  "Purple",
  "Brown"
].map((o) => ({
  value: o.replace(" ", "_").toLowerCase(),
  label: o
}));

const flavorOptions = [
  "Vanilla", 
  "Chocolate", 
  "Strawberry", 
  "Cookies"
].map((o) => ({
    value: o.replace(" ", "_").toLowerCase(),
    label: o
  }));

const groupedOptions = [
  {
    label: "Colors",
    options: colorOptions
  },
  {
    label: "Flavors",
    options: flavorOptions
  }
];

export default () => (
  <Select options={groupedOptions} onChange={(e) => console.log(e)} />
);

By doing this, we have the following data structure for groupedOptions:

label: "Flavors"
options: Array(4)
    0: {label: "Vanilla", value: "vanilla"}
    1: {label: "Chocolate", value: "chocolate"}
    2: {label: "Strawberry", value: "strawberry"}
    3: {label: "Cookies", value: "cookies"}

However, whenever the console log of the React-Select component activates for the OnChange of the selected value we are only provided this {value: "vanilla", label:"Vanilla"}

I was wondering what would be the best approach to attach the upper groupLabel (Colors or Flavors) to this object or to be able to pull out this value and remap it to appear like the following:

{ 
  label: "Flavors"
  options: {
  value: "vanilla",
  label: "Vanilla"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文