Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2668/#2925: Messages/Toast replace method #2932

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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