Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoselect doesn't replace the old value with the new one if i change the options array (case of language change when selected) #24488

Closed
eran-or opened this issue Jan 18, 2021 · 0 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module!

Comments

@eran-or
Copy link

eran-or commented Jan 18, 2021

!!! Just to be clear - this is working solution.

I moved to controlled Component solution because the uncontrolled has the bug.

reproduce

copy this solution and remove the hooks (useState, useEffect, onChange) and all related
Try a model with two different languages
const options1 = [{id:"1", name: "hello"}, {id:"2", name: "world" }]
const options2 = [{id:"1", name: "שלום", {id:"2", name: "עולם"}]
After render Select an option from the list.
Then change the language - use a native select for this
This will inject a different options.
The selected value should be the name from the new options array

/* typescript */
import React, {memo, useEffect, useState} from 'react'
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

interface ComboProps {
placeholder?: string;
options: Option[]
}

type Option = { id: string, name: string }

const Combo: React.FC = ({options}) => {
const [value, setValue] = useState<Option | null>(null)

useEffect(()=>{
const newValue = options.find(o=>o.id === value?.id) || null
setValue(newValue)
}, [options])

return <Autocomplete
options={options}
getOptionLabel={(option) => option?.name}
// getOptionSelected helping to get rid of warning message
getOptionSelected={(option, value)=> option.id === value.id}
value={value}
onChange={(event, newValue) => setValue(newValue)}
style={{ width: 300 }}
renderInput={(params) =>{
  return  <TextField {...params} label="Combo box" variant="outlined" />
}}

/>
}

export default memo(Combo)

@eran-or eran-or added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 18, 2021
@oliviertassinari oliviertassinari removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 18, 2021
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

3 participants