question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How Downshift onSelect get MenuItem key or show specific fields as MenuItem label?

See original GitHub issue

I’m trying to use the example from material-ui https://material-ui-next.com/demos/autocomplete/

In renderSuggestion function, I have menu item with key, how can I get the key with onSelect function?

  renderSuggestion({
    suggestion,
    index,
    itemProps,
    highlightedIndex,
    selectedItem
  }) {
    const isHighlighted = highlightedIndex === index;
    const isSelected = (selectedItem === null);
    return (
      <MenuItem
        {...itemProps}
        key={suggestion.id}
        selected={isHighlighted}
        component="div"
        style={{
          fontWeight: isSelected ? 500 : 400
        }}
      >
        {suggestion.firstName} {suggestion.lastName}
      </MenuItem>
    );
  }
        <Downshift onSelect={selection => {
          console.log(selection);
            this.setState({selectedStudentId: selection});
          }}>
          {({
            getInputProps,
            getItemProps,
            isOpen,
            inputValue,
            selectedItem,
            highlightedIndex
          }) => (
            <div>
            <Grid container spacing={40}>
              <Grid item xs={4}>
                          <div>
              {this.renderInput({
                fullWidth: true,
                classes,
                InputProps: getInputProps({
                  placeholder: "Type student name",
                  id: "integration-downshift-simple"
                })
              })}
              {isOpen ? (
                <Paper className={classes.paper} square>
                  {this.getSuggestions(inputValue).map((suggestion, index) =>
                    this.renderSuggestion({
                      suggestion,
                      index,
                      itemProps: getItemProps({
                        item: suggestion.firstName + " " + suggestion.lastName
                      }),
                      highlightedIndex,
                      selectedItem
                    })
                  )}
                </Paper>
              ) : null}
            </div>
              </Grid>
              <Grid item xs={4}>
                <Button variant="raised" color="primary" onClick={this.searchStudent}>
                  Search
                  <Search />
                </Button>
              </Grid>
            </Grid>
            </div>
          )}
        </Downshift>

If I change to below:

itemProps: getItemProps({
    item: suggestion
}),

When I select the object, it will show as: [object Object].

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
donysukardicommented, Mar 19, 2018

@TommyQu You need to pass itemToString prop to Downshift component to tell it how to render the selected item, e.g.

<Downshift
   ...
   itemToString={item => `${item.firstName} ${item.lastName}`}
/>

@kentcdodds we should think of a way to better highlight the importance of itemToString. Seems like a lot of people miss it from the current documentation.

0reactions
kentcdoddscommented, Mar 20, 2018

I’m not sure what to do about this one so I’m going to go ahead and close it. Someone can open a PR to improve the docs if they like.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Downshift in Common Dropdown Use Cases
getItemProps : This function returns the props you should apply to any menu item elements you render.
Read more >
How to use Tab instead of Enter in Downshift to select ...
I set the onKeyDown handler to recognize Tab key press and I am able to get the highlightedIndex value correctly but how to...
Read more >
useSelect
It returns a set of props that are meant to be called, and their results destructured on the dropdown's elements: its label, toggle...
Read more >
Building accessible components with Downshift
In this tutorial, we'll demonstrate how to build accessible components using Downshift. Downshift is a JavaScript library for building ...
Read more >
React Select component - Material UI
Basic select ; InputLabel id · "demo-simple-select-label" ; MenuItem value · {10} ; MenuItem value · {20} ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found