1

I am trying to work with the Autocomplete component,

this is my code:

 <Autocomplete
 multiple
 id="checkboxes-tags-demo"
 options={ownerOptions2}
 disableCloseOnSelect
 filterSelectedOptions={true}
 getOptionLabel={(option) => option.title}
 onChange={(event, newValue) => {
 console.log(newValue);
 ** ****props.onSelectionChange(newValue)***** **
 }}
 renderOption={(option, { selected }) => (
 <React.Fragment>
 <Checkbox
 icon={icon}
 checkedIcon={checkedIcon}
 style={{ marginRight: 8 }}
 checked={selected}
 />
 {option.title}
 </React.Fragment>
 )}
 style={{ width: 500 }}
 renderInput={(params) => (
 <TextField {...params} variant="outlined" label="label" placeholder="" />
 )}
 />

Without the "onChange" part, it's working great. But I need to save and do something with the selected values, (is there another way to do it?).

When I'm adding the "onChange" part and run there a function from props, neither the filterSelectedOptions nor the checkbox is working.

What is the right way to do it?

Tnx

1
  • how did you fix this? Commented Jun 17, 2022 at 10:31

1 Answer 1

2

My guess (based on https://material-ui.com/components/autocomplete/#controllable-states ) is that if you specify onChange or value you need to supply both (and therefore handle the state of value yourself).

answered Oct 21, 2020 at 14:41
Sign up to request clarification or add additional context in comments.

2 Comments

A more specific problem : The onChange does working, but, if iI trying to use a function from props inside it, so the filterSelectedOptions is not working. any ideas?
What does that function do? If it changes the state of the parent component you might need to manage the state of this Autocompomplete from the parent component.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.