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/components/SelectFormik.js | 18 +++ src/pages/my/address/create.js | 266 +++++++++++++++++++++++------------------ src/styles/globals.css | 13 +- 3 files changed, 181 insertions(+), 116 deletions(-) create mode 100644 src/components/SelectFormik.js (limited to 'src') diff --git a/src/components/SelectFormik.js b/src/components/SelectFormik.js new file mode 100644 index 00000000..4fd711c5 --- /dev/null +++ b/src/components/SelectFormik.js @@ -0,0 +1,18 @@ +import ReactSelect from "react-select"; + +const SelectFormik = ({ + options, + field, + form +}) => { + return option.value === field.value) : ''} + onChange={(option) => form.setFieldValue(field.name, option.value)} + onBlur={field.onBlur} + classNamePrefix={field} + /> +} + +export default SelectFormik; \ No newline at end of file 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} + /> + + +
); diff --git a/src/styles/globals.css b/src/styles/globals.css index e223036b..6c0a7607 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -124,7 +124,8 @@ html, body { .btn-yellow, .btn-light, - .btn-red { + .btn-red, + .btn-green { @apply block w-fit @@ -159,6 +160,16 @@ html, body { ; } + .btn-green { + @apply + bg-green_r-3 + border-green_r-6 + text-green_r-11 + disabled:text-green_r-10 + disabled:bg-green_r-6 + ; + } + .btn-light { @apply bg-gray_r-3 -- cgit v1.2.3