|
|
@ -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, |
|
|
|