I have data coming in from API in the following format:
const companies = [
{ type: "Banks", names: ["CIBC", "RBC", "BMO"] },
{ type: "E-Banks", names: ["Oaken", "XYZ", "EQ"] },
{ type: "Credit Unions", names: ["ABC", "TTB"] }
];
In Material UI's autocomplete I want to be able to populate 'type' property as the groupBy heading which is working but I am unable to figure out how to populate these 'names' within options.
Sandbox is here: https://codesandbox.io/s/material-demo-forked-oo3td?file=/demo.js
My autocomplete component looks like this:
<Autocomplete
open
onClose={handleClose}
multiple
value={pendingValue}
onChange={(event, newValue) => {
setPendingValue(newValue);
}}
disableCloseOnSelect
disablePortal
//renderTags={() => null}
noOptionsText="No labels"
renderOption={(option, { selected }) => (
<>
<DoneIcon
//className={classes.iconSelected}
style={{ visibility: selected ? "visible" : "hidden" }}
/>
{/* how to map below */}
<div className={classes.text}>{[...option.names]}</div>
<CloseIcon
//className={classes.close}
style={{ visibility: selected ? "visible" : "hidden" }}
/>
</>
)}
options={[...companies].sort((a, b) => {
// Display the selected labels first.
let ai = value.indexOf(a);
ai = ai === -1 ? value.length + companies.indexOf(a) : ai;
let bi = value.indexOf(b);
bi = bi === -1 ? value.length + companies.indexOf(b) : bi;
return ai - bi;
})}
groupBy={(option) => option.type}
getOptionLabel={(option) => option.names[0]} //how to map here
renderInput={(params) => (
<InputBase
ref={params.InputProps.ref}
inputProps={params.inputProps}
autoFocus
className={classes.inputBase}
/>
)}
/>
I tried spreading the names in renderOption prop but then in the autocompelte it just populates all options in one line which makes sense since its part of one option. I am unable to figure out how I could have it in seperate lines along with being able to individually select them.