From f6b7a2df5a26aee5854e3b8a8214acf0431e8af1 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Sat, 1 Apr 2023 02:05:47 -0400 Subject: [PATCH] fix(striker-ui): add input unmount function builder to form utils --- striker-ui/hooks/useFormUtils.ts | 12 ++++++++++-- striker-ui/types/FormUtils.d.ts | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/striker-ui/hooks/useFormUtils.ts b/striker-ui/hooks/useFormUtils.ts index 0aeafd73..f0ae1ea3 100644 --- a/striker-ui/hooks/useFormUtils.ts +++ b/striker-ui/hooks/useFormUtils.ts @@ -18,14 +18,14 @@ const useFormUtils = < const setMessage = useCallback( (key: keyof M, message?: Message) => { - messageGroupRef.current.setMessage?.call(null, String(key), message); + messageGroupRef?.current?.setMessage?.call(null, String(key), message); }, [messageGroupRef], ); const setMessageRe = useCallback( (re: RegExp, message?: Message) => { - messageGroupRef.current.setMessageRe?.call(null, re, message); + messageGroupRef?.current?.setMessageRe?.call(null, re, message); }, [messageGroupRef], ); @@ -73,6 +73,13 @@ const useFormUtils = < [setValidity], ); + const buildInputUnmountFunction = useCallback( + (key: keyof M) => () => { + unsetKey(key); + }, + [unsetKey], + ); + const isFormInvalid = useMemo( () => Object.values(formValidity).some((isInputValid) => !isInputValid), [formValidity], @@ -81,6 +88,7 @@ const useFormUtils = < return { buildFinishInputTestBatchFunction, buildInputFirstRenderFunction, + buildInputUnmountFunction, formValidity, isFormInvalid, setFormValidity, diff --git a/striker-ui/types/FormUtils.d.ts b/striker-ui/types/FormUtils.d.ts index a33da4d0..5eed5d1b 100644 --- a/striker-ui/types/FormUtils.d.ts +++ b/striker-ui/types/FormUtils.d.ts @@ -14,9 +14,16 @@ type InputFirstRenderFunctionBuilder = ( key: keyof M, ) => InputFirstRenderFunction; +type InputUnmountFunction = () => void; + +type InputUnmountFunctionBuilder = ( + key: keyof M, +) => InputUnmountFunction; + type FormUtils = { buildFinishInputTestBatchFunction: InputTestBatchFinishCallbackBuilder; buildInputFirstRenderFunction: InputFirstRenderFunctionBuilder; + buildInputUnmountFunction: InputUnmountFunctionBuilder; formValidity: FormValidity; isFormInvalid: boolean; setFormValidity: import('react').Dispatch<