[improvement] Actionsheet: add cancel event (#796)

This commit is contained in:
张敏 2018-03-29 11:43:21 +08:00 committed by GitHub
parent 99361427f9
commit 1c2b45503e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 3 deletions

View File

@ -7,7 +7,7 @@
<demo-block :title="$t('title2')"> <demo-block :title="$t('title2')">
<van-button @click="show2 = true">{{ $t('button2') }}</van-button> <van-button @click="show2 = true">{{ $t('button2') }}</van-button>
<van-actionsheet v-model="show2" :actions="actions" :cancel-text="$t('cancel')" /> <van-actionsheet v-model="show2" :actions="actions" :cancel-text="$t('cancel')" @cancel="handleCancel" />
</demo-block> </demo-block>
<demo-block :title="$t('title3')"> <demo-block :title="$t('title3')">
@ -61,6 +61,10 @@ export default {
methods: { methods: {
onClick(item) { onClick(item) {
Toast(item.name); Toast(item.name);
},
handleCancel() {
Toast('cancel');
} }
} }
}; };

View File

@ -72,6 +72,12 @@ Actionsheet will get another style if there is a `title` prop.
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | - | - | | close-on-click-overlay | Whether to close when click overlay | `Boolean` | - | - |
| get-container | Return the mount node for actionsheet | `Function` | - | `() => HTMLElement` | | get-container | Return the mount node for actionsheet | `Function` | - | `() => HTMLElement` |
### Event
| Event | Description | Arguments |
|-----------|-----------|-----------|
| cancel | Triggered when cancel click | - |
### Data struct of actions ### Data struct of actions
| key | Description | | key | Description |

View File

@ -75,6 +75,12 @@ export default {
| close-on-click-overlay | 点击遮罩是否关闭`Actionsheet` | `Boolean` | - | - | | close-on-click-overlay | 点击遮罩是否关闭`Actionsheet` | `Boolean` | - | - |
| get-container | 指定挂载的 HTML 节点 | `Function` | - | `() => HTMLElement` | | get-container | 指定挂载的 HTML 节点 | `Function` | - | `() => HTMLElement` |
### Event
| 事件名 | 说明 | 参数 |
|-----------|-----------|-----------|
| cancel | 取消按钮点击时触发 | - |
### actions ### actions
`API`中的`actions`为一个对象数组,数组中的每一个对象配置每一列,每一列有以下`key` `API`中的`actions`为一个对象数组,数组中的每一个对象配置每一列,每一列有以下`key`

View File

@ -3,7 +3,7 @@
<div class="van-actionsheet" :class="{ 'van-actionsheet--withtitle': title }" v-show="value"> <div class="van-actionsheet" :class="{ 'van-actionsheet--withtitle': title }" v-show="value">
<div class="van-actionsheet__header van-hairline--top-bottom" v-if="title"> <div class="van-actionsheet__header van-hairline--top-bottom" v-if="title">
<div v-text="title" /> <div v-text="title" />
<icon name="close" @click="$emit('input', false)" /> <icon name="close" @click="handleCancel" />
</div> </div>
<ul v-else class="van-hairline--bottom"> <ul v-else class="van-hairline--bottom">
<li <li
@ -24,7 +24,7 @@
v-if="cancelText" v-if="cancelText"
v-text="cancelText" v-text="cancelText"
class="van-actionsheet__item van-actionsheet__cancel van-hairline--top" class="van-actionsheet__item van-actionsheet__cancel van-hairline--top"
@click="$emit('input', false)" @click="handleCancel"
/> />
<div v-else class="van-actionsheet__content"> <div v-else class="van-actionsheet__content">
<slot /> <slot />
@ -65,6 +65,11 @@ export default create({
if (typeof item.callback === 'function') { if (typeof item.callback === 'function') {
item.callback(item); item.callback(item);
} }
},
handleCancel() {
this.$emit('input', false);
this.$emit('cancel');
} }
} }
}); });

View File

@ -107,6 +107,11 @@ describe('ActionSheet', () => {
const cancelButton = wrapper.find('.van-actionsheet__cancel')[0]; const cancelButton = wrapper.find('.van-actionsheet__cancel')[0];
expect(wrapper.contains('.van-actionsheet__cancel')).to.be.true; expect(wrapper.contains('.van-actionsheet__cancel')).to.be.true;
expect(cancelButton.text()).to.equal('cancel'); expect(cancelButton.text()).to.equal('cancel');
const eventStub = sinon.stub(wrapper.vm, '$emit');
cancelButton.trigger('click');
expect(eventStub.calledWith('cancel'));
}); });
it('toggle actionsheet value from v-model', (done) => { it('toggle actionsheet value from v-model', (done) => {