[improvement] TreeSelect: 优化性能

re #1244
This commit is contained in:
rex 2019-02-03 21:30:17 +08:00 committed by GitHub
parent cc783fc86f
commit d605c45190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 17 deletions

View File

@ -35,12 +35,13 @@ VantComponent({
watch: { watch: {
items() { items() {
this.updateSubItems(); this.updateSubItems().then(() => {
this.updateMainHeight(); this.updateMainHeight();
});
}, },
maxHeight() { maxHeight() {
this.updateItemHeight(); this.updateItemHeight(this.data.subItems);
this.updateMainHeight(); this.updateMainHeight();
}, },
@ -67,31 +68,29 @@ VantComponent({
// 更新子项列表 // 更新子项列表
updateSubItems() { 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(children);
return this.set({ subItems: children });
this.updateItemHeight();
}, },
// 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定 // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定
updateMainHeight() { updateMainHeight() {
const { items = [], subItems = [] } = this.data;
const maxHeight = Math.max( const maxHeight = Math.max(
this.data.items.length * ITEM_HEIGHT, items.length * ITEM_HEIGHT,
this.data.subItems.length * ITEM_HEIGHT subItems.length * ITEM_HEIGHT
); );
this.set({ mainHeight: Math.min(maxHeight, this.data.maxHeight) }); this.set({ mainHeight: Math.min(maxHeight, this.data.maxHeight) });
}, },
// 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定 // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定
updateItemHeight() { updateItemHeight(subItems) {
this.set({ const itemHeight = Math.min(subItems.length * ITEM_HEIGHT, this.data.maxHeight);
itemHeight: Math.min(
this.data.subItems.length * ITEM_HEIGHT, return this.set({ itemHeight });
this.data.maxHeight
)
});
} }
} }
}); });

View File

@ -22,7 +22,7 @@
> >
<view <view
wx:for="{{ subItems }}" wx:for="{{ subItems }}"
wx:key="item.id" wx:key="id"
class="van-ellipsis van-hairline--bottom content-item-class {{ utils.bem('tree-select__item', { active: activeId === item.id, disabled: item.disabled }) }} {{ activeId === item.id ? 'content-active-class' : '' }} {{ item.disabled ? 'content-disabled-class' : '' }}" class="van-ellipsis van-hairline--bottom content-item-class {{ utils.bem('tree-select__item', { active: activeId === item.id, disabled: item.disabled }) }} {{ activeId === item.id ? 'content-active-class' : '' }} {{ item.disabled ? 'content-disabled-class' : '' }}"
data-item="{{ item }}" data-item="{{ item }}"
bind:tap="onSelectItem" bind:tap="onSelectItem"