Skip to content

Commit 83472f5

Browse files
committed
Use FormContext instead of passing down as props
1 parent 31f1d49 commit 83472f5

File tree

3 files changed

+62
-61
lines changed

3 files changed

+62
-61
lines changed

refactor/src/components/Checkout/Billing.component.tsx

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Imports
22
import {
3-
FieldValues,
43
SubmitHandler,
54
useForm,
6-
UseFormRegister,
5+
useFormContext,
6+
FormProvider,
77
} from 'react-hook-form';
88

99
// Components
@@ -12,54 +12,51 @@ import Button from '../UI/Button.component';
1212

1313
// Constants
1414
import { INPUT_FIELDS } from '@/utils/constants/INPUT_FIELDS';
15+
import { ICheckoutDataProps } from '@/utils/functions/functions';
1516

1617
interface IBillingProps {
17-
onSubmit: SubmitHandler<FieldValues>;
18+
handleFormSubmit: SubmitHandler<ICheckoutDataProps>;
1819
}
1920

20-
interface IOrderButtonProps {
21-
register: UseFormRegister<FieldValues>;
22-
}
21+
const OrderButton = () => {
22+
const { register } = useFormContext();
2323

24-
const OrderButton = ({ register }: IOrderButtonProps) => (
25-
<div className="w-full p-2">
26-
<input
27-
placeholder="paymentMethod"
28-
type="hidden"
29-
value="cod"
30-
checked
31-
{...register('paymentMethod')}
32-
/>
33-
<div className="mt-4 flex justify-center">
34-
<Button>BESTILL</Button>
24+
return (
25+
<div className="w-full p-2">
26+
<input
27+
placeholder="paymentMethod"
28+
type="hidden"
29+
value="cod"
30+
checked
31+
{...register('paymentMethod')}
32+
/>
33+
<div className="mt-4 flex justify-center">
34+
<Button>BESTILL</Button>
35+
</div>
3536
</div>
36-
</div>
37-
);
37+
);
38+
};
3839

39-
const Billing = ({ onSubmit }: IBillingProps) => {
40-
const {
41-
register,
42-
handleSubmit,
43-
formState: { errors },
44-
} = useForm();
40+
const Billing = ({ handleFormSubmit }: IBillingProps) => {
41+
const methods = useForm<ICheckoutDataProps>();
4542

4643
return (
4744
<section className="text-gray-700 container p-4 py-2 mx-auto">
48-
<form onSubmit={handleSubmit(onSubmit)}>
49-
<div className="mx-auto lg:w-1/2 flex flex-wrap">
50-
{INPUT_FIELDS.map(({ id, label, name, customValidation }) => (
51-
<InputField
52-
key={id}
53-
inputLabel={label}
54-
inputName={name}
55-
customValidation={customValidation}
56-
errors={errors}
57-
register={register}
58-
/>
59-
))}
60-
<OrderButton register={register} />
61-
</div>
62-
</form>
45+
<FormProvider {...methods}>
46+
<form onSubmit={methods.handleSubmit(handleFormSubmit)}>
47+
<div className="mx-auto lg:w-1/2 flex flex-wrap">
48+
{INPUT_FIELDS.map(({ id, label, name, customValidation }) => (
49+
<InputField
50+
key={id}
51+
inputLabel={label}
52+
inputName={name}
53+
customValidation={customValidation}
54+
/>
55+
))}
56+
<OrderButton />
57+
</div>
58+
</form>
59+
</FormProvider>
6360
</section>
6461
);
6562
};

refactor/src/components/Checkout/CheckoutForm.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const CheckoutForm = () => {
112112
refetch();
113113
}, [refetch]);
114114

115-
const onSubmit = (submitData: ICheckoutDataProps) => {
115+
const handleFormSubmit = (submitData: ICheckoutDataProps) => {
116116
const checkOutData = createCheckoutData(submitData);
117117

118118
setOrderData(checkOutData);
@@ -126,7 +126,7 @@ const CheckoutForm = () => {
126126
{/* Order*/}
127127
<CartContents />
128128
{/*Payment Details*/}
129-
<Billing onSubmit={onSubmit} />
129+
<Billing handleFormSubmit={handleFormSubmit} />
130130
{/*Error display*/}
131131
{requestError && (
132132
<div className="h-32 text-xl text-center text-red-600">
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FieldValues, UseFormRegister } from 'react-hook-form';
1+
import { FieldValues, useFormContext, UseFormRegister } from 'react-hook-form';
22

33
interface ICustomValidation {
44
required?: boolean;
@@ -11,8 +11,8 @@ export interface IInputRootObject {
1111
inputLabel: string;
1212
inputName: string;
1313
customValidation: ICustomValidation;
14-
errors: IErrors;
15-
register: UseFormRegister<FieldValues>;
14+
errors?: IErrors;
15+
register?: UseFormRegister<FieldValues>;
1616
type?: string;
1717
}
1818

@@ -31,20 +31,24 @@ export const InputField = ({
3131
customValidation,
3232
inputLabel,
3333
inputName,
34-
register,
3534
type,
36-
}: IInputRootObject) => (
37-
<div className="w-1/2 p-2">
38-
<label htmlFor={inputName} className="pb-4">
39-
{inputLabel}
40-
</label>
41-
<input
42-
className="bg-gray-50 border border-gray-300 text-gray-900 text-sm focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
43-
id={inputName}
44-
placeholder={inputLabel}
45-
type={type ?? 'text'}
46-
{...customValidation}
47-
{...register(inputName)}
48-
/>
49-
</div>
50-
);
35+
}: IInputRootObject) => {
36+
37+
const { register } = useFormContext();
38+
39+
return (
40+
<div className="w-1/2 p-2">
41+
<label htmlFor={inputName} className="pb-4">
42+
{inputLabel}
43+
</label>
44+
<input
45+
className="bg-gray-50 border border-gray-300 text-gray-900 text-sm focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
46+
id={inputName}
47+
placeholder={inputLabel}
48+
type={type ?? 'text'}
49+
{...customValidation}
50+
{...register(inputName)}
51+
/>
52+
</div>
53+
);
54+
};

0 commit comments

Comments
 (0)