Skip to content

Commit

Permalink
Fix primefaces#3455: Toast add remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Oct 10, 2022
1 parent 0d0bc80 commit 99a544d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
25 changes: 15 additions & 10 deletions components/lib/toast/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ export const Toast = React.memo(
const [messagesState, setMessagesState] = React.useState([]);
const containerRef = React.useRef(null);

const show = (value) => {
if (value) {
const messages = assignIdentifiers(value, true);
const show = (messageInfo) => {
if (messageInfo) {
const messages = assignIdentifiers(messageInfo, true);

messagesState.length === 0 && ZIndexUtils.set('toast', containerRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex['toast']);
setMessagesState(messages);
}
};

const assignIdentifiers = (value, copy) => {
const assignIdentifiers = (messageInfo, copy) => {
let messages;

if (Array.isArray(value)) {
const multipleMessages = value.reduce((acc, message) => {
if (Array.isArray(messageInfo)) {
const multipleMessages = messageInfo.reduce((acc, message) => {
acc.push({ _pId: messageIdx++, message });

return acc;
Expand All @@ -39,7 +39,7 @@ export const Toast = React.memo(
messages = multipleMessages;
}
} else {
const message = { _pId: messageIdx++, message: value };
const message = { _pId: messageIdx++, message: messageInfo };

if (copy) {
messages = messagesState ? [...messagesState, message] : [message];
Expand All @@ -56,20 +56,24 @@ export const Toast = React.memo(
setMessagesState([]);
};

const replace = (value) => {
const replaced = assignIdentifiers(value, false);
const replace = (messageInfo) => {
const replaced = assignIdentifiers(messageInfo, false);

setMessagesState(replaced);
};

const onClose = (messageInfo) => {
const remove = (messageInfo) => {
const messages = messagesState.filter((msg) => msg._pId !== messageInfo._pId);

setMessagesState(messages);

props.onRemove && props.onRemove(messageInfo.message);
};

const onClose = (messageInfo) => {
remove(messageInfo);
};

const onEntered = () => {
props.onShow && props.onShow();
};
Expand All @@ -88,6 +92,7 @@ export const Toast = React.memo(
props,
show,
replace,
remove,
clear,
getElement: () => containerRef.current
}));
Expand Down
1 change: 1 addition & 0 deletions components/lib/toast/toast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ export declare class Toast extends React.Component<ToastProps, any> {
public show(message: ToastMessageType): void;
public clear(): void;
public replace(message: ToastMessageType): void;
public remove(message: ToastMessageType): void;
public getElement(): HTMLDivElement;
}

0 comments on commit 99a544d

Please sign in to comment.