From e951cc8e835d29019de5bade60a44bb713dca7a4 Mon Sep 17 00:00:00 2001 From: JDMathew Date: Mon, 15 Jul 2024 17:37:30 -0700 Subject: [PATCH 1/3] add default value to be used when suppressing error --- packages/forms/src/components/Form.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/forms/src/components/Form.tsx b/packages/forms/src/components/Form.tsx index 37139cb0..4915d0a2 100644 --- a/packages/forms/src/components/Form.tsx +++ b/packages/forms/src/components/Form.tsx @@ -123,6 +123,12 @@ export type FormRef = { export const FormContext = React.createContext(null); +const DEFAULT_CONTEXT_VALUE: FormContextValue = { + refs: [], + submitForm: () => Promise.resolve(), + focusField: () => {}, +}; + export const useForm = () => { const context = React.useContext(FormContext); if (!context) { From f86313ae501e094010ad533a14843ac93d2f8031 Mon Sep 17 00:00:00 2001 From: JDMathew Date: Mon, 15 Jul 2024 17:38:15 -0700 Subject: [PATCH 2/3] Add suppress error option to useForm hook --- packages/forms/src/components/Form.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/forms/src/components/Form.tsx b/packages/forms/src/components/Form.tsx index 4915d0a2..2b1de013 100644 --- a/packages/forms/src/components/Form.tsx +++ b/packages/forms/src/components/Form.tsx @@ -129,8 +129,15 @@ const DEFAULT_CONTEXT_VALUE: FormContextValue = { focusField: () => {}, }; -export const useForm = () => { +export const useForm = ( + { suppressError }: { suppressError?: boolean } = { suppressError: false }, +) => { const context = React.useContext(FormContext); + + if (!context && suppressError) { + return DEFAULT_CONTEXT_VALUE; // return default values so internal useForm hooks don't throw undefined errors when user choose to suppress errors + } + if (!context) { __DEV__ && console.error( @@ -140,5 +147,6 @@ export const useForm = () => { 'useForm must be used within a FormContextProvider, please wrap your form field inside the
component', ); } + return context; }; From e8213e204eb4dbd43503db7a46bfa6292192d45b Mon Sep 17 00:00:00 2001 From: JDMathew Date: Tue, 16 Jul 2024 16:57:14 -0700 Subject: [PATCH 3/3] add suppressError to useFormField --- packages/forms/src/hooks/useFormField.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/forms/src/hooks/useFormField.ts b/packages/forms/src/hooks/useFormField.ts index ff3b79b2..36a7c196 100644 --- a/packages/forms/src/hooks/useFormField.ts +++ b/packages/forms/src/hooks/useFormField.ts @@ -17,6 +17,7 @@ export type UseFormField = { hasValidation: boolean; hasError?: boolean; errorMessage?: string; + suppressError?: boolean; }; export const useFormField = ({ @@ -30,9 +31,10 @@ export const useFormField = ({ accessibilityHint, errorMessage, editable = true, + suppressError, style = {}, }: UseFormField) => { - const { refs, submitForm, focusField } = useForm(); + const { refs, submitForm, focusField } = useForm({ suppressError }); const fieldRef = React.useRef(ref); const checks = __DEV__ ? useChecks?.() : undefined;