

Build a Stepper Form with validation using a NPM package [ formik-stepper ]
source link: https://dev.to/riyadelberkawy/build-a-stepper-form-with-validation-using-a-npm-package-formik-stepper-4oa
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

![Cover image for Build a Stepper Form with validation using a NPM package [ formik-stepper ]](https://res.cloudinary.com/practicaldev/image/fetch/s--UEZtfEzZ--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihz78o10e5v81ewq6h8s.png)
Build a Stepper Form with validation using a NPM package [ formik-stepper ]
Jul 23
・2 min read
React Formik Stepper Component
It's a custom form with Multiple Steps, This is a reusable and scalable React component based on the Formik library, By adding validation schema, It will not go to the next step unless the entries are validated in the current step, You can install it by npm install formik-stepper
and experiment the example in the Documentation.
GitHub repo and the Documentation
Example
import React from "react";
import * as Yup from "yup"
import { FormikStepper, FormikStep, InputField } from "formik-stepper";
const validationSchema = Yup.object().shape({
firstName: Yup.string().required("The First Name field is required"),
lastName: Yup.string().required("The Last Name field is required"),
email: Yup.string()
.email("The email must be a valid email address.")
.required("The Email field is required"),
password: Yup.string()
.required("The Password field is required")
.matches(
/^(?=.*[A-Za-z])(?=.*\d)(?=.*)[A-Za-z\d]{8,}$/,
`Must Contain 8 Characters, One Uppercase, One Lowercase,
One Number and one special case Character [@$!%*#?&-_]`
),
privacy: Yup.boolean()
.isTrue()
.oneOf([true], "The terms and conditions must be accepted."),
});
export const FormStepper = () => {
const onSubmit = async ( values, { setSubmitting } ) => {
console.log(values);
setSubmitting(false); //// Important
};
return(
<FormikStepper
/// Accept all Formik props
onSubmit={onSubmit} /// onSubmit Function
initialValues={{
firstName: "",
lastName: "",
email: "",
password: "",
privacy: false,
}}
validationSchema={validationSchema}
labelsColor="secondary" /// The text label color can be root variables or css => #fff
withStepperLine /// false as default and If it is false, it hides stepper line
nextBtnLabel="step" /// Next as default
prevBtnLabel="return" /// Prev as default
submitBtnLabel="Done" /// Submit as default
nextBtnColor="primary" /// as default and The color can be root variables or css => #fff
prevBtnColor="danger" /// as default and The color can be root variables or css => #fff
submitBtnColor="success" /// as default and The color can be root variables or css => #fff
>
{/* First Step */}
<FormikStep
label="Profile Info" /// The text label of Step
withIcons="fa fa-user" // to add icon into the circle must add icon as class Name like Fontawesome
withNumbers /// If true, it hides the icon and shows the step number
iconColor="white" /// The color can be root variables or css => #fff
circleColor="danger" /// The color can be root variables or css => #fff
>
<InputField name="firstName" label="First Name"></InputField>
<InputField name="lastName" label="Last Name" />
</FormikStep>
{/* Second Step */}
<FormikStep
label="Login Info"
withIcons="fa fa-lock"
iconColor="white"
circleColor="danger"
>
<InputField name="email" label="Email" type="email" />
<InputField name="password" label="password" type="password" />
<div>
<InputField name="privacy" label="privacy" type="checkbox" />
</div>
</FormikStep>
</FormikStepper>
);
);
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK