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

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

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

Loading…
Cancel
Save