fix(striker-ui): limit messages to show in MessageGroup

main
Tsu-ba-me 2 years ago
parent 7eaa377f28
commit c63cedb315
  1. 6
      striker-ui/components/GeneralInitForm.tsx
  2. 59
      striker-ui/components/MessageGroup.tsx
  3. 6
      striker-ui/components/NetworkInitForm.tsx

@ -780,7 +780,11 @@ const GeneralInitForm = forwardRef<
</MUIGrid> </MUIGrid>
</MUIGrid> </MUIGrid>
</MUIGrid> </MUIGrid>
<MessageGroup defaultMessageType="warning" ref={messageGroupRef} /> <MessageGroup
count={1}
defaultMessageType="warning"
ref={messageGroupRef}
/>
{helpMessage && ( {helpMessage && (
<MessageBox <MessageBox
onClose={() => { onClose={() => {

@ -1,5 +1,6 @@
import { import {
forwardRef, forwardRef,
ReactNode,
useCallback, useCallback,
useImperativeHandle, useImperativeHandle,
useMemo, useMemo,
@ -13,6 +14,7 @@ type Messages = {
}; };
type MessageGroupOptionalProps = { type MessageGroupOptionalProps = {
count?: number;
defaultMessageType?: MessageBoxProps['type']; defaultMessageType?: MessageBoxProps['type'];
}; };
@ -25,6 +27,7 @@ type MessageGroupForwardedRefContent = {
}; };
const MESSAGE_GROUP_DEFAULT_PROPS: Required<MessageGroupOptionalProps> = { const MESSAGE_GROUP_DEFAULT_PROPS: Required<MessageGroupOptionalProps> = {
count: 0,
defaultMessageType: 'info', defaultMessageType: 'info',
}; };
@ -33,7 +36,10 @@ const MessageGroup = forwardRef<
MessageGroupProps MessageGroupProps
>( >(
( (
{ defaultMessageType = MESSAGE_GROUP_DEFAULT_PROPS.defaultMessageType }, {
count = MESSAGE_GROUP_DEFAULT_PROPS.count,
defaultMessageType = MESSAGE_GROUP_DEFAULT_PROPS.defaultMessageType,
},
ref, ref,
) => { ) => {
const [messages, setMessages] = useState<Messages>({}); const [messages, setMessages] = useState<Messages>({});
@ -65,26 +71,39 @@ const MessageGroup = forwardRef<
}); });
}, []); }, []);
const messageElements = useMemo( const messageElements = useMemo(() => {
() => const pairs = Object.entries(messages);
Object.entries(messages).map(([messageKey, message]) => { const isValidCount = count > 0;
let messageElement; const limit = isValidCount ? count : pairs.length;
const result: ReactNode[] = [];
if (message) {
const { children: messageChildren, type = defaultMessageType } = pairs.every(([messageKey, message]) => {
message; if (message) {
const { children: messageChildren, type = defaultMessageType } =
messageElement = ( message;
<MessageBox key={`message-${messageKey}`} type={type}>
{messageChildren} result.push(
</MessageBox> <MessageBox key={`message-${messageKey}`} type={type}>
); {messageChildren}
} </MessageBox>,
);
}
return result.length < limit;
});
return messageElement; if (isValidCount && result.length === 0) {
}), result.push(
[defaultMessageType, messages], <MessageBox
); key="message-placeholder"
sx={{ visibility: 'hidden' }}
text="Placeholder"
/>,
);
}
return result;
}, [count, defaultMessageType, messages]);
useImperativeHandle(ref, () => ({ exists, setMessage, setMessageRe }), [ useImperativeHandle(ref, () => ({ exists, setMessage, setMessageRe }), [
exists, exists,

@ -1237,7 +1237,11 @@ const NetworkInitForm = forwardRef<
ref={dnsCSVInputRef} ref={dnsCSVInputRef}
/> />
</FlexBox> </FlexBox>
<MessageGroup defaultMessageType="warning" ref={messageGroupRef} /> <MessageGroup
count={1}
defaultMessageType="warning"
ref={messageGroupRef}
/>
</MUIBox> </MUIBox>
</MUIBox> </MUIBox>
); );

Loading…
Cancel
Save