fix(striker-ui): avoid depend on outdated message list in MessageGroup setters

main
Tsu-ba-me 2 years ago
parent d22b11bb90
commit b00afbe7cf
  1. 47
      striker-ui/components/MessageGroup.tsx

@ -62,20 +62,22 @@ const MessageGroup = forwardRef<
(key: string, message?: Message) => { (key: string, message?: Message) => {
let length = 0; let length = 0;
const { [key]: unused, ...rest } = messages; setMessages((previous) => {
const result: Messages = rest; const { [key]: unused, ...rest } = previous;
const result: Messages = rest;
if (message) { if (message) {
result[key] = message; result[key] = message;
} }
length = Object.keys(result).length; length = Object.keys(result).length;
onSet?.call(null, length); return result;
});
setMessages(result); onSet?.call(null, length);
}, },
[messages, onSet], [onSet],
); );
const setMessageRe = useCallback( const setMessageRe = useCallback(
(re: RegExp, message?: Message) => { (re: RegExp, message?: Message) => {
@ -87,22 +89,25 @@ const MessageGroup = forwardRef<
length += 1; length += 1;
} }
: undefined; : undefined;
const result: Messages = {};
setMessages((previous) => {
Object.keys(messages).forEach((key: string) => { const result: Messages = {};
if (re.test(key)) {
assignMessage?.call(null, result, key); Object.keys(previous).forEach((key: string) => {
} else { if (re.test(key)) {
result[key] = messages[key]; assignMessage?.call(null, result, key);
length += 1; } else {
} result[key] = previous[key];
length += 1;
}
});
return result;
}); });
onSet?.call(null, length); onSet?.call(null, length);
setMessages(result);
}, },
[messages, onSet], [onSet],
); );
const messageElements = useMemo(() => { const messageElements = useMemo(() => {

Loading…
Cancel
Save