user image

Jo Micheal
Published in : 2021-12-22

[solved] Render/Show a custom option in Autocomplete Material UI?

React js

How can i render a custom option of AutoComplete component in Material UI? i'd like to show the option with small text beside it using the span tag in HTML ? like the list below 

How can I handle this?

Comments

Mohamed Atef Date : 2021-12-22

Best answers

51

Best answers

51

Welcome Back, 
You can show custom render option in the AutoComplete in Material UI using renderOption with React.Fragment

like

 renderOption={(option) => ( <React.Fragment> <Markup content={option.name + `<span >${option.type}<span>`} /> </React.Fragment> )}

the entire code will looks like 

<Autocomplete filterOptions={filterOptions} options={this.state.dropdown} // getOptionLabel={(option) => { return option.title || '' } } groupBy={(option) => option.type} getOptionLabel={(option) => option.name || ''} renderOption={(option) => ( <React.Fragment> <Markup content={option.name + `<span >${option.type}<span>`} /> </React.Fragment> )} // closeIcon= { () => { return; } } renderInput={(params) => ( <TextField {...params} placeholder="" value={values.search} label="" name=search /> )} onChange={ (e, obj) => { console.log(obj); setFieldValue('search', obj); } }/>

Let me know if you have another questions

 

Jo Micheal Date : 2021-12-22

Thank you so much!

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

TypeError: Cannot read properties of null (reading 'scrollTop') in jest
Publish date: 2022-09-08 | Comments: 1

Tag: React js

Module not found: Can't resolve 'swiper' in Reactjs [Solved]
Publish date: 2021-10-03 | Comments: 1

Tag: React js

React Component setState doesn't update my property
Publish date: 2022-03-29 | Comments: 2

Tag: React js

npm install @mui/material returns error
Publish date: 2022-04-02 | Comments: 1

Tag: React js

[Solved] Cannot read properties of null (reading 'getBoundingClientRect')
Publish date: 2021-12-02 | Comments: 2

Tag: React js