Skip to content

Commit

Permalink
Fix #3250: Messages/Toast fix replace method (#3251)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Sep 7, 2022
1 parent 923df0c commit 9f1e902
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
26 changes: 24 additions & 2 deletions components/lib/messages/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
};

Expand Down
34 changes: 22 additions & 12 deletions components/lib/toast/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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);
};

Expand Down

0 comments on commit 9f1e902

Please sign in to comment.