feat(Collapse): add open、close event (#3176)

This commit is contained in:
Lindy 2020-05-24 19:39:40 +08:00 committed by GitHub
parent f2c1b58ada
commit dc7e3d2453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import Page from '../../common/page'; import Page from '../../common/page';
import Toast from '../../dist/toast/toast';
Page({ Page({
data: { data: {
@ -9,13 +10,25 @@ Page({
title2: '有赞零售', title2: '有赞零售',
title3: '有赞美业', title3: '有赞美业',
content1: '提供多样店铺模板,快速搭建网上商城', content1: '提供多样店铺模板,快速搭建网上商城',
content2: '网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失', content2:
'网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失',
content3: '线上拓客,随时预约,贴心顺手的开单收银' content3: '线上拓客,随时预约,贴心顺手的开单收银'
}, },
onChange(event) { onChange(event) {
const { key } = event.currentTarget.dataset; const { key } = event.currentTarget.dataset;
this.setData({ this.setData({
[key]: event.detail [key]: event.detail
}); });
},
onOpen(event) {
const openItem = event.detail;
Toast(`当前展开的面板的name: ${openItem}`);
},
onClose(event) {
const closeItem = event.detail;
Toast(`当前关闭的面板的name: ${closeItem}`);
} }
}); });

View File

@ -14,6 +14,14 @@
</van-collapse> </van-collapse>
</demo-block> </demo-block>
<demo-block title="监听面板打开关闭事件">
<van-collapse value="{{ active2 }}" data-key="active2" accordion bind:change="onChange" bind:open="onOpen" bind:close="onClose">
<van-collapse-item title="{{ title1 }}">{{ content1 }}</van-collapse-item>
<van-collapse-item title="{{ title2 }}">{{ content2 }}</van-collapse-item>
<van-collapse-item title="{{ title3 }}">{{ content3 }}</van-collapse-item>
</van-collapse>
</demo-block>
<demo-block title="自定义标题内容"> <demo-block title="自定义标题内容">
<van-collapse value="{{ active3 }}" data-key="active3" bind:change="onChange"> <van-collapse value="{{ active3 }}" data-key="active3" bind:change="onChange">
<van-collapse-item> <van-collapse-item>
@ -32,3 +40,5 @@
</van-collapse-item> </van-collapse-item>
</van-collapse> </van-collapse>
</demo-block> </demo-block>
<van-toast id="van-toast" />

View File

@ -75,6 +75,29 @@ Page({
}); });
``` ```
### 事件监听
`van-collapse` 提供了 `change`, `open``close` 事件。`change` 事件在面板切换时触发。`open` 事件在面板展开时触发。`close` 事件在面板关闭时触发。
```html
<van-collapse
value="{{ activeNames }}"
bind:change="onChange"
bind:open="onOpen"
bind:close="onClose"
>
<van-collapse-item title="有赞微商城" name="1">
提供多样店铺模板,快速搭建网上商城
</van-collapse-item>
<van-collapse-item title="有赞零售" name="2">
网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失
</van-collapse-item>
<van-collapse-item title="有赞美业" name="3">
线上拓客,随时预约,贴心顺手的开单收银
</van-collapse-item>
</van-collapse>
```
### 自定义标题内容 ### 自定义标题内容
```html ```html
@ -117,6 +140,8 @@ Page({
| 事件名 | 说明 | 参数 | | 事件名 | 说明 | 参数 |
| ------ | -------------- | ------------------------------ | | ------ | -------------- | ------------------------------ |
| change | 切换面板时触发 | activeNames: _string \| Array_ | | change | 切换面板时触发 | activeNames: _string \| Array_ |
| open | 展开面板时触发 | currentName: _string \| number_ |
| close | 关闭面板时触发 | currentName: _string \| number_ |
### CollapseItem Props ### CollapseItem Props

View File

@ -6,22 +6,22 @@ VantComponent({
relation: { relation: {
name: 'collapse-item', name: 'collapse-item',
type: 'descendant', type: 'descendant',
current: 'collapse', current: 'collapse'
}, },
props: { props: {
value: { value: {
type: null, type: null,
observer: 'updateExpanded', observer: 'updateExpanded'
}, },
accordion: { accordion: {
type: Boolean, type: Boolean,
observer: 'updateExpanded', observer: 'updateExpanded'
}, },
border: { border: {
type: Boolean, type: Boolean,
value: true, value: true
}, }
}, },
methods: { methods: {
@ -33,17 +33,25 @@ VantComponent({
switch(name: string | number, expanded: boolean) { switch(name: string | number, expanded: boolean) {
const { accordion, value } = this.data; const { accordion, value } = this.data;
const changeItem = name;
if (!accordion) { if (!accordion) {
name = expanded name = expanded
? (value || []).concat(name) ? (value || []).concat(name)
: (value || []).filter( : (value || []).filter(
(activeName: string | number) => activeName !== name (activeName: string | number) => activeName !== name
); );
} else { } else {
name = expanded ? name : ''; name = expanded ? name : '';
} }
if (expanded) {
this.$emit('open', changeItem);
} else {
this.$emit('close', changeItem);
}
this.$emit('change', name); this.$emit('change', name);
this.$emit('input', name); this.$emit('input', name);
}, }
}, }
}); });