From ae07c8b66d96ed9ee69b9baed52c3f034d28cfc3 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 14 Mar 2023 19:48:46 -0400 Subject: [PATCH] fix(striker-ui): add setMsgSetter in FormUtils --- striker-ui/hooks/useFormUtils.ts | 15 ++++++++++++++- striker-ui/types/FormUtils.d.ts | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/striker-ui/hooks/useFormUtils.ts b/striker-ui/hooks/useFormUtils.ts index 70727b88..f866bf1d 100644 --- a/striker-ui/hooks/useFormUtils.ts +++ b/striker-ui/hooks/useFormUtils.ts @@ -1,6 +1,8 @@ import { MutableRefObject, useCallback, useMemo, useState } from 'react'; -import buildMapToMessageSetter from '../lib/buildMapToMessageSetter'; +import buildMapToMessageSetter, { + buildMessageSetter, +} from '../lib/buildMapToMessageSetter'; import buildObjectStateSetterCallback from '../lib/buildObjectStateSetterCallback'; import { MessageGroupForwardedRefContent } from '../components/MessageGroup'; @@ -45,6 +47,16 @@ const useFormUtils = < [ids, messageGroupRef], ); + const setMsgSetter = useCallback( + (id: keyof M, setter?: MessageSetterFunction, isOverwrite?: boolean) => { + if (!msgSetters[id] || isOverwrite) { + msgSetters[id] = + setter ?? buildMessageSetter(String(id), messageGroupRef); + } + }, + [messageGroupRef, msgSetters], + ); + return { buildFinishInputTestBatchFunction, buildInputFirstRenderFunction, @@ -52,6 +64,7 @@ const useFormUtils = < isFormInvalid, msgSetters, setFormValidity, + setMsgSetter, setValidity, }; }; diff --git a/striker-ui/types/FormUtils.d.ts b/striker-ui/types/FormUtils.d.ts index 1f106121..5a665490 100644 --- a/striker-ui/types/FormUtils.d.ts +++ b/striker-ui/types/FormUtils.d.ts @@ -23,5 +23,10 @@ type FormUtils = { setFormValidity: import('react').Dispatch< import('react').SetStateAction> >; + setMsgSetter: ( + id: keyof M, + setter?: MessageSetterFunction, + isOverwrite?: boolean, + ) => void; setValidity: (key: keyof M, value: boolean) => void; };