Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Grommet Select - passing in object data as options

I am using Grommet v2 components and trying to mirror the display used in the Select 'Seasons' example in Grommet's storybook.

The field appears like this:

The difference is my data needs to separate label and value:

const Options = [
   {
     label: "S01",
     value: "283736"
   },
   {
     label: "S02",
     value: "293774"
   },

instead of using the default:

const Options = [
  "S01",
  "S02",

Here is an example on Codesandbox

The object format was used in Grommet's example of ObjectMultiSelect in their storybook. I found the Select component needs labelKey="label" and valueKey="value" to load the objects as options, but adding these two props seems to break the component options.

I would like for the data passed in to resemble

const Options = [
   {
     label: "S01",
     value: "283736"
   },
   {
     label: "S02",
     value: "293774"
   },

and still have the options displayed as above.

like image 628
Brantley English Avatar asked Apr 25 '19 21:04

Brantley English


1 Answers

            <Select
              options={[
                {
                  lab: "S01",
                  val: "283736"
                },
                {
                  lab: "S02",
                  value: "293774"
                }
              ]}
              labelKey="lab"
              dropHeight="large"
              name="primaryServer"
              value={this.props.values.primaryServer}
              placeholder="Select Primary Server"
              emptySearchMessage={"No Servers Available"}
              onChange={({ option }) => {
                console.log(option.lab)
                console.log(option.val)
              }}
            />
            // Output // S01 // 283736
            // This worked for me. labelKey="lab" is required.
like image 122
Rohit Bhambhani Avatar answered Sep 19 '22 08:09

Rohit Bhambhani