New Contrast Pro UI Kit and template with 5 admin dashboards, 23 pages and 10000+ components for $49 ($50 off)🎉
postImage

How to create a beautiful multiselect using Contrast in React.

blogImage

By Emmanuel Chinonso

Web Developer

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

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 cdbreact

Or using Yarn

yarn add cdbreact

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";

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;

Code:

import './App.css';
import Multiselect from './Multiselect';
function App() {
return (
<Router>
<div className="App">
<Multiselect />
</div>
</Router>
);
}
export default App;

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

ad-banner

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

Share Post:

Comments

...