From 92f2c3626c4aebbef7a5202d0d79585d1391789a Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 3 Mar 2023 21:04:21 -0500 Subject: [PATCH] fix(striker-ui): organize types in useFormUtils hook --- striker-ui/hooks/useFormUtils.ts | 47 ++++++++++---------------------- striker-ui/types/FormUtils.d.ts | 27 ++++++++++++++++++ 2 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 striker-ui/types/FormUtils.d.ts diff --git a/striker-ui/hooks/useFormUtils.ts b/striker-ui/hooks/useFormUtils.ts index 8f71dfc3..70727b88 100644 --- a/striker-ui/hooks/useFormUtils.ts +++ b/striker-ui/hooks/useFormUtils.ts @@ -1,20 +1,9 @@ -import { - Dispatch, - MutableRefObject, - SetStateAction, - useCallback, - useMemo, - useState, -} from 'react'; +import { MutableRefObject, useCallback, useMemo, useState } from 'react'; import buildMapToMessageSetter from '../lib/buildMapToMessageSetter'; import buildObjectStateSetterCallback from '../lib/buildObjectStateSetterCallback'; import { MessageGroupForwardedRefContent } from '../components/MessageGroup'; -type FormValidity = { - [K in keyof T]?: boolean; -}; - const useFormUtils = < U extends string, I extends InputIds, @@ -22,37 +11,28 @@ const useFormUtils = < >( ids: I, messageGroupRef: MutableRefObject, -): { - buildFinishInputTestBatchFunction: ( - key: keyof M, - ) => (result: boolean) => void; - buildInputFirstRenderFunction: ( - key: keyof M, - ) => ({ isRequired }: { isRequired: boolean }) => void; - formValidity: FormValidity; - isFormInvalid: boolean; - msgSetters: MapToMessageSetter; - setFormValidity: Dispatch>>; -} => { +): FormUtils => { const [formValidity, setFormValidity] = useState>({}); + const setValidity = useCallback((key: keyof M, value: boolean) => { + setFormValidity( + buildObjectStateSetterCallback>(key, value), + ); + }, []); + const buildFinishInputTestBatchFunction = useCallback( (key: keyof M) => (result: boolean) => { - setFormValidity( - buildObjectStateSetterCallback>(key, result), - ); + setValidity(key, result); }, - [], + [setValidity], ); const buildInputFirstRenderFunction = useCallback( (key: keyof M) => - ({ isRequired }: { isRequired: boolean }) => { - setFormValidity( - buildObjectStateSetterCallback>(key, !isRequired), - ); + ({ isValid }: InputFirstRenderFunctionArgs) => { + setValidity(key, isValid); }, - [], + [setValidity], ); const isFormInvalid = useMemo( @@ -72,6 +52,7 @@ const useFormUtils = < isFormInvalid, msgSetters, setFormValidity, + setValidity, }; }; diff --git a/striker-ui/types/FormUtils.d.ts b/striker-ui/types/FormUtils.d.ts new file mode 100644 index 00000000..1f106121 --- /dev/null +++ b/striker-ui/types/FormUtils.d.ts @@ -0,0 +1,27 @@ +type FormValidity = { + [K in keyof T]?: boolean; +}; + +type InputTestBatchFinishCallbackBuilder = ( + key: keyof M, +) => InputTestBatchFinishCallback; + +type InputFirstRenderFunctionArgs = { isValid: boolean }; + +type InputFirstRenderFunction = (args: InputFirstRenderFunctionArgs) => void; + +type InputFirstRenderFunctionBuilder = ( + key: keyof M, +) => InputFirstRenderFunction; + +type FormUtils = { + buildFinishInputTestBatchFunction: InputTestBatchFinishCallbackBuilder; + buildInputFirstRenderFunction: InputFirstRenderFunctionBuilder; + formValidity: FormValidity; + isFormInvalid: boolean; + msgSetters: MapToMessageSetter; + setFormValidity: import('react').Dispatch< + import('react').SetStateAction> + >; + setValidity: (key: keyof M, value: boolean) => void; +};