From d605c45190c1d500e45cf02dd6ed782926710988 Mon Sep 17 00:00:00 2001 From: rex Date: Sun, 3 Feb 2019 21:30:17 +0800 Subject: [PATCH] =?UTF-8?q?[improvement]=20TreeSelect:=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit re #1244 --- packages/tree-select/index.ts | 31 +++++++++++++++---------------- packages/tree-select/index.wxml | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/tree-select/index.ts b/packages/tree-select/index.ts index 8b5c97f1..4452ed8d 100644 --- a/packages/tree-select/index.ts +++ b/packages/tree-select/index.ts @@ -35,12 +35,13 @@ VantComponent({ watch: { items() { - this.updateSubItems(); - this.updateMainHeight(); + this.updateSubItems().then(() => { + this.updateMainHeight(); + }); }, maxHeight() { - this.updateItemHeight(); + this.updateItemHeight(this.data.subItems); this.updateMainHeight(); }, @@ -67,31 +68,29 @@ VantComponent({ // 更新子项列表 updateSubItems() { - const selectedItem = this.data.items[this.data.mainActiveIndex] || {}; + const { items, mainActiveIndex } = this.data; + const { children = [] } = items[mainActiveIndex] || {}; - this.set({ subItems: selectedItem.children || [] }); - - this.updateItemHeight(); + this.updateItemHeight(children); + return this.set({ subItems: children }); }, // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定 updateMainHeight() { + const { items = [], subItems = [] } = this.data; const maxHeight = Math.max( - this.data.items.length * ITEM_HEIGHT, - this.data.subItems.length * ITEM_HEIGHT + items.length * ITEM_HEIGHT, + subItems.length * ITEM_HEIGHT ); this.set({ mainHeight: Math.min(maxHeight, this.data.maxHeight) }); }, // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定 - updateItemHeight() { - this.set({ - itemHeight: Math.min( - this.data.subItems.length * ITEM_HEIGHT, - this.data.maxHeight - ) - }); + updateItemHeight(subItems) { + const itemHeight = Math.min(subItems.length * ITEM_HEIGHT, this.data.maxHeight); + + return this.set({ itemHeight }); } } }); diff --git a/packages/tree-select/index.wxml b/packages/tree-select/index.wxml index 124d5d6a..e1f25b50 100644 --- a/packages/tree-select/index.wxml +++ b/packages/tree-select/index.wxml @@ -22,7 +22,7 @@ >