🎉
Windframe Form builder is finally here.
postImage

How to create a beautiful React multiselect.

blogImage

By Emmanuel Chinonso

Web Developer

React Bootstrap Multi-selects are a form control that displays a collapsable list of multiple values after the click. This is especially important in creating forms, menus or surveys. They enable you to use and arrow keys to navigate through options and use the key to select the required option.

We will look at creating 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 the multi-select drop-down with the search, you need to download the pro version here.

Prerequisites

The react multi-select drop-down with a 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.

React Multiselect

Setup

To check that you have node installed. To do this, run the following command in your terminal.

Code:

r
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:

r
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

r
npm install --save ./path-to-the-cdbreact-pro-tgz-file

Or using Yarn

r
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:

r
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.

js
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,
},
];

We have the search prop included in the code below, which you can change if you wish.

r
return (
<CDBContainer>
<CBDMultiselect
search
color="secondary"
options={option}
selected="value"
/>
</CDBContainer>
);
};
export default Multiselect;

After you have styled and created the multiselect with search component, you can move to your app.js file and render the component on the App component.

Code:

r
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

React Bootstrap Multiselect

Now we have created the React Multi-select dropdown with a search using contrast.

Resources

CDBReact Multiselect Docs

Link to code on github

Get Contrast Pro

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Download the free react template

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...