From fc3132f3305e4bd7d82625d981522f64dd7f3941 Mon Sep 17 00:00:00 2001 From: tianhao Date: Tue, 16 Jun 2026 13:00:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Action=20Toast=20=E2=80=94=20=E5=86=85?= =?UTF-8?q?=E5=B5=8C=E6=93=8D=E4=BD=9C=E6=8C=89=E9=92=AE=E7=9A=84=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/core.js b/src/core.js index 341a553..4d78f10 100644 --- a/src/core.js +++ b/src/core.js @@ -591,6 +591,21 @@ const _progressHTML = (opts) => { `; }; +/** + * Action Toast 模板 + * @param {Array} actions - 操作按钮数组 [{ text, onClick, style }] + * @returns {string} HTML + */ +const _actionHTML = (actions) => { + if (!Array.isArray(actions) || actions.length === 0) return ''; + const buttons = actions.map((a, i) => { + const bg = a.style?.background || a.color || '#6366f1'; + const cls = `met-action-btn-${i}`; + return ``; + }).join(''); + return _btnRow(buttons); +}; + /** * 主对象 */ @@ -980,6 +995,40 @@ const meToast = { }; }, + /** + * Action Toast — 带操作按钮的通知 + * @param {string|Object} messageOrOpts - 消息 + * @param {Array} actions - 操作按钮 [{ text, onClick, color }] + * @param {Object} [opts] - 选项 + * @returns {Object} toast实例 + dismiss + */ + action(messageOrOpts, actions = [], opts = {}) { + const normalized = normalizeArgs([messageOrOpts, opts], 'info'); + normalized.duration = opts.duration ?? 0; + normalized.closeButton = opts.closeButton ?? true; + + const t = this._emit({ + ...normalized, + html: _actionHTML(actions) + (normalized.html || ''), + }); + + if (Array.isArray(actions)) { + setTimeout(() => { + actions.forEach((a, i) => { + const btn = t.el?.querySelector(`.met-action-btn-${i}`); + if (btn && typeof a.onClick === 'function') { + btn.addEventListener('click', () => { + try { a.onClick(t); } catch (e) { console.error('Action onClick error:', e); } + if (a.close !== false) t.close(); + }); + } + }); + }, 0); + } + + return { id: t.id, toast: t, dismiss: () => t.close() }; + }, + /** * 队列Toast * @param {Array} messages - 消息数组