feat(tabs): 为 tabs.add 新增 active 选项 (#2607)

* tabs.add新增chang

是否将插入项切换为当前标签,l默认为true

* change参数

* feat: 将 `change` 选项更改命名为 `active`

* chore: 修改 closable 选项 JSDoc 类型

---------

Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com>
pull/2626/head
topwms 2025-04-13 23:05:20 +08:00 committed by GitHub
parent 4af2bb53b1
commit 44e54e586e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -110,6 +110,7 @@ tabs.render({
| id | 标签的 `lay-id` 属性值 | string | - |
| index | 活动标签的索引或 `lay-id` 属性值,默认取当前选中标签的索引 | number | - |
| mode | 标签的插入方式。支持以下可选值:<ul><li>`append` 插入标签到最后</li> <li>`prepend` 插入标签到最前</li> <li>`before` 在活动标签前插入</li> <li>`after` 在活动标签后插入</li></ul> | string | `append` |
| active | 是否将新增项设置为活动标签 | boolean | `true` |
| closable | 标签是否可关闭。初始值取决于 `options.closable` | boolean | `false` |
| headerItem | 自定义标签头部元素,如 `headerItem: '<li></li>'` | string | - |
| bodyItem | 自定义标签内容元素,如 `bodyItem: '<div></div>'` | string | - |

View File

@ -173,7 +173,8 @@ layui.define('component', function(exports) {
* @param {string} opts.id - 标签的 lay-id 属性值
* @param {string} [opts.index] - 活动标签索引默认取当前选中标签的索引
* @param {('append'|'prepend'|'after'|'before')} [opts.mode='append'] - 标签插入方式
* @param {string} [opts.closable] - 标签是否可关闭初始值取决于 options.closable
* @param {boolean} [opts.active] - 是否将新增项设置为活动标签
* @param {boolean} [opts.closable] - 标签是否可关闭初始值取决于 options.closable
* @param {string} [opts.headerItem] - 自定义标签头部元素
* @param {string} [opts.bodyItem] - 自定义标签内容元素
* @param {Function} [opts.done] - 标签添加成功后执行的回调函数
@ -185,6 +186,11 @@ layui.define('component', function(exports) {
var newHeaderItem = that.renderHeaderItem(opts);
var newBodyItem = that.renderBodyItem(opts);
// 选项默认值
opts = $.extend({
active: true
}, opts);
// 插入方式
if (/(before|after)/.test(opts.mode)) { // 在活动标签前后插入
var data = that.data();
@ -202,8 +208,12 @@ layui.define('component', function(exports) {
container.body.elem[mode](newBodyItem);
}
// 将插入项切换为当前标签
that.change(newHeaderItem, true);
// 是否将新增项设置为活动标签
if (opts.active) {
that.change(newHeaderItem, true);
} else {
that.roll('auto');
}
// 回调
var params = that.data();