fix(striker-ui): add onSet callback to MessageGroup

main
Tsu-ba-me 2 years ago
parent f15592f82f
commit 50452c49c0
  1. 56
      striker-ui/components/MessageGroup.tsx

@ -10,12 +10,13 @@ import {
import MessageBox, { Message, MessageBoxProps } from './MessageBox'; import MessageBox, { Message, MessageBoxProps } from './MessageBox';
type Messages = { type Messages = {
[messageKey: string]: Message | undefined; [messageKey: string]: Message;
}; };
type MessageGroupOptionalProps = { type MessageGroupOptionalProps = {
count?: number; count?: number;
defaultMessageType?: MessageBoxProps['type']; defaultMessageType?: MessageBoxProps['type'];
onSet?: (length: number) => void;
}; };
type MessageGroupProps = MessageGroupOptionalProps; type MessageGroupProps = MessageGroupOptionalProps;
@ -26,9 +27,13 @@ type MessageGroupForwardedRefContent = {
setMessageRe?: (re: RegExp, message?: Message) => void; setMessageRe?: (re: RegExp, message?: Message) => void;
}; };
const MESSAGE_GROUP_DEFAULT_PROPS: Required<MessageGroupOptionalProps> = { const MESSAGE_GROUP_DEFAULT_PROPS: Required<
Omit<MessageGroupOptionalProps, 'onSet'>
> &
Pick<MessageGroupOptionalProps, 'onSet'> = {
count: 0, count: 0,
defaultMessageType: 'info', defaultMessageType: 'info',
onSet: undefined,
}; };
const MessageGroup = forwardRef< const MessageGroup = forwardRef<
@ -39,6 +44,7 @@ const MessageGroup = forwardRef<
{ {
count = MESSAGE_GROUP_DEFAULT_PROPS.count, count = MESSAGE_GROUP_DEFAULT_PROPS.count,
defaultMessageType = MESSAGE_GROUP_DEFAULT_PROPS.defaultMessageType, defaultMessageType = MESSAGE_GROUP_DEFAULT_PROPS.defaultMessageType,
onSet,
}, },
ref, ref,
) => { ) => {
@ -48,28 +54,56 @@ const MessageGroup = forwardRef<
(key: string) => messages[key] !== undefined, (key: string) => messages[key] !== undefined,
[messages], [messages],
); );
const setMessage = useCallback((key: string, message?: Message) => { const setMessage = useCallback(
setMessages((previous) => { (key: string, message?: Message) => {
const result = { ...previous }; let length = 0;
setMessages(({ [key]: unused, ...rest }) => {
const result: Messages = rest;
if (message) {
result[key] = message; result[key] = message;
}
length = Object.keys(result).length;
return result; return result;
}); });
}, []);
const setMessageRe = useCallback((re: RegExp, message?: Message) => { onSet?.call(null, length);
},
[onSet],
);
const setMessageRe = useCallback(
(re: RegExp, message?: Message) => {
let length = 0;
const assignMessage = message
? (container: Messages, key: string) => {
container[key] = message;
length += 1;
}
: undefined;
setMessages((previous) => { setMessages((previous) => {
const result = { ...previous }; const result: Messages = {};
Object.keys(previous).forEach((key: string) => { Object.keys(previous).forEach((key: string) => {
if (re.test(key)) { if (re.test(key)) {
result[key] = message; assignMessage?.call(null, result, key);
} else {
result[key] = previous[key];
length += 1;
} }
}); });
return result; return result;
}); });
}, []);
onSet?.call(null, length);
},
[onSet],
);
const messageElements = useMemo(() => { const messageElements = useMemo(() => {
const pairs = Object.entries(messages); const pairs = Object.entries(messages);
@ -78,7 +112,6 @@ const MessageGroup = forwardRef<
const result: ReactNode[] = []; const result: ReactNode[] = [];
pairs.every(([messageKey, message]) => { pairs.every(([messageKey, message]) => {
if (message) {
const { children: messageChildren, type = defaultMessageType } = const { children: messageChildren, type = defaultMessageType } =
message; message;
@ -87,7 +120,6 @@ const MessageGroup = forwardRef<
{messageChildren} {messageChildren}
</MessageBox>, </MessageBox>,
); );
}
return result.length < limit; return result.length < limit;
}); });

Loading…
Cancel
Save