Life property to Messages components
I'm submitting a new feature request ...
Current behavior
Currently messages doesn't have auto disposable option with time seconds
Expected behavior
<p-messages [(value)]="msgs" [life]="1000" >
What is the motivation / use case for changing the behavior?
Having the requirement to display a messages with auto disposable after some time interval.
+1 I agree that this should be implemented as well. In most cases, Messages and Growl both have different uses. However the Messages component not having a 'timeout' is rather unfortunate.
@cagataycivici Is this something that could be implemented? Or are you open for the creation of a pull request for this feature?
Sounds good to me.
+1
+1
+1
Doublicate of #3232 . Maybe should be rejected according to p-toast #7139 @yigitfindikli .
It looks like there hasn't been any movement on this for awhile. I went ahead and created a directive to implement this functionality for my own project.
import { AfterContentInit, Directive, OnDestroy } from '@angular/core';
import { Message } from 'primeng/api';
import { Messages } from 'primeng/messages';
import { Subscription } from 'rxjs';
@Directive({
selector: 'p-messages[enableLife]'
})
export class PrimeMessagesLifeDirective implements AfterContentInit, OnDestroy {
private msgSub: Subscription;
constructor(private host: Messages) {}
ngOnDestroy(): void {
this.msgSub?.unsubscribe();
}
ngAfterContentInit(): void {
if (this.host.messageService && this.host.enableService && !this.host.contentTemplate) {
this.msgSub = this.host.messageService.messageObserver.subscribe((messages) => {
if (!messages) { return; }
if (messages instanceof Array) {
messages.map(this.processMessages);
} else {
this.processMessages(messages);
}
});
}
}
private processMessages(message: Message) {
if (!message.life) { return; }
setTimeout(() => {
const index = this.host.value?.indexOf(message) ?? -1;
if (index !== -1) {
this.host.removeMessage(index);
this.host.cd.markForCheck();
}
}, message.life);
}
}
<p-messages enableLife></p-messages>
Hope this helps anyone wanting life on Messages. 😊
Duplicate of https://github.com/primefaces/primeng/issues/3232