user

Jo Micheal

22 Dec 2021

[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

22 Dec 2021

Best Answer

best answer
githubgithubgithub

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 style="font-size: 10px !important;">${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 style="font-size: 10px !important;">${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

 

Replies

Jo Micheal

22 Dec 2021

Thank you so much!

© 2024 Copyrights reserved for web-brackets.com