Autocomplete

React Bootstrap 5 Autocomplete component

Autocomplete component predicts the words being typed based on the first few letters given by the user. You can search the list using basic scroll and the keyboard arrows

Note: Read the API tab to find all available options and advanced customization


Basic example

The dataFilter property is required in order for component to work properly. The option accepts a function that is expected to return an array of results or a Promise that resolves to an array of results.

        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete } from 'mdb-react-ui-kit';

            const defaultData = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
      
            export default function App() {
              const [data, setData] = useState(defaultData);

              const onSearch = (value: string) =>
                setData(defaultData.filter((item) => item.toLowerCase().startsWith(value.toLowerCase())));

              return (
                <MDBAutocomplete data={data} label='Example label' onSearch={onSearch} />
              );
            }
          
        
    

Display value

The displayValue property allow to separate oryginal result value from the value that will be displayed in the result list or input (after selection). Its useful when the data returned by the dataFilter function is an array of objects. You can specify which parameter of the object should be displayed.

        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete } from 'mdb-react-ui-kit';

            const defaultAdvanced = [
              { title: 'One', description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit' },
              { title: 'Two', description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit' },
              { title: 'Three', description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit' },
              { title: 'Four', description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit' },
              { title: 'Five', description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit' },
            ];
      
            export default function App() {
              const [advancedData, setAdvancedData] = useState(defaultAdvanced);

              const onAdvancedSearch = (value: string) =>
                setAdvancedData(defaultAdvanced.filter((item) => item.title.toLowerCase().startsWith(value.toLowerCase())));

              return (
                <MDBAutocomplete
                  data={advancedData}
                  label='Example label'
                  displayValue={(row) => row.title}
                  onSearch={onAdvancedSearch}
                />
              );
            }
          
        
    


Threshold

Use threshold property to specify a minimum number of the characters in the input field needed to perform a search operation.

        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete } from 'mdb-react-ui-kit';

            const defaultData = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
      
            export default function App() {
              const [thresholdData, setThresholdData] = useState(defaultData);

              const onThresholdSearch = (value: string) =>
              value.length > 1
                ? setThresholdData(defaultData.filter((item) => item.toLowerCase().startsWith(value.toLowerCase())))
                : setThresholdData([]);

              return (
                <MDBAutocomplete data={thresholdData} label='Example label' onSearch={onThresholdSearch} />
              );
            }
          
        
    

Custom item template

The itemContent property allow to display custom HTML in the result list.

        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete } from 'mdb-react-ui-kit';

            const customDefData = [
              { title: 'One', subtitle: 'Secondary text' },
              { title: 'Two', subtitle: 'Secondary text' },
              { title: 'Three', subtitle: 'Secondary text' },
              { title: 'Four', subtitle: 'Secondary text' },
              { title: 'Five', subtitle: 'Secondary text' },
              { title: 'Six', subtitle: 'Secondary text' },
            ];
      
            export default function App() {
              const [customData, setCustomData] = useState(customDefData);

              const onCustomSearch = (value: string) =>
                setCustomData(customDefData.filter((item) => item.title.toLowerCase().startsWith(value.toLowerCase())));

              return (
                <MDBAutocomplete
                  data={customData}
                  label='Example label'
                  displayValue={(row) => row.title}
                  onSearch={onCustomSearch}
                  itemContent={(result) => (
                    <div className='autocomplete-custom-item-content'>
                      <div className='autocomplete-custom-item-title'>{result.title}</div>
                      <div className='autocomplete-custom-item-subtitle'>{result.subtitle}</div>
                    </div>
                  )}
                />
              );
            }
          
        
    
        
            
            .autocomplete-custom-item-content {
              display: flex;
              flex-direction: column;
            }

            .autocomplete-custom-item-title {
              font-weight: 500;
            }

            .autocomplete-custom-item-subtitle {
              font-size: 0.8rem;
            }
          
        
    

Custom content

A custom content container with a class .autocomplete-custom-content will be displayed at the end of the autocomplete-custom-item-subtitle dropdown. You can use it to display a number of search results.

        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete } from 'mdb-react-ui-kit';

            
            const defaultData = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
      
            export default function App() {
              const [contentData, setContentData] = useState(defaultData);

              const onContentSearch = (value: string) =>
                setContentData(defaultData.filter((item) => item.toLowerCase().startsWith(value.toLowerCase())));

              return (
                <MDBAutocomplete
                  data={contentData}
                  label='Example label'
                  onSearch={onContentSearch}
                  customContent={<div className='autocomplete-custom-content'>Search results: {contentData.length}</div>}
                />
              );
            }
          
        
    
        
            
            .autocomplete-custom-content {
              padding: 6.5px 16px;
            }
          
        
    

Validation

Looks good!
Input value is required
        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete, MDBValidation, MDBValidationItem, MDBBtn } from 'mdb-react-ui-kit';
      
            const defaultData = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];

            export default function App() {
              const [data, setData] = useState(defaultData);

              const onSearch = (value: string) =>
                setData(defaultData.filter((item) => item.toLowerCase().startsWith(value.toLowerCase())));

              return (
                <MDBValidation>
                  <MDBValidationItem>
                     <MDBAutocomplete data={data} label='Example label' onSearch={onSearch} />
                  </MDBValidationItem>
    
                  <MDBBtn size='sm' className='mt-3' type='submit'>
                    Submit
                  </MDBBtn>
                </MDBValidation>
              );
            }
          
        
    

Autocomplete - API


Import

        
            
        import { MDBAutocomplete } from 'mdb-react-ui-kit';
        
        
    

Properties

MDBAutocomplete

Name Type Default Description Example
autoSelect boolean false Enables auto selecting on Tab press <MDBAutocomplete autoSelect />
customContent ReactNode - Custom HTML template that will be displayed at the end of the component dropdown <MDBAutocomplete customContent={element} />
data string[] | Record[] [] Autocomplete data state <MDBAutocomplete data={defaultData} />
listHeight string '190px' The autcomplete list height <MDBAutocomplete listHeight="200px" />
isLoading boolean false Enables input spinner <MDBAutocomplete isLoading={isLoading} />
noResults string 'No results found' Text displayed with no matching results <MDBAutocomplete noResults='-' />

Events

Name Type Description
displayValue (row: Record) => any Function executed for complex search results, to get value to display in the results list
itemContent (value: Record) => ReactNode Function that returns custom template for result item
onClose () => void Executed on autocomplete close
onOpen () => void Executed on autocomplete open
onSelect (value: string) => void Executed when option selected
onSearch (data: string) => void Executed when the autocomplete value changes