From 6d54fca66693bde88ca76738372eb9532c6c824d Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 16 Aug 2022 00:16:33 -0400 Subject: [PATCH] fix(striker-ui): allow check if message key exists in MessageGroup --- striker-ui/components/MessageGroup.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/striker-ui/components/MessageGroup.tsx b/striker-ui/components/MessageGroup.tsx index cadfa321..c7b5664f 100644 --- a/striker-ui/components/MessageGroup.tsx +++ b/striker-ui/components/MessageGroup.tsx @@ -19,6 +19,7 @@ type MessageGroupOptionalProps = { type MessageGroupProps = MessageGroupOptionalProps; type MessageGroupForwardedRefContent = { + exists?: (key: string) => boolean; setMessage?: (key: string, message?: Message) => void; }; @@ -36,6 +37,10 @@ const MessageGroup = forwardRef< ) => { const [messages, setMessages] = useState({}); + const exists = useCallback( + (key: string) => messages[key] !== undefined, + [messages], + ); const setMessage = useCallback((key: string, message?: Message) => { setMessages((previous) => { const result = { ...previous }; @@ -67,7 +72,10 @@ const MessageGroup = forwardRef< [defaultMessageType, messages], ); - useImperativeHandle(ref, () => ({ setMessage }), [setMessage]); + useImperativeHandle(ref, () => ({ exists, setMessage }), [ + exists, + setMessage, + ]); return <>{messageElements}; },