I have 2 select boxes that i am building uisng the autocomplete component in material ui - https://mui.com/material-ui/react-autocomplete/#checkboxes
I have a edge case im trying to fix:
- A user selects both "Consumer Electronics" and "Fashion & Apparel" in the first main industries select box.
- A user then selects in the second sub industries select box, "Computer & Laptops" and "Eyewear". (1 from each group).
- If a user then removes "Fashion & Apparel" from the first select box, all of the items that were selected with that category should be removed in the second select box.
example:
https://codesandbox.io/s/checkboxestags-demo-material-ui-forked-qox19g?file=/demo.tsx
You can see in the second "Autocomplete" im using the "filterOptions" to only show options depending on the first "Autocomplete"
also see data.json in the example for the raw data.
code:
return (
<Autocomplete
multiple
options={subIndustries}
filterOptions={(x) =>
x.filter((x) =>
getValues("main_industries").find((m) => m.name === x.category)
)
}
groupBy={(option) => option.category}
disableCloseOnSelect
getOptionLabel={(option) => option.name}
onChange={(_, newValue: any) => {
setValue("sub_industries", newValue, { shouldValidate: true });
}}
defaultValue={getValues("sub_industries")}
onInputChange={(_, val) => {
console.log(val);
}}
renderOption={(props, option, { selected }) => (
<li {...props}>
<Checkbox
icon={uncheckedIcon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.name}
</li>
)}
renderInput={(params) => (
<TextField
{...params}
label="Sub Industries"
placeholder="Sub Industries"
/>
)}
/>
);
};
