From dc7e3d2453485cd8a9b1461617acb649979d3fec Mon Sep 17 00:00:00 2001 From: Lindy <33708359+Lindysen@users.noreply.github.com> Date: Sun, 24 May 2020 19:39:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(Collapse):=20add=20open=E3=80=81close=20ev?= =?UTF-8?q?ent=20(#3176)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/pages/collapse/index.js | 15 ++++++++++++++- example/pages/collapse/index.wxml | 10 ++++++++++ packages/collapse/README.md | 25 +++++++++++++++++++++++++ packages/collapse/index.ts | 26 +++++++++++++++++--------- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/example/pages/collapse/index.js b/example/pages/collapse/index.js index 39e097f4..25a1d364 100644 --- a/example/pages/collapse/index.js +++ b/example/pages/collapse/index.js @@ -1,4 +1,5 @@ import Page from '../../common/page'; +import Toast from '../../dist/toast/toast'; Page({ data: { @@ -9,13 +10,25 @@ Page({ title2: '有赞零售', title3: '有赞美业', content1: '提供多样店铺模板,快速搭建网上商城', - content2: '网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失', + content2: + '网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失', content3: '线上拓客,随时预约,贴心顺手的开单收银' }, + onChange(event) { const { key } = event.currentTarget.dataset; this.setData({ [key]: event.detail }); + }, + + onOpen(event) { + const openItem = event.detail; + Toast(`当前展开的面板的name: ${openItem}`); + }, + + onClose(event) { + const closeItem = event.detail; + Toast(`当前关闭的面板的name: ${closeItem}`); } }); diff --git a/example/pages/collapse/index.wxml b/example/pages/collapse/index.wxml index 24a6ecab..6afa1e10 100644 --- a/example/pages/collapse/index.wxml +++ b/example/pages/collapse/index.wxml @@ -14,6 +14,14 @@ + + + {{ content1 }} + {{ content2 }} + {{ content3 }} + + + @@ -32,3 +40,5 @@ + + diff --git a/packages/collapse/README.md b/packages/collapse/README.md index 81e7f3f5..a32a7acf 100644 --- a/packages/collapse/README.md +++ b/packages/collapse/README.md @@ -75,6 +75,29 @@ Page({ }); ``` +### 事件监听 + +`van-collapse` 提供了 `change`, `open` 和 `close` 事件。`change` 事件在面板切换时触发。`open` 事件在面板展开时触发。`close` 事件在面板关闭时触发。 + +```html + + + 提供多样店铺模板,快速搭建网上商城 + + + 网店吸粉获客、会员分层营销、一机多种收款,告别经营低效和客户流失 + + + 线上拓客,随时预约,贴心顺手的开单收银 + + +``` + ### 自定义标题内容 ```html @@ -117,6 +140,8 @@ Page({ | 事件名 | 说明 | 参数 | | ------ | -------------- | ------------------------------ | | change | 切换面板时触发 | activeNames: _string \| Array_ | +| open | 展开面板时触发 | currentName: _string \| number_ | +| close | 关闭面板时触发 | currentName: _string \| number_ | ### CollapseItem Props diff --git a/packages/collapse/index.ts b/packages/collapse/index.ts index 7d10c582..a58425eb 100644 --- a/packages/collapse/index.ts +++ b/packages/collapse/index.ts @@ -6,22 +6,22 @@ VantComponent({ relation: { name: 'collapse-item', type: 'descendant', - current: 'collapse', + current: 'collapse' }, props: { value: { type: null, - observer: 'updateExpanded', + observer: 'updateExpanded' }, accordion: { type: Boolean, - observer: 'updateExpanded', + observer: 'updateExpanded' }, border: { type: Boolean, - value: true, - }, + value: true + } }, methods: { @@ -33,17 +33,25 @@ VantComponent({ switch(name: string | number, expanded: boolean) { const { accordion, value } = this.data; + const changeItem = name; if (!accordion) { name = expanded ? (value || []).concat(name) : (value || []).filter( - (activeName: string | number) => activeName !== name - ); + (activeName: string | number) => activeName !== name + ); } else { name = expanded ? name : ''; } + + if (expanded) { + this.$emit('open', changeItem); + } else { + this.$emit('close', changeItem); + } + this.$emit('change', name); this.$emit('input', name); - }, - }, + } + } });