From 9f1e9027fb80164bcb1ebf621e44ac4c21cc5e5b Mon Sep 17 00:00:00 2001 From: Melloware Date: Wed, 7 Sep 2022 08:15:37 -0400 Subject: [PATCH] Fix #3250: Messages/Toast fix replace method (#3251) --- components/lib/messages/Messages.js | 26 ++++++++++++++++++++-- components/lib/toast/Toast.js | 34 +++++++++++++++++++---------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/components/lib/messages/Messages.js b/components/lib/messages/Messages.js index fd31872692..4d3c2424c0 100644 --- a/components/lib/messages/Messages.js +++ b/components/lib/messages/Messages.js @@ -13,7 +13,7 @@ export const Messages = React.memo( const show = (value) => { if (value) { - let messages; + const messages = assignIdentifiers(value, true); if (Array.isArray(value)) { for (let i = 0; i < value.length; i++) { @@ -29,12 +29,34 @@ export const Messages = React.memo( } }; + const assignIdentifiers = (value, copy) => { + let messages; + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + value[i].id = messageIdx++; + if (copy) { + messages = [...messagesState, ...value]; + } else { + messages = value; + } + } + } else { + value.id = messageIdx++; + if (copy) { + messages = messagesState ? [...messagesState, value] : [value]; + } else { + messages = [value]; + } + } + return messages; + }; + const clear = () => { setMessagesState([]); }; const replace = (value) => { - const replaced = Array.isArray(value) ? value : [value]; + const replaced = assignIdentifiers(value, false); setMessagesState(replaced); }; diff --git a/components/lib/toast/Toast.js b/components/lib/toast/Toast.js index cf3148b032..2b1409fc51 100644 --- a/components/lib/toast/Toast.js +++ b/components/lib/toast/Toast.js @@ -16,25 +16,35 @@ export const Toast = React.memo( const show = (value) => { if (value) { - let messages; + const messages = assignIdentifiers(value, true); + messagesState.length === 0 && ZIndexUtils.set('toast', containerRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex['toast']); + setMessagesState(messages); + } + }; - if (Array.isArray(value)) { - const multipleMessages = value.reduce((acc, message) => { - acc.push({ _pId: messageIdx++, message }); + const assignIdentifiers = (value, copy) => { + let messages; + if (Array.isArray(value)) { + const multipleMessages = value.reduce((acc, message) => { + acc.push({ _pId: messageIdx++, message }); - return acc; - }, []); + return acc; + }, []); + if (copy) { messages = messagesState ? [...messagesState, ...multipleMessages] : multipleMessages; } else { - const message = { _pId: messageIdx++, message: value }; + messages = multipleMessages; + } + } else { + const message = { _pId: messageIdx++, message: value }; + if (copy) { messages = messagesState ? [...messagesState, message] : [message]; + } else { + messages = [message]; } - - messagesState.length === 0 && ZIndexUtils.set('toast', containerRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex['toast']); - - setMessagesState(messages); } + return messages; }; const clear = () => { @@ -43,7 +53,7 @@ export const Toast = React.memo( }; const replace = (value) => { - const replaced = Array.isArray(value) ? value : [value]; + const replaced = assignIdentifiers(value, false); setMessagesState(replaced); };