fix(striker-ui): allow check if message key exists in MessageGroup

main
Tsu-ba-me 2 years ago
parent df91749802
commit 6d54fca666
  1. 10
      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<Messages>({});
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}</>;
},

Loading…
Cancel
Save