[Improvement] Actionsheet: add select event (#1594)

This commit is contained in:
neverland 2018-08-06 11:46:10 +08:00 committed by GitHub
parent 58eab6f2df
commit 1f67c28998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 36 deletions

View File

@ -2,12 +2,18 @@
<demo-section>
<demo-block :title="$t('basicUsage')">
<van-button @click="show1 = true">{{ $t('button1') }}</van-button>
<van-actionsheet v-model="show1" :actions="actions" />
<van-actionsheet v-model="show1" :actions="actions" @select="onSelect" />
</demo-block>
<demo-block :title="$t('title2')">
<van-button @click="show2 = true">{{ $t('button2') }}</van-button>
<van-actionsheet v-model="show2" :actions="actions" :cancel-text="$t('cancel')" @cancel="handleCancel" />
<van-actionsheet
v-model="show2"
:actions="actions"
:cancel-text="$t('cancel')"
@cancel="onCancel"
@select="onSelect"
/>
</demo-block>
<demo-block :title="$t('title3')">
@ -53,20 +59,22 @@ export default {
computed: {
actions() {
return [
{ name: this.$t('option'), callback: this.onClick },
{ name: this.$t('option') },
{ name: this.$t('option'), subname: this.$t('description') },
{ name: this.$t('option'), loading: true },
{ loading: true },
{ name: this.$t('disabledOption'), disabled: true }
];
}
},
methods: {
onClick(item) {
onSelect(item) {
this.show1 = false;
this.show2 = false;
this.$toast(item.name);
},
handleCancel() {
onCancel() {
this.$toast('cancel');
}
}
@ -75,10 +83,6 @@ export default {
<style lang="postcss">
.demo-actionsheet {
.actionsheet-wx {
color: #06bf04;
}
.van-button {
margin-left: 15px;
}

View File

@ -13,7 +13,11 @@ Vue.use(Actionsheet);
Use `actions` prop to set options of actionsheet.
```html
<van-actionsheet v-model="show" :actions="actions" />
<van-actionsheet
v-model="show"
:actions="actions"
@select="onSelect"
/>
```
```javascript
@ -23,15 +27,13 @@ export default {
show: false,
actions: [
{
name: 'Option',
callback: this.onClick
name: 'Option'
},
{
name: 'Option',
description: 'Description'
},
{
name: 'Option',
loading: true
},
{
@ -43,7 +45,8 @@ export default {
},
methods: {
onClick(item) {
onSelect(item) {
this.show = false;
Toast(item.name);
}
}
@ -53,7 +56,13 @@ export default {
#### Actionsheet with cancel button
```html
<van-actionsheet v-model="show" :actions="actions" cancel-text="Cancel" />
<van-actionsheet
v-model="show"
:actions="actions"
cancel-text="Cancel"
@select="onSelect"
@calcel="onCancel"
/>
```
#### Actionsheet with title
@ -72,8 +81,8 @@ Actionsheet will get another style if there is a `title` prop.
| actions | Options | `Array` | `[]` |
| title | Title | `String` | - |
| cancel-text | Text of cancel button | `String` | - |
| overlay | Whether to show overlay | `Boolean` | - |
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | - |
| overlay | Whether to show overlay | `Boolean` | `true` |
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | `true` |
| lazy-render | Whether to lazy render util appeared | `Boolean` | `true` |
| get-container | Return the mount node for actionsheet | `() => HTMLElement` | - |
@ -81,6 +90,7 @@ Actionsheet will get another style if there is a `title` prop.
| Event | Description | Arguments |
|-----------|-----------|-----------|
| select | Triggered when click option | item |
| cancel | Triggered when cancel click | - |
### Data struct of actions
@ -92,4 +102,3 @@ Actionsheet will get another style if there is a `title` prop.
| className | className for the option |
| loading | Whether to be loading status |
| disabled | Whether to be disabled |
| callback | Triggered when click option |

View File

@ -9,7 +9,7 @@
<li
v-for="item in actions"
:class="[b('item', { disabled: item.disabled || item.loading }), item.className, 'van-hairline--top']"
@click.stop="onClickItem(item)"
@click.stop="onSelect(item)"
>
<template v-if="!item.loading">
<span :class="b('name')">{{ item.name }}</span>
@ -59,9 +59,10 @@ export default create({
},
methods: {
onClickItem(item) {
if (item.callback && !item.disabled && !item.loading) {
item.callback(item);
onSelect(item) {
if (!item.disabled && !item.loading) {
item.callback && item.callback(item);
this.$emit('select', item);
}
},

View File

@ -6,7 +6,7 @@ exports[`callback events 1`] = `
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
<!---->
</li>
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
<li class="van-actionsheet__item van-actionsheet__item--disabled van-hairline--top"><span class="van-actionsheet__name">Option</span>
<!---->
</li>
</ul>

View File

@ -8,17 +8,21 @@ test('callback events', () => {
value: true,
actions: [
{ name: 'Option', callback },
{ name: 'Option' }
{ name: 'Option', disabled: true }
],
cancelText: 'Cancel'
}
});
wrapper.findAll('li').trigger('click');
const options = wrapper.findAll('li');
options.at(0).trigger('click');
options.at(1).trigger('click');
wrapper.find('.van-actionsheet__cancel').trigger('click');
expect(callback.mock.calls.length).toBe(1);
expect(wrapper.emitted('cancel')).toBeTruthy();
expect(wrapper.emitted('input')[0][0]).toBeFalsy();
expect(wrapper.emitted('select')[0][0]).toBeTruthy();
expect(wrapper.emitted('select')[0][1]).toBeFalsy();
expect(wrapper).toMatchSnapshot();
});

View File

@ -13,7 +13,11 @@ Vue.use(Actionsheet);
需要传入一个`actions`的数组,数组的每一项是一个对象,对象属性见文档下方表格。
```html
<van-actionsheet v-model="show" :actions="actions" />
<van-actionsheet
v-model="show"
:actions="actions"
@select="onSelect"
/>
```
```javascript
@ -23,15 +27,13 @@ export default {
show: false,
actions: [
{
name: '选项',
callback: this.onClick
name: '选项'
},
{
name: '选项',
subname: '描述信息'
},
{
name: '选项',
loading: true
},
{
@ -43,7 +45,9 @@ export default {
},
methods: {
onClick(item) {
onSelect(item) {
// 点击选项时默认不会关闭菜单,可以手动关闭
this.show = false;
Toast(item.name);
}
}
@ -55,7 +59,13 @@ export default {
如果传入了`cancelText`属性,且不为空,则会在下方显示一个取消按钮,点击会将当前`Actionsheet`关闭。
```html
<van-actionsheet v-model="show" :actions="actions" cancel-text="取消" />
<van-actionsheet
v-model="show"
:actions="actions"
cancel-text="取消"
@select="onSelect"
@calcel="onCancel"
/>
```
#### 带标题的 Actionsheet
@ -74,9 +84,9 @@ export default {
|-----------|-----------|-----------|-------------|
| actions | 菜单选项 | `Array` | `[]` |
| title | 标题 | `String` | - |
| cancel-text | 取消按钮文字 | `String` | - |
| overlay | 是否显示遮罩层 | `Boolean` | - |
| close-on-click-overlay | 点击遮罩是否关闭菜单 | `Boolean` | - |
| cancel-text | 取消按钮文字,为空时不展示取消按钮 | `String` | - |
| overlay | 是否显示遮罩层 | `Boolean` | `true` |
| close-on-click-overlay | 点击遮罩是否关闭菜单 | `Boolean` | `true` |
| lazy-render | 是否在首次显示弹层时才渲染 DOM 节点 | `Boolean` | `true` |
| get-container | 指定挂载的 HTML 节点 | `() => HTMLElement` | - |
@ -84,6 +94,7 @@ export default {
| 事件名 | 说明 | 参数 |
|-----------|-----------|-----------|
| select | 选中选项时触发,禁用或加载状态下不会触发 | item: 选项对应的对象 |
| cancel | 取消按钮点击时触发 | - |
### actions
@ -97,4 +108,3 @@ export default {
| className | 为对应列添加额外的 class |
| loading | 是否为加载状态 |
| disabled | 是否为禁用状态 |
| callback | 点击时的回调。该回调接受一个参数,参数为当前点击 action 的对象信息 |