How Downshift onSelect get MenuItem key or show specific fields as MenuItem label?
See original GitHub issueI’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:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@TommyQu You need to pass itemToString prop to Downshift component to tell it how to render the selected item, e.g.
@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.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.