getOptionLabel
Like Kalhan said, getOptionLabel sets the string label in the dropdown.
For example:
const users = [
{ userName: 'bob', age: 20, message:[...] },
{ userName: 'jane', age: 43, message:[...] },
{ userName: 'joe', age: 88, message:[...] },
}
<Autocomplete
id="combo-box-demo"
options={users}
getOptionLabel={(user) => user.userName }
getOptionSelected
To clarify, getOptionSelected is used to determine if the selected value (i.e. the string in the autocomplete text field when you select an item from the dropdown) matches an option (in this case, user object) from the options array.
According to the Material-ui docs, getOptionSelected has the following signature, where option is the option to test and value is the value to test against:
function(option: T, value: T) => boolean
As an example, by using getOptionSelected, I can get the full user object when an element is selected from the dropdown (also avoids warnings such as, "The value provided to Autocomplete is invalid...")
const users = [
{ userName: 'bob', age: 20, message:[...] },
{ userName: 'jane', age: 43, message:[...] },
{ userName: 'joe', age: 88, message:[...] },
}
<Autocomplete
id="combo-box-demo"
options={users}
getOptionLabel={(user) => user.userName }
getOptionSelected={(option, value) => option.userName === value.userName }
onChange={(event, newValue) => {
this.setValue(newValue);
}}
// more code
setValue = (newValue) => {
console.log(newValue); // { userName: 'jane', age: 43, message:[...] }
}