From b8683f61a97fd89de6e5a8097791d559630aa4af Mon Sep 17 00:00:00 2001 From: rex Date: Wed, 24 Apr 2019 00:41:03 +0800 Subject: [PATCH] [new feature] Badge: add new event click & improve docs fix #1542 --- packages/badge-group/index.ts | 37 +++++++++++++++-------------------- packages/badge/README.md | 29 +++++++++++++++------------ packages/badge/index.ts | 21 +++++++++++++++----- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/packages/badge-group/index.ts b/packages/badge-group/index.ts index 7e7ca429..221a9b9a 100644 --- a/packages/badge-group/index.ts +++ b/packages/badge-group/index.ts @@ -1,5 +1,4 @@ import { VantComponent } from '../common/component'; -import { isNumber } from '../common/utils'; VantComponent({ relation: { @@ -7,52 +6,48 @@ VantComponent({ type: 'descendant', linked(target: Weapp.Component) { this.badges.push(target); - this.setActive(); + this.setActive(this.data.active); }, unlinked(target: Weapp.Component) { this.badges = this.badges.filter(item => item !== target); - this.setActive(); + this.setActive(this.data.active); } }, props: { active: { type: Number, - value: 0 + value: 0, + observer: 'setActive' } }, - watch: { - active: 'setActive' - }, - beforeCreate() { this.badges = []; this.currentActive = -1; }, methods: { - setActive(badge: Weapp.Component | number) { - let { active } = this.data; - const { badges } = this; + setActive(active: number) { + const { badges, currentActive } = this; - if (badge && !isNumber(badge)) { - active = badges.indexOf(badge); + if (!badges.length) { + return Promise.resolve(); } - if (active === this.currentActive) { - return; - } + this.currentActive = active; - if (this.currentActive !== -1 && badges[this.currentActive]) { - this.$emit('change', active); - badges[this.currentActive].setActive(false); + const stack = []; + + if (currentActive !== active && badges[currentActive]) { + stack.push(badges[currentActive].setActive(false)); } if (badges[active]) { - badges[active].setActive(true); - this.currentActive = active; + stack.push(badges[active].setActive(true)); } + + return Promise.all(stack); } } }); diff --git a/packages/badge/README.md b/packages/badge/README.md index f3f395eb..c9b466c0 100644 --- a/packages/badge/README.md +++ b/packages/badge/README.md @@ -55,12 +55,11 @@ Page({ |-----------|-----------|-----------|-------------| | active | 选中`badge`的索引 | `String | Number` | `0` | -### Badge API +### BadgeGroup Event -| 参数 | 说明 | 类型 | 默认值 | -|-----------|-----------|-----------|-------------| -| title | 内容 | `String` | `''` | -| info | 提示消息 | `String | Number` | `''` | +| 事件名 | 说明 | 参数 | +|------|------|------| +| change | 切换徽章时触发 | 当前选中徽章的索引 | ### BadgeGroup 外部样式类 @@ -68,15 +67,21 @@ Page({ |-----------|-----------| | custom-class | 根节点样式类 | +### Badge API + +| 参数 | 说明 | 类型 | 默认值 | +|-----------|-----------|-----------|-------------| +| title | 内容 | `String` | `''` | +| info | 提示消息 | `String | Number` | `''` | + +### Badge Event + +| 事件名 | 说明 | 参数 | +|------|------|------| +| click | 点击徽章时触发 | 当前徽章的索引 | + ### Badge 外部样式类 | 类名 | 说明 | |-----------|-----------| | custom-class | 根节点样式类 | - -### 更新日志 - -| 版本 | 类型 | 内容 | -|-----------|-----------|-----------| -| 0.0.1 | feature | 新增组件 | -| 0.3.2 | bugfix | 修复 active 属性不生效的问题 | diff --git a/packages/badge/index.ts b/packages/badge/index.ts index c8857364..71cd0b90 100644 --- a/packages/badge/index.ts +++ b/packages/badge/index.ts @@ -3,7 +3,10 @@ import { VantComponent } from '../common/component'; VantComponent({ relation: { type: 'ancestor', - name: 'badge-group' + name: 'badge-group', + linked(target: Weapp.Component) { + this.parent = target; + } }, props: { @@ -13,14 +16,22 @@ VantComponent({ methods: { onClick() { - const group = this.getRelationNodes('../badge-group/index')[0]; - if (group) { - group.setActive(this); + const { parent } = this; + + if (!parent) { + return; } + + const index = parent.badges.indexOf(this); + + parent.setActive(index).then(() => { + this.$emit('click', index); + parent.$emit('change', index); + }); }, setActive(active: boolean) { - this.set({ active }); + return this.set({ active }); } } });