From e7ccd70dccec48d59b54e23dbfabec2303eedb19 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 17 Aug 2022 22:12:55 -0400 Subject: [PATCH] fix(striker-ui): allow set messages in MessageGroup with regex --- striker-ui/components/MessageGroup.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/striker-ui/components/MessageGroup.tsx b/striker-ui/components/MessageGroup.tsx index c7b5664f..c6e8fe38 100644 --- a/striker-ui/components/MessageGroup.tsx +++ b/striker-ui/components/MessageGroup.tsx @@ -21,6 +21,7 @@ type MessageGroupProps = MessageGroupOptionalProps; type MessageGroupForwardedRefContent = { exists?: (key: string) => boolean; setMessage?: (key: string, message?: Message) => void; + setMessageRe?: (re: RegExp, message?: Message) => void; }; const MESSAGE_GROUP_DEFAULT_PROPS: Required = { @@ -50,6 +51,19 @@ const MessageGroup = forwardRef< return result; }); }, []); + const setMessageRe = useCallback((re: RegExp, message?: Message) => { + setMessages((previous) => { + const result = { ...previous }; + + Object.keys(previous).forEach((key: string) => { + if (re.test(key)) { + result[key] = message; + } + }); + + return result; + }); + }, []); const messageElements = useMemo( () => @@ -72,9 +86,10 @@ const MessageGroup = forwardRef< [defaultMessageType, messages], ); - useImperativeHandle(ref, () => ({ exists, setMessage }), [ + useImperativeHandle(ref, () => ({ exists, setMessage, setMessageRe }), [ exists, setMessage, + setMessageRe, ]); return <>{messageElements};