How to create nested group in material UI React - reactjs

<Autocomplete
id="grouped-demo"
options={options.sort((a, b) => -b.firstLetter.localeCompare(a.firstLetter))}
groupBy={(option) => option.firstLetter}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="With categories" variant="outlined" />}
/>
Above example code from material UI https://material-ui.com/components/autocomplete/#grouped
Here how can i achieve nested group

Related

How to store Autocomplete Material- UI values and names in array in spread operator hooks

const [filter, setFilter] = useState({ DEPARTMENT: " ", Hosp_Name: " ", Equip_Name: " ", Status: " ",
Equip_Id: " " });
const handleChange = (event,value ) => {
setFilter({...filter,[event.name]: value.value });
console.log(filter)
return (
<Autocomplete
disablePortal
id="combo-box-demo"
name="DEPARTMENT"
options={dept}
options={dept.map(option => ( option.DEPARTMENT? option.DEPARTMENT:'' ))}
value={option.DEPARTMENT}
sx={{ width: 200 }}
renderInput={(params) => <TextField {...params} label="Equip" />}
onChange = {handleChange}
/>
<Autocomplete
disablePortal
id="combo-box-demo"
options={equip.map((option) => (option.EQUIPMENT_NAME))}
name="Equip_Name"
sx={{ width: 200 }}
renderInput={(params) => <TextField {...params} label="Equip" />}
onChange={handleChange}
/>
<Autocomplete
disablePortal
id="combo-box-demo"
name="Hosp_Name"
options={hospname.map((option) => (option.Unit_Name)? option.Unit_Name:'' )}
sx={{ width: 150 }}
renderInput={(params) => <TextField {...params} label="Hosp" />}
onChange={handleChange}
/>
<Autocomplete
disablePortal
id="combo-box-demo"
name="Status"
options={status.map((option) => (option.EQUIP_STATUS)? option.EQUIP_STATUS:'')}
sx={{ width: 150 }}
renderInput={(params) => <TextField {...params} label="Status" />}
onChange={handleChange}
/>
<Autocomplete
disablePortal
id="combo-box-demo"
name="Equip_Id"
options={equipid.map((option) => (option.EQUIPMENT_ID)? option.EQUIPMENT_ID:'')}
sx={{ width: 150 }}
renderInput={(params) => <TextField {...params} label="ID" />}
onChange={handleChange}
/>
I want to store name and value in setFilter({...filter,[event.name]: value.value });
console.log(filter) should show data from auto coplete as [DEPARTMENT=ccu&Equip_Name=Cardiac monitor&Hosp_Name=Beas Hosp) ....and so on]
and use to query api like this http://192.168.16.118:4000/equipments?search=${filter}
to get the value from node js express

candidate.toLowerCase is not a function. (In 'candidate.toLowerCase()', 'candidate.toLowerCase' is undefined) Material UI

I am using AutoComplete API of material UI. there is an object of top100Films which contains the title and year of movies. My autocomplete works fine if i search with top100Film.title as shown in code
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title} // <--
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
/>
But if i want to search the movies with top100Films.year then i get an error message
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.year}//<--
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
/>
candidate.toLowerCase is not a function. (In 'candidate.toLowerCase()', 'candidate.toLowerCase' is undefined)
How can i use any other data field except .title
the code is in Link
getOptionLabel should return string while top100Films.year returns a number. change the getOptionLabel as below and it'll work fine:
getOptionLabel={(option) => option.year.toString()}
sandbox
need cast year in to string codesandbox

How to add onClick event on the options in Autocomplete Component(Material UI)?

How is possible to add onClick event on the options - i mean when u click the selected option i want to link somewhere, not to just put in the searchField(or textField)? I search really hard on the web, but i just cant find how to do that.
<Autocomplete
freeSolo
classes={classes}
options={searchItems}
getOptionLabel={(option) =>
option.title ? option.title : option.name
}
style={{ width: 300, borderRight: "none", borderLeft: "none" }}
renderInput={(params) => {
return (
<TextField
{...params}
variant="outlined"
fullWidth
placeholder="Search for movie, tv or person"
value={value}
onChange={(e) => handleChange(e)}
/>
);
}}
/>
You need to move the onChange function inside the Autocomplete component as below:
<Autocomplete
id="combo-box-demo"
classes={classes}
options={searchItems}
getOptionLabel={(option) =>
option.title ? option.title : option.name
}
style={{ width: 300, borderRight: "none", borderLeft: "none" }}
onChange={(e, value) => console.log(e.target, value.title)}
renderInput={(params) => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
/>
Here is an example that I have created. When the onChange function in the Autocomplete component is triggered, it displays values on the console. According to the doc, onchange function here pass three props called event, value and reason.

How to get searched Input in Material UI AutoComplete

I am using material ui in reactjs.
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => {
console.log(option)
return option.title
}
}
style={{ width: "100%" }}
onInputChange ={(event) => testMethod(event)}
renderInput={(params) => <TextField {...params} label="Search here" variant="outlined" />}
/>
I have Following Issues:
How to get the searched input value
To show dropdown only after typing search text
Filter options based on search input

How do I use Autocomplete component of Material UI with InputBase?

The params object dosen't seem to be working with InputBase. I also tried ref={params.inputProps}. Im using the googleplaces autocomplete
<Autocomplete
id="combo-box-demo"
options={autocomplete}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} />} // InputBase instead of TextField
/>
You just have to spread out the params. The params includes InputLabelProps and InputProps, so you have to separate those out from the rest, and spread the InputProps back in.
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => {
const {InputLabelProps,InputProps,...rest} = params;
return <InputBase {...params.InputProps} {...rest} />}}
/>
Working demo: https://codesandbox.io/s/material-demo-forked-6yhsk?file=/demo.tsx

Resources