From 57a490ffbd7b9488449ddaf19d3ab86fbc94f024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=C4=9F=C3=A7e=20K=C3=BC=C3=A7=C3=BCko=C4=9Flu?= Date: Tue, 7 Mar 2023 15:36:38 +0300 Subject: [PATCH] Fixed #2989 - Toast: new close and life-end methods --- api-generator/components/toast.js | 26 ++++++++++++++++++++++++++ components/toast/Toast.d.ts | 13 ++++++++++++- components/toast/Toast.spec.js | 3 ++- components/toast/Toast.vue | 6 ++++-- components/toast/ToastMessage.vue | 8 ++++---- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/api-generator/components/toast.js b/api-generator/components/toast.js index c5003a4ee..1306aadb6 100644 --- a/api-generator/components/toast.js +++ b/api-generator/components/toast.js @@ -31,6 +31,31 @@ const ToastProps = [ } ]; +const ToastEvents = [ + { + name: 'close', + description: 'Callback to invoke when the toast is closed.', + arguments: [ + { + name: 'message', + type: 'any', + description: 'Message of toast.' + } + ] + }, + { + name: 'life-end', + description: 'Callback to invoke when the toast timeout is over.', + arguments: [ + { + name: 'message', + type: 'any', + description: 'Message of toast.' + } + ] + } +]; + const ToastSlots = [ { name: 'message', @@ -43,6 +68,7 @@ module.exports = { name: 'Toast', description: 'Toast is used to display messages in an overlay.', props: ToastProps, + events: ToastEvents, slots: ToastSlots } }; diff --git a/components/toast/Toast.d.ts b/components/toast/Toast.d.ts index 16b7b8a1c..72fead832 100755 --- a/components/toast/Toast.d.ts +++ b/components/toast/Toast.d.ts @@ -144,7 +144,18 @@ export interface ToastSlots { /** * Defines valid emits in Toast component. */ -export interface ToastEmits {} +export interface ToastEmits { + /** + * Callback to invoke when the toast is closed. + * @param {ToastMessageOptions} message - Toast message. + */ + close(message: ToastMessageOptions): void; + /** + * Callback to invoke when the toast's timeout is over. + * @param {ToastMessageOptions} message - Toast message. + */ + 'life-end'(message: ToastMessageOptions): void; +} /** * **PrimeVue - Toast** diff --git a/components/toast/Toast.spec.js b/components/toast/Toast.spec.js index 51529c642..936dbf7bd 100644 --- a/components/toast/Toast.spec.js +++ b/components/toast/Toast.spec.js @@ -44,8 +44,9 @@ describe('Toast.vue', () => { }); it('should close toast', async () => { - await wrapper.vm.remove({ severity: 'success', summary: 'Success Message', detail: 'Message Content', life: 3000 }); + await wrapper.vm.remove({ message: { severity: 'success', summary: 'Success Message', detail: 'Message Content', life: 3000 }, type: 'close' }); + expect(wrapper.emitted()['close'][0][0].message).not.toBe({}); expect(wrapper.find('.p-toast-message').exists()).toBe(false); }); diff --git a/components/toast/Toast.vue b/components/toast/Toast.vue index 9118fce1a..61b798047 100755 --- a/components/toast/Toast.vue +++ b/components/toast/Toast.vue @@ -31,6 +31,7 @@ var messageIdx = 0; export default { name: 'Toast', inheritAttrs: false, + emits: ['close', 'life-end'], props: { group: { type: String, @@ -111,17 +112,18 @@ export default { this.messages = [...this.messages, message]; }, - remove(message) { + remove(params) { let index = -1; for (let i = 0; i < this.messages.length; i++) { - if (this.messages[i] === message) { + if (this.messages[i] === params.message) { index = i; break; } } this.messages.splice(index, 1); + this.$emit(params.type, { message: params.message }); }, onAdd(message) { if (this.group == message.group) { diff --git a/components/toast/ToastMessage.vue b/components/toast/ToastMessage.vue index b93ef369c..246d5f616 100755 --- a/components/toast/ToastMessage.vue +++ b/components/toast/ToastMessage.vue @@ -62,7 +62,7 @@ export default { mounted() { if (this.message.life) { this.closeTimeout = setTimeout(() => { - this.close(); + this.close({ message: this.message, type: 'life-end' }); }, this.message.life); } }, @@ -70,12 +70,12 @@ export default { this.clearCloseTimeout(); }, methods: { - close() { - this.$emit('close', this.message); + close(params) { + this.$emit('close', params); }, onCloseClick() { this.clearCloseTimeout(); - this.close(); + this.close({ message: this.message, type: 'close' }); }, clearCloseTimeout() { if (this.closeTimeout) {