Open In App

Multi Select Search Using ReactJS

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
1 Like
Like
Report

Multi-Select Search Using ReactJS enables users to search for and select multiple items from a list simultaneously, enhancing usability and efficiency in interfaces like forms or tag selection. React interviews often include challenges related to building interactive and user-friendly components. One such common task is creating a multi-select search functionality. This article will guide you through a practical example of implementing a multi-select search component using React.

Prerequisites:

Approach to implement Multi Select Search:

The goal is to create a multi-select search component where users can type a query, see suggestions, and select multiple options. The selected options will be displayed as pills, and users can remove them individually.

Steps to Create the Project:

  • Step 1: Set Up Your React App with Vite:
npm create vite@latest
  • Step 2: Navigate to the Project Directory
cd multi-select-search
  • Step 3: Install the package dependency.
npm install

Project Structure:

Screenshot-2024-02-09-185123
project structure

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"web-vitals": "^2.1.4"
}
CSS
/* App.css  */
body {
  font-family: sans-serif;
}

.user-search-container {
  display: flex;
  position: relative;
}

.user-search-input {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 20px;
}

.user-search-input input {
  border: none;
  height: 20px;
  padding: 5px;
}

.user-search-input input:focus {
  outline: none;
}

.suggestions-list {
  max-height: 300px;
  overflow-y: scroll;
  list-style: none;
  padding: 0;
  margin: 0;
  position: absolute;
  background-color: #fff;
  border: 1px solid #ccc;
}

.suggestions-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid #ccc;
}

.suggestions-list li:last-child {
  border-bottom: none;
}

.suggestions-list li:hover {
  background-color: #ccc;
}

.suggestions-list li img {
  height: 20px;
}

.user-pill {
  height: 20px;
  display: flex;
  align-items: center;
  gap: 5px;
  background-color: black;
  color: #fff;
  padding: 5px 10px;
  border-radius: 16px;
  cursor: pointer;
}

.user-pill img {
  height: 100%;
}
.suggestions-list li.active {
  background-color: #ccc;
}
JavaScript JavaScript JavaScript

Steps to run the application:

npm run dev

Output:


Next Article
Article Tags :

Similar Reads