Hello Fellow Developers. Welcome to second blog of Material UI Series in this article we are gonna learn about how to use react Material UI’s AutoComplete.
First create a react application.
Install reacts Material UI Package. Use following command
npm install @mui/material @emotion/react @emotion/styled
now your app.js should look this code:-
import * as React from 'react'; import TextField from '@mui/material/TextField'; import Autocomplete from '@mui/material/Autocomplete'; export default function ComboBox() { return ( <Autocomplete disablePortal id="combo-box-demo" options={top8Films} sx={{ width: 300 }} renderInput={(params) => <TextField {...params} label="Movie" />} /> ); } const top8Films = [ { label: 'The Shawshank Redemption', year: 1994 }, { label: 'The Godfather', year: 1972 }, { label: 'The Godfather: Part II', year: 1974 }, { label: 'The Dark Knight', year: 2008 }, { label: '12 Angry Men', year: 1957 }, { label: "Schindler's List", year: 1993 }, { label: 'Pulp Fiction', year: 1994 }, { label: 'The Lord of the Rings: The Return of the King', year: 2003, } ]
That’s a normal autocomplete or a select tag with search option on. Now Material UI lets us take this code to next level we can create a lots of different outputs using Material UI’s tags and classes.
import * as React from 'react'; import TextField from '@mui/material/TextField'; import Autocomplete from '@mui/material/Autocomplete'; import Stack from '@mui/material/Stack'; export default function Playground() { const defaultProps = { options: top8Films, getOptionLabel: (option) => option.title, }; const flatProps = { options: top8Films.map((option) => option.title), }; const [value, setValue] = React.useState(null); return ( <Stack spacing={1} sx={{ width: 300 }}> <Autocomplete {...defaultProps} id="disable-close-on-select" disableCloseOnSelect renderInput={(params) => ( <TextField {...params} label="disableCloseOnSelect" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="clear-on-escape" clearOnEscape renderInput={(params) => ( <TextField {...params} label="clearOnEscape" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="disable-clearable" disableClearable renderInput={(params) => ( <TextField {...params} label="disableClearable" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="include-input-in-list" includeInputInList renderInput={(params) => ( <TextField {...params} label="includeInputInList" variant="standard" /> )} /> <Autocomplete {...flatProps} id="flat-demo" renderInput={(params) => ( <TextField {...params} label="flat" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="controlled-demo" value={value} onChange={(event, newValue) => { setValue(newValue); }} renderInput={(params) => ( <TextField {...params} label="controlled" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="auto-complete" autoComplete includeInputInList renderInput={(params) => ( <TextField {...params} label="autoComplete" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="disable-list-wrap" disableListWrap renderInput={(params) => ( <TextField {...params} label="disableListWrap" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="open-on-focus" openOnFocus renderInput={(params) => ( <TextField {...params} label="openOnFocus" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="auto-highlight" autoHighlight renderInput={(params) => ( <TextField {...params} label="autoHighlight" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="auto-select" autoSelect renderInput={(params) => ( <TextField {...params} label="autoSelect" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="disabled" disabled renderInput={(params) => ( <TextField {...params} label="disabled" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="disable-portal" disablePortal renderInput={(params) => ( <TextField {...params} label="disablePortal" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="blur-on-select" blurOnSelect renderInput={(params) => ( <TextField {...params} label="blurOnSelect" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="clear-on-blur" clearOnBlur renderInput={(params) => ( <TextField {...params} label="clearOnBlur" variant="standard" /> )} /> <Autocomplete {...defaultProps} id="select-on-focus" selectOnFocus renderInput={(params) => ( <TextField {...params} label="selectOnFocus" variant="standard" /> )} /> <Autocomplete {...flatProps} id="readOnly" readOnly defaultValue={flatProps.options[13]} renderInput={(params) => ( <TextField {...params} label="readOnly" variant="standard" /> )} /> </Stack> ); } const top8Films = [ { title: 'The Shawshank Redemption', year: 1994 }, { title: 'The Godfather', year: 1972 }, { title: 'The Godfather: Part II', year: 1974 }, { title: 'The Dark Knight', year: 2008 }, { title: '12 Angry Men', year: 1957 }, { title: "Schindler's List", year: 1993 }, { title: 'Pulp Fiction', year: 1994 }, { title: 'The Lord of the Rings: The Return of the King', year: 2003, }, ]
Here are some examples like disable close on select, clear on Escape, Disable clearable, flat, controlled and many more. Try this code in your code playgrounds. For creating more examples with Material UI’s AutoComplete please refer to this:- https://mui.com/material-ui/react-autocomplete/
Link to the previous blog: https://www.thecodehubs.com/react-material-ui/