Skip to content

Commit a43b1fe

Browse files
committed
chore(common): CHECKOUT-6970 Move form provider to context package
1 parent f94db4b commit a43b1fe

File tree

11 files changed

+28
-39
lines changed

11 files changed

+28
-39
lines changed

packages/locale/src/TranslatedString/TranslatedString.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createLanguageService } from '@bigcommerce/checkout-sdk';
22
import React from 'react';
33
import { create } from 'react-test-renderer';
44

5-
import { LocaleContext } from '../context';
5+
import { LocaleContext } from '../contexts';
66

77
import TranslatedString from './TranslatedString';
88

packages/locale/src/TranslatedString/TranslatedString.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TranslationData } from '@bigcommerce/checkout-sdk';
22
import React, { FunctionComponent } from 'react';
33

4-
import { useLocale } from '../context';
4+
import { useLocale } from '../contexts';
55

66
export interface TranslatedStringProps {
77
id: string;
File renamed without changes.

packages/locale/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { default as TranslatedString } from './TranslatedString/TranslatedString';
2-
export { LocaleContext, LocaleContextType, useLocale } from './context';
2+
export { LocaleContext, LocaleContextType, useLocale } from './contexts';

packages/payment-integration-api/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export { EmbeddedCheckoutUnsupportedError } from './errors';
22
export { default as CheckoutButtonResolveId } from './CheckoutButtonResolveId';
33
export { default as CheckoutButtonProps } from './CheckoutButtonProps';
4-
export { FormProvider } from './providers';
54
export { default as PaymentFormService } from './PaymentFormService';
65
export { default as PaymentMethodProps } from './PaymentMethodProps';
76
export { default as PaymentMethodResolveId } from './PaymentMethodResolveId';

packages/payment-integration-api/src/providers/FormProvider.tsx

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/payment-integration-api/src/providers/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/ui/src/form/Form/Form.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { Form as FormikForm, FormikFormProps } from 'formik';
44
import { values } from 'lodash';
55
import React, { createRef, FunctionComponent, memo, useCallback, useRef } from 'react';
66

7-
import { FormProvider } from '@bigcommerce/checkout/payment-integration-api';
8-
9-
import { FormContextType } from '../contexts';
7+
import { FormContextType, FormProvider } from '../contexts';
108

119
export interface FormProps extends FormikFormProps {
1210
testId?: string;
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
import { noop } from 'lodash';
2-
import { createContext } from 'react';
1+
import { isFunction, noop } from 'lodash';
2+
import React, { createContext, FunctionComponent, memo, ReactNode, useMemo, useState } from 'react';
33

44
export interface FormContextType {
55
isSubmitted: boolean;
66
setSubmitted(isSubmitted: boolean): void;
77
}
88

9-
const FormContext = createContext<FormContextType>({
9+
export const FormContext = createContext<FormContextType>({
1010
isSubmitted: false,
1111
setSubmitted: noop,
1212
});
1313

14+
type FormContextFunction = (props: FormContextType) => ReactNode;
15+
16+
export interface FormProviderProps {
17+
initialIsSubmitted?: boolean;
18+
children: ReactNode | FormContextFunction;
19+
}
20+
21+
export const FormProvider: FunctionComponent<FormProviderProps> = memo(
22+
({ children, initialIsSubmitted = false }) => {
23+
const [isSubmitted, setSubmitted] = useState(initialIsSubmitted);
24+
const contextValue = useMemo(() => ({ isSubmitted, setSubmitted }), [isSubmitted]);
25+
26+
return (
27+
<FormContext.Provider value={contextValue}>
28+
{isFunction(children) ? children({ isSubmitted, setSubmitted }) : children}
29+
</FormContext.Provider>
30+
);
31+
},
32+
);
33+
1434
export default FormContext;

0 commit comments

Comments
 (0)