From 4557e27e1b80185ad72935f2448c3eaf9742e653 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 30 Mar 2023 17:06:06 -0400 Subject: [PATCH] fix(striker-ui): add function to set form validity by regex --- striker-ui/hooks/useFormUtils.ts | 23 ++++++++++++++++++++++- striker-ui/types/FormUtils.d.ts | 3 ++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/striker-ui/hooks/useFormUtils.ts b/striker-ui/hooks/useFormUtils.ts index f866bf1d..0839b5de 100644 --- a/striker-ui/hooks/useFormUtils.ts +++ b/striker-ui/hooks/useFormUtils.ts @@ -16,12 +16,32 @@ const useFormUtils = < ): FormUtils => { const [formValidity, setFormValidity] = useState>({}); - const setValidity = useCallback((key: keyof M, value: boolean) => { + const setValidity = useCallback((key: keyof M, value?: boolean) => { setFormValidity( buildObjectStateSetterCallback>(key, value), ); }, []); + const setValidityRe = useCallback((re: RegExp, value?: boolean) => { + setFormValidity((previous) => { + const result: FormValidity = {}; + + Object.keys(previous).forEach((key) => { + const id = key as keyof M; + + if (re.test(key)) { + if (value !== undefined) { + result[id] = value; + } + } else { + result[id] = previous[id]; + } + }); + + return result; + }); + }, []); + const buildFinishInputTestBatchFunction = useCallback( (key: keyof M) => (result: boolean) => { setValidity(key, result); @@ -66,6 +86,7 @@ const useFormUtils = < setFormValidity, setMsgSetter, setValidity, + setValidityRe, }; }; diff --git a/striker-ui/types/FormUtils.d.ts b/striker-ui/types/FormUtils.d.ts index 5a665490..5af8aac0 100644 --- a/striker-ui/types/FormUtils.d.ts +++ b/striker-ui/types/FormUtils.d.ts @@ -28,5 +28,6 @@ type FormUtils = { setter?: MessageSetterFunction, isOverwrite?: boolean, ) => void; - setValidity: (key: keyof M, value: boolean) => void; + setValidity: (key: keyof M, value?: boolean) => void; + setValidityRe: (re: RegExp, value?: boolean) => void; };