Skip to content

Commit

Permalink
Fix #2668/#2925: Messages/Toast replace method (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored May 25, 2022
1 parent 6f510ab commit 4b17eb7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
11 changes: 10 additions & 1 deletion components/doc/toast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,16 @@ toast.current.show({life: 5000, severity: 'error', summary: 'Error Message', det

<CodeHighlight>
{`
toast.current.clear();
toast.current.clear();
`}
</CodeHighlight>

<h5>Replacing Messages</h5>
<p><i>replace(newMessages)</i> method adds new messages after removing all old messages.</p>

<CodeHighlight lang="js">
{`
toast.current.replace(newMessages);
`}
</CodeHighlight>

Expand Down
5 changes: 3 additions & 2 deletions components/lib/messages/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const Messages = React.memo(React.forwardRef((props, ref) => {

const show = (value) => {
if (value) {
let messages = [];
let messages;

if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
Expand All @@ -33,7 +33,8 @@ export const Messages = React.memo(React.forwardRef((props, ref) => {
}

const replace = (value) => {
setMessagesState(value);
const replaced = Array.isArray(value) ? value : [value];
setMessagesState(replaced);
}

const onClose = (message) => {
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 @@ -41,4 +41,5 @@ export interface ToastProps {
export declare class Toast extends React.Component<ToastProps, any> {
public show(message: ToastMessageType): void;
public clear(): void;
public replace(message: ToastMessageType): void;
}
20 changes: 11 additions & 9 deletions components/lib/toast/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {
let messages;

if (Array.isArray(value)) {
const currentMessages = [...value];
for (let i = 0; i < currentMessages.length; i++) {
const message = { ...currentMessages[i] };
message.id = messageIdx++;
currentMessages[i] = message;
for (let i = 0; i < value.length; i++) {
value[i].id = messageIdx++;
messages = [...messagesState, ...value];
}
messages = [...messagesState, ...currentMessages];
}
else {
const currentMessage = { ...value };
currentMessage.id = messageIdx++;
messages = messagesState ? [...messagesState, currentMessage] : [currentMessage];
value.id = messageIdx++;
messages = messagesState ? [...messagesState, value] : [value];
}

messagesState.length === 0 && ZIndexUtils.set('toast', containerRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex['toast']);
Expand All @@ -43,6 +39,11 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {
setMessagesState([]);
}

const replace = (value) => {
const replaced = Array.isArray(value) ? value : [value];
setMessagesState(replaced);
}

const onClose = (message) => {
const messages = messagesState.filter(msg => msg.id !== message.id);
setMessagesState(messages);
Expand All @@ -66,6 +67,7 @@ export const Toast = React.memo(React.forwardRef((props, ref) => {

React.useImperativeHandle(ref, () => ({
show,
replace,
clear
}));

Expand Down

0 comments on commit 4b17eb7

Please sign in to comment.