From b517722a1d370cbd7457cf98866b806832d61680 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 13 Jan 2023 17:03:51 +0700 Subject: Formik and yup implement --- src/pages/my/address/create.js | 266 +++++++++++++++++++++++------------------ 1 file changed, 151 insertions(+), 115 deletions(-) (limited to 'src/pages') diff --git a/src/pages/my/address/create.js b/src/pages/my/address/create.js index 457429e3..f11a7b67 100644 --- a/src/pages/my/address/create.js +++ b/src/pages/my/address/create.js @@ -9,6 +9,9 @@ import useFormValidation from "../../../helpers/formValidation"; import { toast } from "react-hot-toast"; import { useRouter } from "next/router"; import _ from "lodash"; +import { Field, Form, Formik } from "formik"; +import * as Yup from "yup"; +import SelectFormik from "../../../components/SelectFormik"; const initialFormValue = { type: null, @@ -20,7 +23,13 @@ const initialFormValue = { district: null, subDistrict: null, zip: '' -} +}; + +const validationSchema = Yup.object().shape({ + name: Yup.string() + .min(2, 'Minimal karakter 2') + .required('Harus di-isi') +}); const validationScheme = { type: ['label:Label Alamat', 'required'], @@ -110,126 +119,153 @@ export default function CreateAddress() { router.back(); } } + + // useEffect(() => { + // }, [formInputs]); + // console.log(_.isEmpty(formInputs), formInputs); return ( - { !_.isEmpty(formInputs) && ( -
handleFormSubmit(e, onSubmit)}> - - handleSelectChange('type', value)} - value={formInputs?.type} - /> -
{formErrors?.type}
- - - -
{formErrors?.name}
- - - -
{formErrors?.email}
- - - -
{formErrors?.mobile}
- - - -
{formErrors?.street}
- - - -
{formErrors?.zip}
- - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={cities} - value={formInputs.city} - onChange={(value) => handleSelectChange('city', value)} - /> -
{formErrors?.city}
- - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={districts} - value={formInputs.district} - onChange={(value) => handleSelectChange('district', value)} - isDisabled={!formInputs.city} - /> - - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={subDistricts} - onChange={(value) => handleSelectChange('subDistrict', value)} - value={formInputs.subDistrict} - isDisabled={!formInputs.district} - /> - - - - ) } + + {({ errors, touched }) => ( +
+ + + + + + { errors.name && touched.name && ( +
{errors.name}
+ ) } + + + + { errors.email && touched.email && ( +
{errors.email}
+ ) } + + )} +
+ +
handleFormSubmit(e, onSubmit)}> + { JSON.stringify(formInputs) } + + handleSelectChange('type', value)} + value={formInputs?.type} + /> +
{formErrors?.type}
+ + + +
{formErrors?.name}
+ + + +
{formErrors?.email}
+ + + +
{formErrors?.mobile}
+ + + +
{formErrors?.street}
+ + + +
{formErrors?.zip}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={cities} + value={formInputs.city} + onChange={(value) => handleSelectChange('city', value)} + /> +
{formErrors?.city}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={districts} + value={formInputs.district} + onChange={(value) => handleSelectChange('district', value)} + isDisabled={!formInputs.city} + /> + + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={subDistricts} + onChange={(value) => handleSelectChange('subDistrict', value)} + value={formInputs.subDistrict} + isDisabled={!formInputs.district} + /> + + +
); -- cgit v1.2.3