Open In App

ReactJS UI Ant Design AutoComplete Component

Last Updated : 21 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Ant Design Library has this component pre-built, and it is very easy to integrate as well. AutoComplete Component is used for auto-completing the free text value with the option value. We can use the following approach in ReactJS to use the Ant Design AutoComplete Component.

AutoComplete Methods:

  • blur(): This method is used to remove the focus from the element.
  • focus(): This method is used to get the focus on the element.

AutoComplete Props:

  • allowClear: It is used to show the clear button.
  • autoFocus: It is used to get the focus when the component is mounted.
  • backfill: If this value is true, then it back fills the selected item the input when using the keyboard.
  • children(for customize input element): It is used to customize the input element.
  • children(for dataSource): It is used for the data source to auto-complete.
  • defaultActiveFirstOption: It is used to denote whether the first option is to be active by default or not.
  • defaultOpen: It is used to indicate the initial open state of the dropdown.
  • defaultValue: It is used to indicate the initially selected option.
  • disabled: It is used to disable the select.
  • dropdownClassName: It is used to pass the class name for the styling of the dropdown menu.
  • dropdownMatchSelectWidth: It is used to determine whether the select input and the dropdown menu are of the same width or not.
  • filterOption: It is used to apply the filter options against it if this is set to true.
  • notFoundContent: It is used to show the content when no result matches.
  • open: It is used to control the open state of the dropdown.
  • options: It is used to pass the select options.
  • placeholder: It is used to indicate the placeholder of input.
  • value: It is used to denote the selected option value.
  • onBlur: It is a callback function that is triggered on leaving the component.
  • onChange: It is a callback function that is triggered on the selection of an option or input value change.
  • onDropdownVisibleChange: It is a callback function that is triggered when the dropdown is open.
  • onFocus: It is a callback function that is triggered when entering the component.
  • onSearch: It is a callback function that is triggered when searching items.
  • onSelect: It is a callback function that is triggered on the selection of an option.

Creating React Application And Installing Module:

  • Step 1: Create a React application using the following command:

    npx create-react-app foldername
  • Step 2: After creating your project folder i.e. foldername, move to it using the following command:

    cd foldername
  • Step 3: After creating the ReactJS application, Install the required module using the following command:

    npm install antd

Project Structure: It will look like the following.

Project Structure

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

App.js




import React, { useState } from 'react'
import "antd/dist/antd.css";
import { AutoComplete } from 'antd';
  
export default function App() {
  
  const [currentValue, setCurrentValue] = useState('')
  const options = [
    {label: 'One', value: 'One'}, 
    {label: 'Two', value: 'Two'},
    {label: 'Three', value: 'Three'}, 
    {label: 'Four', value: 'Four'},
    {label: 'Five', value: 'Five'}
  ]
  
  return (
    <div style={{ display: 'block',
                  width: 700, padding: 30 }}>
      <h4>ReactJS Ant-Design AutoComplete Component</h4>
      <AutoComplete
        options={options}
        style={{ width: 200 }}
        onSelect={(value)=> {
            setCurrentValue(value)
        }}
        placeholder="Enter your text"
      /> <br />
  
      <p>Selected Value {`${currentValue}`} </p>
  
    </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://ant.design/components/auto-complete/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads