Skip to content

Commit

Permalink
Fix primefaces#5083: Toast/Messages remove by message
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Oct 12, 2023
1 parent a23bc1d commit f276d62
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
9 changes: 6 additions & 3 deletions components/lib/messages/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TransitionGroup } from 'react-transition-group';
import { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
import { mergeProps } from '../utils/Utils';
import { ObjectUtils, mergeProps } from '../utils/Utils';
import { MessagesBase } from './MessagesBase';
import { UIMessage } from './UIMessage';

Expand Down Expand Up @@ -70,9 +70,12 @@ export const Messages = React.memo(
};

const remove = (messageInfo) => {
setMessagesState((prev) => prev.filter((msg) => msg._pId !== messageInfo._pId));
// allow removal by ID or by message equality
const removeMessage = messageInfo._pId ? messageInfo.message : messageInfo;

props.onRemove && props.onRemove(messageInfo.message);
setMessagesState((prev) => prev.filter((msg) => msg._pId !== messageInfo._pId && !ObjectUtils.deepEquals(msg.message, removeMessage)));

props.onRemove && props.onRemove(removeMessage);
};

const onClose = (messageInfo) => {
Expand Down
14 changes: 8 additions & 6 deletions components/lib/toast/Toast.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as React from 'react';
import { TransitionGroup } from 'react-transition-group';
import { PrimeReactContext } from '../api/Api';
import PrimeReact, { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
import { useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { Portal } from '../portal/Portal';
import { ZIndexUtils, mergeProps } from '../utils/Utils';
import { ObjectUtils, ZIndexUtils, mergeProps } from '../utils/Utils';
import { ToastBase } from './ToastBase';
import { ToastMessage } from './ToastMessage';
import PrimeReact from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';

let messageIdx = 0;

Expand Down Expand Up @@ -75,9 +74,12 @@ export const Toast = React.memo(
};

const remove = (messageInfo) => {
setMessagesState((m) => m.filter((msg) => msg._pId !== messageInfo._pId));
// allow removal by ID or by message equality
const removeMessage = messageInfo._pId ? messageInfo.message : messageInfo;

setMessagesState((prev) => prev.filter((msg) => msg._pId !== messageInfo._pId && !ObjectUtils.deepEquals(msg.message, removeMessage)));

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

const onClose = (messageInfo) => {
Expand Down
6 changes: 6 additions & 0 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export default class ObjectUtils {
else return this.deepEquals(obj1, obj2);
}

/**
* Compares two JSON objects for deep equality recursively comparing both objects.
* @param {*} a the first JSON object
* @param {*} b the second JSON object
* @returns true if equals, false it not
*/
static deepEquals(a, b) {
if (a === b) return true;

Expand Down

0 comments on commit f276d62

Please sign in to comment.