...

/

The Input Field Component

The Input Field Component

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

We'll cover the following...

We’ll create a reusable input field component. For the input field, we’re interested in the label, the field itself, and the error message for errors.

We’ll create a components/InputField/index.js file to write the code for our InputField component.

.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 InputField component

We make the InputField component accept a number of props including name and label.

In the InputField component, we render the label, the field, and the error message. The value of the label is obtained from the props. Then we pass the name prop to the Field component. We also pass the remaining props from InputField to the Field component. This will allow us to pass props like type, placeholder, and so on.

Then we use the ErrorMessage component to display error messages for the field. We make this component render the ...