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.

[Autocomplete] `getOptionLabel` callback is getting the value instead of option object

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior šŸ˜Æ

When using different types for options and value properties, the getOptionLabel method is passed value instead of option object.

letā€™s say Iā€™m using following types:

type Option = { label: string; value: any; group?: string }
type Value = string;

Iā€™m expecting getOptionLabel method always passed an Option type but thatā€™s not the case. Thatā€™s why the picked option can not be displayed properly.

getOptionLabel={(option) => option.label || "!!!"} //here sometimes Value type is passes instead of Option type.
isOptionEqualToValue={(option, value) => return option.value === value}

Expected behavior šŸ¤”

Always pass and option to getOptionLabel as a param to be consistent.

Steps to reproduce šŸ•¹

Hereā€™s a demo: https://codesandbox.io/s/controllablestates-material-demo-forked-0lekyg?file=/demo.js

Steps:

  1. Render a controlled Autocomplete component using different types for options and value props. (object and string)
  2. Utilize isOptionEqualToValue and getOptionLabel callback props

Context šŸ”¦

Iā€™m trying to render options that are objects like {label:ā€œLabelā€, value:ā€œsome valueā€}, but I want to use the value of the selected option instead of the whole option object.

Your environment šŸŒŽ

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
lovervoordecommented, Apr 28, 2022

For my use-case, I wanted to have the option as a string because my onChange uses the value to update an object in redux state, and I donā€™t want the label in there as well.

2reactions
ZeeshanTambolicommented, Nov 11, 2022

Okay I am re-opening the PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material-ui <Autocomplete /> getOptionLabel - passing empty ...
Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""]. I have 2 components. The child one is:
Read more >
Autocomplete API - Material UI - MUI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
API - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >
appendToBody | Vue Select
Callback to get an option key. If option is an object and has an id , returns option.id by default, otherwise tries to...
Read more >
How to Use The Autocomplete Component in Material-UI
getOptionLabel ā€” Used to determine the string value for a given option. This is needed because our options array is an array of...
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