The MultiSelect Drop-Down Component
Learn how to create a reusable multi-select drop-down component with React, Formik, and react-select.
We'll cover the following...
We'll cover the following...
Just like the CheckboxGroup component, we may want to select multiple options in our drop-down. To do this, we can simply pass multiple={true} to the Field component.
<Field name={name} {...restOfProps} as="select" multiple={true}>{options.map((option) => (<option key={option.value} value={option.value}>{option.label}</option>))}</Field>
We then make the initial value for the field an array since we want to accept multiple options. But the default interface for a multiselect field is not quite usable and will need some customization. To do this, we’ll use the react-select library.
MultiSelect with react-select
We’ll create the MultiSelect component in the components/MultiSelect/index.js file.
.form-container {
background: white;
box-shadow: 0px 4px 0px #eff1f3;
border-radius: 20px;
max-width: 480px;
margin: 56px auto;
padding: 40px;
}
.header-text {
font-weight: 700;
font-size: 24px;
line-height: 120%;
text-align: center;
color: #231942;
}
.sub-text {
font-weight: 400;
font-size: 16px;
line-height: 24px;
color: #99a0ae;
text-align: center;
margin-top: 8px;
}
* {
font-family: Helvetica, Cambria, Droid Sans, Helvetica Neue, sans-serif;
box-sizing: border-box;
margin: 0;
padding: 0;
transition: 0.6s;
}
body {
background: #f6f8fa;
}
button {
outline: none;
border: none;
}
button[type="submit"] {
background: #67adc8;
border-radius: 8px;
margin-top: 48px;
width: 100%;
font-weight: 600;
font-size: 16px;
line-height: 24px;
color: #ffffff;
padding: 16px;
cursor: pointer;
}
button[type="submit"]:hover {
background: #3399c9;
}
SelectDropdown component with react-select library
In the onChange ...