From 3a8ab7fd95e3852a63abcd7f0b2a5b4881803952 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 16 May 2025 22:02:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Toast/Toast.razor.js | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/BootstrapBlazor/Components/Toast/Toast.razor.js b/src/BootstrapBlazor/Components/Toast/Toast.razor.js index 4640ff84110..dfc3e793a00 100644 --- a/src/BootstrapBlazor/Components/Toast/Toast.razor.js +++ b/src/BootstrapBlazor/Components/Toast/Toast.razor.js @@ -3,6 +3,7 @@ import EventHandler from "../../modules/event-handler.js" export function init(id, invoke, callback) { const el = document.getElementById(id) + const progressElement = el.querySelector('.toast-progress') const toast = { element: el, invoke, @@ -10,43 +11,43 @@ export function init(id, invoke, callback) { toast: new bootstrap.Toast(el), showProgress: () => { return toast.toast._config.autohide - } + }, + progressElement } Data.set(id, toast); if (toast.showProgress()) { - toast.progressElement = toast.element.querySelector('.toast-progress') const delay = toast.toast._config.delay / 1000 - toast.progressElement.style.transition = `width linear ${delay}s` + progressElement.style.transition = `width linear ${delay}s` } - EventHandler.on(toast.element, 'shown.bs.toast', () => { + EventHandler.on(el, 'shown.bs.toast', () => { if (toast.showProgress()) { - toast.progressElement.style.width = '100%' + progressElement.style.width = '100%' } }) - EventHandler.on(toast.element, 'hidden.bs.toast', () => { - toast.invoke.invokeMethodAsync(toast.callback) + EventHandler.on(el, 'hidden.bs.toast', () => { + invoke.invokeMethodAsync(toast.callback) }) - toast.toast.show() + EventHandler.on(progressElement, 'transitionend', e => { + toast.toast.hide(); + }); + + toast.toast.show(); } export function update(id) { const t = Data.get(id); - const { element, toast } = t; + const { element, toast, progressElement } = t; const autoHide = element.getAttribute('data-bs-autohide') !== 'false'; - if(autoHide) { + if (autoHide) { const delay = parseInt(element.getAttribute('data-bs-delay')); - const progressElement = element.querySelector('.toast-progress'); toast._config.autohide = autoHide; toast._config.delay = delay; progressElement.style.width = '100%'; progressElement.style.transition = `width linear ${delay / 1000}s`; - EventHandler.on(progressElement, 'transitionend', e => { - toast.hide(); - }); } } @@ -54,10 +55,12 @@ export function dispose(id) { const toast = Data.get(id) Data.remove(id) - EventHandler.off(toast.element, 'shown.bs.toast') - EventHandler.off(toast.element, 'hidden.bs.toast') + const { element, progressElement } = toast; + EventHandler.off(element, 'shown.bs.toast'); + EventHandler.off(element, 'hidden.bs.toast'); + EventHandler.off(progressElement, 'transitionend'); if (toast.toast) { - toast.toast.dispose() + toast.toast.dispose(); } } From 573d6524d0769b5e6548cf6f76bdbe6b7c4162fd Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 16 May 2025 22:06:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E5=AE=8C=E5=96=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Toast/Toast.razor.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/BootstrapBlazor/Components/Toast/Toast.razor.js b/src/BootstrapBlazor/Components/Toast/Toast.razor.js index dfc3e793a00..c3f1a8d3f24 100644 --- a/src/BootstrapBlazor/Components/Toast/Toast.razor.js +++ b/src/BootstrapBlazor/Components/Toast/Toast.razor.js @@ -49,6 +49,11 @@ export function update(id) { progressElement.style.width = '100%'; progressElement.style.transition = `width linear ${delay / 1000}s`; } + else { + toast._config.autohide = false; + progressElement.style.removeProperty('width'); + progressElement.style.removeProperty('transition'); + } } export function dispose(id) {