83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import { withFormik } from "formik";
|
||
import InnerIdentityForm from "./InnerIdentityForm";
|
||
import * as yup from "yup";
|
||
|
||
export interface IdentityFormValues {
|
||
firstName: string;
|
||
lastName: string;
|
||
birthDate: string;
|
||
birthPlace: string;
|
||
fatherName: string;
|
||
gender: string;
|
||
nationalCode: string;
|
||
nationality: string;
|
||
profilePhotoId: string;
|
||
religion: string;
|
||
}
|
||
|
||
export interface WizardFormData {
|
||
identity: IdentityFormValues;
|
||
}
|
||
|
||
export interface IdentityFormProps {
|
||
step: number;
|
||
setStep: React.Dispatch<React.SetStateAction<number>>;
|
||
data: WizardFormData;
|
||
update: (newData: Partial<WizardFormData>) => void;
|
||
}
|
||
|
||
const IdentityFormValidationSchema = yup.object({
|
||
firstName: yup.string().trim().required("نام الزامی است").min(2).max(50),
|
||
lastName: yup.string().trim().required("نام خانوادگی الزامی است").min(2).max(50),
|
||
birthDate: yup
|
||
.string()
|
||
.required("تاریخ تولد الزامی است")
|
||
.matches(/^\d{4}\/\d{2}\/\d{2}$/, "فرمت تاریخ تولد باید به شکل ۱۴۰۳/۰۱/۲۰ باشد"),
|
||
birthPlace: yup.string().trim().required("محل تولد الزامی است").min(2).max(80),
|
||
fatherName: yup.string().trim().required("نام پدر الزامی است").min(2).max(50),
|
||
gender: yup
|
||
.string()
|
||
.required("جنسیت الزامی است")
|
||
.oneOf(["male", "female", "other"], "جنسیت معتبر نیست"),
|
||
nationalCode: yup
|
||
.string()
|
||
.required("کد ملی الزامی است")
|
||
.matches(/^\d{10}$/, "کد ملی باید ۱۰ رقم باشد"),
|
||
nationality: yup.string().trim().required("تابعیت الزامی است").min(2).max(50),
|
||
profilePhotoId: yup.string().trim().required("عکس پرسنلی الزامی است"),
|
||
religion: yup.string().trim().required("دین الزامی است").min(2).max(50),
|
||
});
|
||
|
||
const EMPTY_IDENTITY_VALUES: IdentityFormValues = {
|
||
firstName: "",
|
||
lastName: "",
|
||
birthDate: "",
|
||
birthPlace: "",
|
||
fatherName: "",
|
||
gender: "",
|
||
nationalCode: "",
|
||
nationality: "",
|
||
profilePhotoId: "",
|
||
religion: "",
|
||
};
|
||
|
||
const IdentityForm = withFormik<IdentityFormProps, IdentityFormValues>({
|
||
enableReinitialize: true,
|
||
|
||
mapPropsToValues: (props) => {
|
||
return props.data?.identity || EMPTY_IDENTITY_VALUES;
|
||
},
|
||
|
||
validationSchema: IdentityFormValidationSchema,
|
||
|
||
handleSubmit: (values, { props }) => {
|
||
props.update({
|
||
identity: values,
|
||
});
|
||
|
||
props.setStep((prev) => prev + 1);
|
||
},
|
||
})(InnerIdentityForm);
|
||
|
||
export default IdentityForm;
|