From e1f80b569dcd21a2061c2ba3552903cacad6c92d Mon Sep 17 00:00:00 2001 From: rex Date: Fri, 1 Nov 2019 16:14:52 +0800 Subject: [PATCH] refactor(TreeSelect): refactor with sidebar (#2224) BREAKING CHANGE: rename prop active of sidebar to activeKey --- example/pages/tree-select/index.wxml | 29 ++++++++---- packages/common/style/var.less | 25 +++++++++++ packages/icon/index.less | 6 +++ packages/image/index.wxml | 8 ++-- packages/sidebar-item/index.less | 37 +++++++++------- packages/sidebar-item/index.ts | 16 ++++--- packages/sidebar-item/index.wxml | 2 +- packages/sidebar/index.less | 3 +- packages/sidebar/index.ts | 30 +++++++------ packages/tree-select/index.json | 4 +- packages/tree-select/index.less | 62 ++++++++------------------ packages/tree-select/index.ts | 66 ++++++++++------------------ packages/tree-select/index.wxml | 30 ++++++------- 13 files changed, 164 insertions(+), 154 deletions(-) diff --git a/example/pages/tree-select/index.wxml b/example/pages/tree-select/index.wxml index c142ff04..a8235078 100644 --- a/example/pages/tree-select/index.wxml +++ b/example/pages/tree-select/index.wxml @@ -1,6 +1,5 @@ - + + diff --git a/packages/common/style/var.less b/packages/common/style/var.less index da8f6313..9610a27e 100644 --- a/packages/common/style/var.less +++ b/packages/common/style/var.less @@ -282,6 +282,22 @@ // Search @search-background-color: #f7f8fA; +// Sidebar +@sidebar-width: 85px; + +// SidebarItem +@sidebar-font-size: @font-size-md; +@sidebar-line-height: 20px; +@sidebar-text-color: @text-color; +@sidebar-disabled-text-color: @gray; +@sidebar-padding: 20px @padding-sm 20px @padding-xs; +@sidebar-active-color: @active-color; +@sidebar-background-color: @background-color-light; +@sidebar-selected-font-weight: @font-weight-bold; +@sidebar-selected-text-color: @text-color; +@sidebar-selected-border-color: @red; +@sidebar-selected-background-color: @white; + // Stepper @stepper-active-color: #e8e8e8; @stepper-background-color: @active-color; @@ -346,6 +362,15 @@ @divider-content-left-width: 10%; @divider-content-right-width: 10%; +// TreeSelect +@tree-select-font-size: @font-size-md; +@tree-select-nav-background-color: @background-color-light; +@tree-select-content-background-color: @white; +@tree-select-nav-item-padding: @padding-sm @padding-xs @padding-sm @padding-sm; +@tree-select-item-height: 44px; +@tree-select-item-active-color: @red; +@tree-select-item-disabled-color: @gray; + // Uploader @uploader-size: 80px; @uploader-icon-size: 24px; diff --git a/packages/icon/index.less b/packages/icon/index.less index 6f533d58..a2c2d322 100644 --- a/packages/icon/index.less +++ b/packages/icon/index.less @@ -1,6 +1,12 @@ @import '../common/style/var.less'; @import '@vant/icons/src/index.less'; +:host { + display: flex; + align-items: center; + justify-content: center; +} + .van-icon { &--image { width: 1em; diff --git a/packages/image/index.wxml b/packages/image/index.wxml index 1a43652f..6c48d9ab 100644 --- a/packages/image/index.wxml +++ b/packages/image/index.wxml @@ -16,7 +16,7 @@ bind:error="onError" /> -
@@ -29,8 +29,8 @@ name="photo-o" size="22" /> -
-
+ @@ -43,5 +43,5 @@ name="warning-o" size="22" /> -
+ diff --git a/packages/sidebar-item/index.less b/packages/sidebar-item/index.less index 90d127b3..a3ecf9ba 100644 --- a/packages/sidebar-item/index.less +++ b/packages/sidebar-item/index.less @@ -4,40 +4,45 @@ .van-sidebar-item { display: block; box-sizing: border-box; - padding: 20px 12px 20px 9px; overflow: hidden; - font-size: 14px; - line-height: 1.4; word-wrap: break-word; border-left: 3px solid transparent; user-select: none; - .theme(color, '@gray-darker'); - .theme(background-color, '@background-color'); + .theme(padding, '@sidebar-padding'); + .theme(font-size, '@sidebar-font-size'); + .theme(line-height, '@sidebar-line-height'); + .theme(color, '@sidebar-text-color'); + .theme(background-color, '@sidebar-background-color'); - &--hover { - .theme(background-color, '@active-color'); + &__text { + position: relative; + display: inline-block; + } + + &--hover:not(&--disabled) { + .theme(background-color, '@sidebar-active-color'); } &::after { border-bottom-width: 1px; } - &--active { - font-weight: bold; - .theme(color, '@text-color'); - .theme(border-color, '@red'); + &--selected { + .theme(color, '@sidebar-selected-text-color'); + .theme(font-weight, '@sidebar-selected-font-weight'); + .theme(border-color, '@sidebar-selected-border-color'); &::after { border-right-width: 1px; } } - &--active, - &--active&--hover { - .theme(background-color, '@white'); + &--selected, + &--selected&--hover { + .theme(background-color, '@sidebar-selected-background-color'); } - &__text { - position: relative; + &--disabled { + .theme(color, '@sidebar-disabled-text-color'); } } diff --git a/packages/sidebar-item/index.ts b/packages/sidebar-item/index.ts index 0d5a0b98..a140a1f7 100644 --- a/packages/sidebar-item/index.ts +++ b/packages/sidebar-item/index.ts @@ -1,6 +1,11 @@ import { VantComponent } from '../common/component'; VantComponent({ + classes: [ + 'active-class', + 'disabled-class', + ], + relation: { type: 'ancestor', name: 'sidebar', @@ -12,18 +17,19 @@ VantComponent({ props: { dot: Boolean, info: null, - title: String + title: String, + disabled: Boolean }, methods: { onClick() { const { parent } = this; - if (!parent) { + if (!parent || this.data.disabled) { return; } - const index = parent.items.indexOf(this); + const index = parent.children.indexOf(this); parent.setActive(index).then(() => { this.$emit('click', index); @@ -31,8 +37,8 @@ VantComponent({ }); }, - setActive(active: boolean) { - return this.setData({ active }); + setActive(selected: boolean) { + return this.setData({ selected }); } } }); diff --git a/packages/sidebar-item/index.wxml b/packages/sidebar-item/index.wxml index 6ae8d538..f68e8644 100644 --- a/packages/sidebar-item/index.wxml +++ b/packages/sidebar-item/index.wxml @@ -1,7 +1,7 @@ item !== target); - this.setActive(this.data.active); + this.items = this.children.filter( + (item: WechatMiniprogram.Component.TrivialInstance) => item !== target + ); + this.setActive(this.data.activeKey); } }, props: { - active: { + activeKey: { type: Number, value: 0, observer: 'setActive' @@ -23,28 +25,28 @@ VantComponent({ }, beforeCreate() { - this.items = []; + this.children = []; this.currentActive = -1; }, methods: { - setActive(active: number) { - const { items, currentActive } = this; + setActive(activeKey: number) { + const { children, currentActive } = this; - if (!items.length) { + if (!children.length) { return Promise.resolve(); } - this.currentActive = active; + this.currentActive = activeKey; const stack = []; - if (currentActive !== active && items[currentActive]) { - stack.push(items[currentActive].setActive(false)); + if (currentActive !== activeKey && children[currentActive]) { + stack.push(children[currentActive].setActive(false)); } - if (items[active]) { - stack.push(items[active].setActive(true)); + if (children[activeKey]) { + stack.push(children[activeKey].setActive(true)); } return Promise.all(stack); diff --git a/packages/tree-select/index.json b/packages/tree-select/index.json index 0a336c08..42991a2a 100644 --- a/packages/tree-select/index.json +++ b/packages/tree-select/index.json @@ -1,6 +1,8 @@ { "component": true, "usingComponents": { - "van-icon": "../icon/index" + "van-icon": "../icon/index", + "van-sidebar": "../sidebar/index", + "van-sidebar-item": "../sidebar-item/index" } } diff --git a/packages/tree-select/index.less b/packages/tree-select/index.less index 173043c2..62dfc741 100644 --- a/packages/tree-select/index.less +++ b/packages/tree-select/index.less @@ -3,73 +3,47 @@ .van-tree-select { position: relative; - font-size: 14px; + display: flex; user-select: none; + .theme(font-size, '@tree-select-font-size'); &__nav { - position: absolute; - top: 0; - bottom: 0; - left: 0; - width: 35%; - min-width: 120px; - .theme(background-color, '@background-color-light'); - } + flex: 1; + .theme(background-color, '@tree-select-nav-background-color'); - &__nitem { - position: relative; - padding: 0 9px 0 15px; - line-height: 44px; - - &--active::after { - position: absolute; - top: 0; - bottom: 0; - left: 0; - width: 3.6px; - content: ''; - .theme(background-color, '@red'); + &__inner { + width: 100% !important; + height: 100%; } - &--active { - font-weight: bold; - .theme(background-color, '@white'); - } - - &--disabled { - .theme(color, '@gray-dark'); - } + --sidebar-padding: @tree-select-nav-item-padding; } &__content { - box-sizing: border-box; - width: 65%; - margin-left: 35%; - padding-left: 15px; - .theme(background-color, '@white'); + flex: 2; + .theme(background-color, '@tree-select-content-background-color'); } &__item { position: relative; font-weight: bold; - line-height: 44px; + + .theme(padding, '0 32px 0 @padding-md'); + .theme(line-height, '@tree-select-item-height'); &--active { - .theme(color, '@red'); + .theme(color, '@tree-select-item-active-color'); } &--disabled { - .theme(color, '@gray-dark'); + .theme(color, '@tree-select-item-disabled-color'); } } &__selected { position: absolute; - top: 0; - right: 15px; - bottom: 0; - height: 24px; - margin: auto 0; - line-height: 24px; + top: 50%; + transform: translateY(-50%); + .theme(right, '@padding-md'); } } diff --git a/packages/tree-select/index.ts b/packages/tree-select/index.ts index 32cdefe3..2b4618ee 100644 --- a/packages/tree-select/index.ts +++ b/packages/tree-select/index.ts @@ -1,7 +1,6 @@ import { VantComponent } from '../common/component'; import { Weapp } from 'definitions/weapp'; - -const ITEM_HEIGHT = 44; +import { addUnit } from '../common/utils'; VantComponent({ classes: [ @@ -14,15 +13,20 @@ VantComponent({ ], props: { - items: Array, + items: { + type: Array, + observer: 'updateSubItems' + }, activeId: null, mainActiveIndex: { type: Number, - value: 0 + value: 0, + observer: 'updateSubItems' }, - maxHeight: { - type: Number, - value: 300 + height: { + type: [Number, String], + value: 300, + observer: 'updateHeight' }, max: { type: Number, @@ -31,24 +35,11 @@ VantComponent({ }, data: { - subItems: [], - mainHeight: 0, - itemHeight: 0 + subItems: [] }, - watch: { - items() { - this.updateSubItems().then(() => { - this.updateMainHeight(); - }); - }, - - maxHeight() { - this.updateItemHeight(this.data.subItems); - this.updateMainHeight(); - }, - - mainActiveIndex: 'updateSubItems' + created() { + this.updateHeight(); }, methods: { @@ -57,9 +48,11 @@ VantComponent({ const { item } = event.currentTarget.dataset; const isArray = Array.isArray(this.data.activeId); // 判断有没有超出右侧选择的最大数 - const isOverMax = isArray && (this.data.activeId.length >= this.data.max); + const isOverMax = isArray && this.data.activeId.length >= this.data.max; // 判断该项有没有被选中, 如果有被选中,则忽视是否超出的条件 - const isSelected = isArray ? this.data.activeId.indexOf(item.id) > -1 : this.data.activeId === item.id; + const isSelected = isArray + ? this.data.activeId.indexOf(item.id) > -1 + : this.data.activeId === item.id; if (!item.disabled && (!isOverMax || isSelected)) { this.$emit('click-item', item); @@ -68,7 +61,7 @@ VantComponent({ // 当一个导航被点击时 onClickNav(event: Weapp.Event) { - const { index } = event.currentTarget.dataset; + const index = event.detail; const item = this.data.items[index]; if (!item.disabled) { this.$emit('click-nav', { index }); @@ -80,26 +73,13 @@ VantComponent({ const { items, mainActiveIndex } = this.data; const { children = [] } = items[mainActiveIndex] || {}; - this.updateItemHeight(children); return this.set({ subItems: children }); }, - // 更新组件整体高度,根据最大高度和当前组件需要展示的高度来决定 - updateMainHeight() { - const { items = [], subItems = [] } = this.data; - const maxHeight = Math.max( - items.length * ITEM_HEIGHT, - subItems.length * ITEM_HEIGHT - ); - - this.setData({ mainHeight: Math.min(maxHeight, this.data.maxHeight) }); - }, - - // 更新子项列表高度,根据可展示的最大高度和当前子项列表的高度决定 - updateItemHeight(subItems) { - const itemHeight = Math.min(subItems.length * ITEM_HEIGHT, this.data.maxHeight); - - return this.setData({ itemHeight }); + updateHeight() { + this.setData({ + innerHeight: addUnit(this.data.height) + }); } } }); diff --git a/packages/tree-select/index.wxml b/packages/tree-select/index.wxml index baa6a221..0e8df176 100644 --- a/packages/tree-select/index.wxml +++ b/packages/tree-select/index.wxml @@ -3,29 +3,27 @@ - - {{ item.text }} - + + + - +