...

/

The DateInput Component

The DateInput Component

Learn how to create a reusable date picker component with React, Formik, and the react-datepicker library.

When we want our user to select a date, we make use of the date input field. Clicking this field displays a date picker, which has an appearance that depends on the browser and the operating system of the device. To have more control over the style of the date picker, we can use a JavaScript date picker library, one of which we’ll use later in this lesson. But for now, let’s see how to create a DateInput component with the default date input element.

We’ll write the code for our DateInput component in the components/DateInput/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;
}
Reusable date input component with default date input

Creating a date input field follows the same process as creating a text input field. The difference here is that the type prop we pass to the Field component takes the value, "date". We then update the FormikControl component in components/FormikControl/inde ...