From 4df6e35d51a63cb2da4ebe79e31d995faa664f15 Mon Sep 17 00:00:00 2001 From: tianhao Date: Tue, 16 Jun 2026 13:02:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Toast=E5=88=86=E7=BB=84=E7=AE=A1?= =?UTF-8?q?=E7=90=86=20=E2=80=94=20group()/dismissGroup()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/core.js b/src/core.js index 67747b2..c85083c 100644 --- a/src/core.js +++ b/src/core.js @@ -91,6 +91,7 @@ class Toast { this.paused = false; this.closing = false; this._cleanups = []; + this.group = opts.group || null; } _palette() { @@ -1154,6 +1155,41 @@ const meToast = { count() { return this._toasts.size; }, + + /** + * 创建Toast分组 — 返回一个自动添加 group 参数的子对象 + * @param {string} name - 分组名称 + * @returns {Object} + */ + group(name) { + const self = this; + const methods = ['show','success','error','warning','info','loading','action']; + const g = { _group: name }; + methods.forEach(m => { + g[m] = (...args) => { + const last = typeof args[args.length - 1] === 'object' && args[args.length - 1] !== null && !Array.isArray(args[args.length - 1]) + ? args.pop() : {}; + return self[m](...args, { ...last, group: name }); + }; + }); + g.dismiss = () => self.dismissGroup(name); + g.count = () => self._groupCount(name); + return g; + }, + + /** + * 按组关闭Toast + * @param {string} name - 分组名称 + */ + dismissGroup(name) { + this._toasts.forEach(t => { if (t.group === name) t.close(); }); + }, + + _groupCount(name) { + let c = 0; + this._toasts.forEach(t => { if (t.group === name) c++; }); + return c; + }, }; export { meToast as default, meToast, Toast };