Multi-select is a form control, that after the click displays a collapsable list of multiple values. This is especially important in creating forms, menus or surveys. Multi-select enables you to use ↑ and ↓ arrow keys to navigate through options and use ↵ key to select required option.
We are going to look at how to create this multi-select drop-down with search today using a react library known as contrast.
Contrast also known as CDBReact is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites, and web apps. To use multi-select drop-down with search you need to download the pro version here.
Prerequisites
The multi-select drop-down with search would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:
- Basic React Knowledge
- JavaScript knowledge
- Basic Bootstrap knowledge
- NPM installed
The multi-select we are going to create in this tutorial is going to look like this
Setup
First Check that you have node installed. To do this, run the following command in your terminal.
Code:
node-v
This should show you the current version of node you have installed on your machine.
If you don’t have nodejs installed, download it here..
Installing node also installs npm on your PC but you can still confirm using npm-v. Now that we have node installed, we can start up our React project by going to the directory of our choice and running
Code:
npx create-react-app multiselect-app
I named the project multiselect-app but you can use any name of your choice.
Install CDBReact
Now, we have to install CDBReact in our project
Run the following command to install CBDReact
npm install --save ./path-to-the-cdbreact-pro-tgz-file
Or using Yarn
yarn add ./path-to-the-cdbreact-pro-tgz-file
Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.
multi select dropdown with search
we can now create a file named multiselect.js. This is where we are going to import the multi-select and container component. This is also where we will be writing our code.
Import the multi-select and container components from CDBReact
Code:
import React from "react";
import { CDBMultiselect, CDBContainer } from "cdbreact-pro";
in the code below, we set up the features and styles for the multi-select.
export const Multiselect = () => {
// "showing" key:value pair optional
const option = [
{
text: "Value 1",
showing: true,
},
{
text: "Second Value",
showing: true,
},
{
text: "Third Value",
showing: true,
},
{
text: "Final Value",
showing: true,
},
];
In the code below, we have the search prop included in which you can change if you wish.
return (
<CDBContainer>
<CBDMultiselect
search
color="secondary"
options={option}
selected="value"
/>
</CDBContainer>
);
};
export default Multiselect;
After you have styled and created the multiselect component. You can move to your app.js file and render the component on the App component.
Code:
import './App.css';
import Multiselect from './Multiselect';
import Reactdom from 'react-dom';
function App() {
return (
<Router>
<div className="App">
<Multiselect />
</div>
</Router>
);
}
Reactdom.render(<App />, document.getElementById('root'))
The page we created should look like the image below
Now we have create the Multi-select dropdown with a search using contrast.
Resources
Create Stunning Websites and Web Apps
Building different custom components in react for your web apps or websites can get very stressful. Thats why we decided to build contrast. We have put together a UI kit with over 10000+ components, 5 admin dashboards and 23 additional different pages template for building almost any type of web app or webpage into a single product called Contrast Pro. Try contrast pro!
Contrast React Bootstrap PRO is a Multipurpose pro template, UI kit to build your next landing, admin, SAAS, prelaunch, etc. project with a clean, well documented, well crafted template and UI components. Learn more about Contrast Pro
Resources
Tailwind grid-How to use tailwind CSS grid templates in your project
How to create a Beautiful Responsive Navigation bar Using Tailwind CSS
Tailwind form-How to create and style a Responsive Form using Tailwind CSS
How to use tailwind CSS padding, margin and border in your project
How to create a beautiful React Bootstrap select with icons.
How to create a Beautiful Responsive Navigation bar Using Tailwind CSS
Tailwind Modal-How to create a React Modal using Tailwind CSS.
How to Implement a React Step Progress Bar Using Tailwind CSS
Top comments (0)