From 3189207a86ed0801dbb657b233de9ef4ca43725b Mon Sep 17 00:00:00 2001 From: gucal Date: Tue, 23 May 2023 13:30:21 +0300 Subject: [PATCH] Refactor #13079 --- src/app/components/toast/toast.interface.ts | 12 +++++++++++- src/app/components/toast/toast.ts | 18 +++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/app/components/toast/toast.interface.ts b/src/app/components/toast/toast.interface.ts index aec83a805be..a7abd4b539f 100644 --- a/src/app/components/toast/toast.interface.ts +++ b/src/app/components/toast/toast.interface.ts @@ -1,6 +1,6 @@ import { TemplateRef } from '@angular/core'; -import { Toast } from './toast'; import { Message } from '../api/message'; +import { Toast } from './toast'; /** * Breakpoints of toast element. */ @@ -36,3 +36,13 @@ export interface ToastCloseEvent { */ message: Message; } +/** + * Custom close event. + * @see {@link ToastItem.onClose} + */ +export interface ToastItemCloseEvent extends ToastCloseEvent { + /** + * Index of the closed element. + */ + index: number; +} diff --git a/src/app/components/toast/toast.ts b/src/app/components/toast/toast.ts index 92a4380294f..07059d5cd37 100755 --- a/src/app/components/toast/toast.ts +++ b/src/app/components/toast/toast.ts @@ -31,7 +31,7 @@ import { TimesCircleIcon } from 'primeng/icons/timescircle'; import { RippleModule } from 'primeng/ripple'; import { ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primeng/utils'; import { Subscription } from 'rxjs'; -import { ToastBreakpoints, ToastCloseEvent } from './toast.interface'; +import { ToastBreakpoints, ToastCloseEvent, ToastItemCloseEvent } from './toast.interface'; @Component({ selector: 'p-toastItem', @@ -98,9 +98,9 @@ import { ToastBreakpoints, ToastCloseEvent } from './toast.interface'; } }) export class ToastItem implements AfterViewInit, OnDestroy { - @Input({required: true}) message!: Message; + @Input() message: Message | null | undefined; - @Input({required: true}) index!: number; + @Input() index: number | null | undefined; @Input() template: TemplateRef | undefined; @@ -112,7 +112,7 @@ export class ToastItem implements AfterViewInit, OnDestroy { @Input() hideTransitionOptions: string | undefined; - @Output() onClose: EventEmitter<{message: Message, index: number}> = new EventEmitter(); + @Output() onClose: EventEmitter = new EventEmitter(); @ViewChild('container') containerViewChild: ElementRef | undefined; @@ -129,8 +129,8 @@ export class ToastItem implements AfterViewInit, OnDestroy { this.zone.runOutsideAngular(() => { this.timeout = setTimeout(() => { this.onClose.emit({ - index: this.index, - message: this.message + index: this.index, + message: this.message }); }, this.message?.life || 3000); }); @@ -156,8 +156,8 @@ export class ToastItem implements AfterViewInit, OnDestroy { this.clearTimeout(); this.onClose.emit({ - index: this.index, - message: this.message + index: this.index, + message: this.message }); event.preventDefault(); @@ -370,7 +370,7 @@ export class Toast implements OnInit, AfterContentInit, OnDestroy { }); } - onMessageClose(event: {message: Message, index: number}) { + onMessageClose(event: ToastItemCloseEvent) { this.messages?.splice(event.index, 1); this.onClose.emit({