fix(striker-ui): allow set messages in MessageGroup with regex

main
Tsu-ba-me 2 years ago
parent 45a6beff7d
commit e7ccd70dcc
  1. 17
      striker-ui/components/MessageGroup.tsx

@ -21,6 +21,7 @@ type MessageGroupProps = MessageGroupOptionalProps;
type MessageGroupForwardedRefContent = { type MessageGroupForwardedRefContent = {
exists?: (key: string) => boolean; exists?: (key: string) => boolean;
setMessage?: (key: string, message?: Message) => void; setMessage?: (key: string, message?: Message) => void;
setMessageRe?: (re: RegExp, message?: Message) => void;
}; };
const MESSAGE_GROUP_DEFAULT_PROPS: Required<MessageGroupOptionalProps> = { const MESSAGE_GROUP_DEFAULT_PROPS: Required<MessageGroupOptionalProps> = {
@ -50,6 +51,19 @@ const MessageGroup = forwardRef<
return result; 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( const messageElements = useMemo(
() => () =>
@ -72,9 +86,10 @@ const MessageGroup = forwardRef<
[defaultMessageType, messages], [defaultMessageType, messages],
); );
useImperativeHandle(ref, () => ({ exists, setMessage }), [ useImperativeHandle(ref, () => ({ exists, setMessage, setMessageRe }), [
exists, exists,
setMessage, setMessage,
setMessageRe,
]); ]);
return <>{messageElements}</>; return <>{messageElements}</>;

Loading…
Cancel
Save