feat: Toast分组管理 — group()/dismissGroup()

This commit is contained in:
tianhao
2026-06-16 13:02:46 +08:00
parent 23745952e5
commit 4df6e35d51
+36
View File
@@ -91,6 +91,7 @@ class Toast {
this.paused = false; this.paused = false;
this.closing = false; this.closing = false;
this._cleanups = []; this._cleanups = [];
this.group = opts.group || null;
} }
_palette() { _palette() {
@@ -1154,6 +1155,41 @@ const meToast = {
count() { count() {
return this._toasts.size; 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 }; export { meToast as default, meToast, Toast };