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 };