...

/

The CheckboxGroup Component

The CheckboxGroup Component

Learn how to create a reusable checkbox component with React and Formik.

When we want our users to select just one option from multiple options, we use the RadioGroup component. But when we want them to select more than one element, then we use the checkbox component.

Using the CheckboxGroup component

The process of creating a checkbox component is almost the same as when creating a RadioGroup component. The difference is that with a CheckboxGroup component, we have to make a provision for the user to select multiple options. To do this, in the initialValues, we’ll use an array instead of a string.

We’ll write the code for the CheckboxGroup component in the components/CheckboxGroup/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;
}
The CheckboxGroup component

Let’s first take a look at the pages/register/index.js ...