DEV Community

Cover image for How to create a beautiful multiselect using Contrast in React.
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to create a beautiful multiselect using Contrast in React.

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

Alt Text

Setup

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

Code:

node-v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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,
    },
];
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

The page we created should look like the image below

Alt Text

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

Resources

CDBReact Multiselect Docs

Link to code on github

Get Contrast Pro

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 Design Boostrap

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

Top comments (0)