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<