fix(striker-ui): add setMsgSetter in FormUtils

This commit is contained in:
Tsu-ba-me 2023-03-14 19:48:46 -04:00
parent 77abfdc933
commit ae07c8b66d
2 changed files with 19 additions and 1 deletions

View File

@ -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<M>(String(id), messageGroupRef);
}
},
[messageGroupRef, msgSetters],
);
return {
buildFinishInputTestBatchFunction,
buildInputFirstRenderFunction,
@ -52,6 +64,7 @@ const useFormUtils = <
isFormInvalid,
msgSetters,
setFormValidity,
setMsgSetter,
setValidity,
};
};

View File

@ -23,5 +23,10 @@ type FormUtils<M extends MapToInputTestID> = {
setFormValidity: import('react').Dispatch<
import('react').SetStateAction<FormValidity<M>>
>;
setMsgSetter: (
id: keyof M,
setter?: MessageSetterFunction,
isOverwrite?: boolean,
) => void;
setValidity: (key: keyof M, value: boolean) => void;
};