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';
      
            export default function App() {
              const [basicValue, setBasicValue] = useState('');
              const data = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];

              return (
                <MDBAutocomplete
                  inputValue={basicValue}
                  setInputValue={setBasicValue}
                  label='Example label'
                  dataFilter={(value: any) => {
                    return data.filter((item) => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  }}
                />
              );
            }
          
        
    

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';
      
            export default function App() {
              const [displayValue, setDisplayValue] = useState('');
              const dataAdvanced = [
                { 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' },
              ];

              return (
                <MDBAutocomplete
                  inputValue={displayValue}
                  setInputValue={setDisplayValue}
                  label='Example label'
                  displayValue={(value: any) => value.title}
                  dataFilter={(value: any) => {
                    return dataAdvanced.filter((item) => {
                      return item.title.toLowerCase().startsWith(value.toLowerCase());
                    });
                  }}
                />
              );
            }
          
        
    


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';
      
            export default function App() {
              const data = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
              const [holdValue, setHoldValue] = useState('');

              return (
                <MDBAutocomplete
                  inputValue={holdValue}
                  setInputValue={setHoldValue}
                  label='Example label'
                  threshold={2}
                  dataFilter={(value: any) => {
                    return data.filter((item) => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  }}
                />
              );
            }
          
        
    

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';
      
            export default function App() {
              const [customIValue, setCustomIValue] = useState('');
              const dataAdvanced = [
                { 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' },
              ];

              return (
                <MDBAutocomplete
                  inputValue={customIValue}
                  setInputValue={setCustomIValue}
                  label='Example label'
                  dataFilter={(value: any) => {
                    return dataAdvanced.filter((item) => {
                      return item.title.toLowerCase().startsWith(value.toLowerCase());
                    });
                  }}
                  displayValue={(value: any) => value.title}
                  itemContent={(result: any) => (
                    <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';
      
            export default function App() {
              const data = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
              const [customCValue, setCustomCValue] = useState('');
              const [searchResult, setSearchResult] = useState(data.length);

              return (
                <MDBAutocomplete
                  inputValue={customCValue}
                  setInputValue={setCustomCValue}
                  label='Example label'
                  getFilteredData={(data: Array<any>) => {
                    setSearchResult(data.length);
                  }}
                  dataFilter={(value: any) => {
                    return data.filter((item) => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  }}
                  customContent={<div className='autocomplete-custom-content'>Search results: {searchResult}</div>}
                />
              );
            }
          
        
    
        
            
            .autocomplete-custom-content {
              padding: 6.5px 16px;
            }
          
        
    

Validation

Looks good!
Input value is required
        
            
            import React, { useState } from 'react';
            import { MDBAutocomplete, MDBValidation, MDBBtn } from 'mdb-react-ui-kit';
      
            export default function App() {
              const data = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
              const [validationValue, setValidationValue] = useState('');

              return (
                <MDBValidation noValidate>
                  <MDBAutocomplete
                    inputValue={validationValue}
                    setInputValue={setValidationValue}
                    validation='Looks good!'
                    required
                    label='Example label'
                    dataFilter={(value: any) => {
                      return data.filter((item) => {
                        return item.toLowerCase().startsWith(value.toLowerCase());
                      });
                    }}
                  />
                  <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
className String '' Add custom class to the MDBAutocomplete <MDBAutocomplete className="class" />
customContent Element - Custom HTML template that will be displayed at the end of the component dropdown <MDBAutocomplete customContent={element} />
dataFilter Function - Function that returns filtered data to the component <MDBAutocomplete dataFilter={(value: any) => { return data.filter((item) => { return item.toLowerCase().startsWith(value.toLowerCase()); }); }} />
displayValue Function - Function executed for complex search results, to get value to display in the results list <MDBAutocomplete displayValue={(value: any) => value.title} />
inputValue String - Stores value of the MDBAutocomplete input <MDBAutocomplete inputValue={input} />
itemContent Function - Function that returns custom template for result item <MDBAutocomplete itemContent={customContent} />
setInputValue String - Set value of the MDBAutocomplete input <MDBAutocomplete setInputValue={setInput} />
threshold Number 0 Minimum input length to start search query <MDBAutocomplete threshold={3} />
tag String 'div' Defines a tag type for the MDBAutocomplete <MDBAutocomplete tag='span' />