From 57a7d7eade5196a04483400f4f2e6b8b04cb1632 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Mon, 11 Jul 2022 11:07:40 +0800 Subject: [PATCH] feat(dropdown-menu): add toggle function --- src/dropdown-menu/README.md | 9 +++++++- src/dropdown-menu/dropdown-menu.ts | 34 ++++++++++++++++------------ src/dropdown-menu/dropdown-menu.wxml | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/dropdown-menu/README.md b/src/dropdown-menu/README.md index 776739383..a876af39c 100644 --- a/src/dropdown-menu/README.md +++ b/src/dropdown-menu/README.md @@ -183,6 +183,13 @@ duration | String / Number | 200 | 动画时长 | N show-overlay | Boolean | true | 是否显示遮罩层 | N z-index | Number | 11600 | 菜单栏 z-index 层级 | N +### DropdownMenu Function + +方法名 | 说明 | 参数 | 返回值 +--|--|--|-- +toggle | 切换下拉菜单的展示状态,传入索引值则切换对应菜单,不传值则关闭当前菜单 | index?: number | `void` + +> 通过 `this.selectComponent` 获取组件示例,调用即可 ### DropdownItem Props 名称 | 类型 | 默认值 | 说明 | 必传 @@ -202,4 +209,4 @@ default-value | String / Number / Array | undefined | 选中值。非受控属 名称 | 参数 | 描述 -- | -- | -- change | `(value: TdDropdownItemOptionValueType | Array)` | 值改变时触发 -confirm | `(value: TdDropdownItemOptionValueType | Array)` | 点击确认时触发 +confirm | `(value: TdDropdownItemOptionValueType | Array)` | 点击确认时触发 \ No newline at end of file diff --git a/src/dropdown-menu/dropdown-menu.ts b/src/dropdown-menu/dropdown-menu.ts index a8bcb7959..a603ab787 100644 --- a/src/dropdown-menu/dropdown-menu.ts +++ b/src/dropdown-menu/dropdown-menu.ts @@ -34,22 +34,12 @@ export default class DropdownMenu extends SuperComponent { }; methods = { - getAllItems() { - const nodes = this.getRelationNodes('./dropdown-item'); - const menus = nodes.map((a) => a.data); - - this.setData({ - nodes, - menus, - }); - }, - toggleDropdown(e: WechatMiniprogram.BaseEvent) { - const { index: idx } = e.currentTarget.dataset; + toggle(index: number) { const { activeIdx, duration } = this.data; const prevItem = this.data.nodes[activeIdx]; - const currItem = this.data.nodes[idx]; + const currItem = this.data.nodes[index]; - if (currItem.data.disabled) return; + if (currItem?.data.disabled) return; if (activeIdx !== -1) { prevItem.triggerEvent('close'); @@ -65,14 +55,14 @@ export default class DropdownMenu extends SuperComponent { ); } - if (activeIdx === idx) { + if (index == null || activeIdx === index) { this.setData({ activeIdx: -1, }); } else { currItem.triggerEvent('open'); this.setData({ - activeIdx: idx, + activeIdx: index, }); currItem.setData( { @@ -86,5 +76,19 @@ export default class DropdownMenu extends SuperComponent { ); } }, + getAllItems() { + const nodes = this.getRelationNodes('./dropdown-item'); + const menus = nodes.map((a) => a.data); + + this.setData({ + nodes, + menus, + }); + }, + handleToggle(e: WechatMiniprogram.BaseEvent) { + const { index } = e.currentTarget.dataset; + + this.toggle(index); + }, }; } diff --git a/src/dropdown-menu/dropdown-menu.wxml b/src/dropdown-menu/dropdown-menu.wxml index 6530bac2d..b5aa75f46 100644 --- a/src/dropdown-menu/dropdown-menu.wxml +++ b/src/dropdown-menu/dropdown-menu.wxml @@ -3,7 +3,7 @@