From 6a7b9ee3b8d36f58d7326025a1936d0b6219bde3 Mon Sep 17 00:00:00 2001 From: Amy Date: Tue, 3 Apr 2018 11:23:40 +0800 Subject: [PATCH] Toast dev (#149) * feat(toast): event hide * test(toast): event hide * feat: event hide -> timeout * docs(toast): event timout --- document/components/docs/en-US/toast.md | 6 ++++++ document/components/docs/zh-CN/toast.md | 6 ++++++ example/pages/toast.vue | 5 ++++- src/components/toast/toast.vue | 3 +++ src/modules/toast/api.js | 2 +- test/unit/specs/toast.spec.js | 24 ++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/document/components/docs/en-US/toast.md b/document/components/docs/en-US/toast.md index c16e4ab5..ec1ba5a8 100644 --- a/document/components/docs/en-US/toast.md +++ b/document/components/docs/en-US/toast.md @@ -85,3 +85,9 @@ | mask | whether to show mask layer | Boolean | true/false | false | | txt | tip text | String | - | '' | | time | display duration, millisecond | Number | - | 3000 | + +### Events + +| Event Name | Description | +| - | - | +| timeout | triggers when the display time is out | diff --git a/document/components/docs/zh-CN/toast.md b/document/components/docs/zh-CN/toast.md index f6614d93..d513fcb9 100644 --- a/document/components/docs/zh-CN/toast.md +++ b/document/components/docs/zh-CN/toast.md @@ -86,3 +86,9 @@ | mask | 遮罩 | Boolean | true/false | false | | txt | 提示信息 | String | - | '' | | time | 显示时间 | Number | - | 3000 | + +### 事件 + +| 事件名 | 说明 | +| - | - | +| timeout | 当显示时间结束时派发 | diff --git a/example/pages/toast.vue b/example/pages/toast.vue index e159452a..872c559c 100644 --- a/example/pages/toast.vue +++ b/example/pages/toast.vue @@ -20,7 +20,10 @@ showToastTime() { this.toast = this.$createToast({ time: 1000, - txt: 'Toast time 1s' + txt: 'Toast time 1s', + onHide: () => { + console.log('hide') + } }) this.toast.show() }, diff --git a/src/components/toast/toast.vue b/src/components/toast/toast.vue index 4b00665c..4c1c4c54 100644 --- a/src/components/toast/toast.vue +++ b/src/components/toast/toast.vue @@ -14,6 +14,8 @@ const COMPONENT_NAME = 'cube-toast' + const EVENT_TIMEOUT = 'timeout' + export default { name: COMPONENT_NAME, mixins: [apiMixin], @@ -65,6 +67,7 @@ if (this.time !== 0) { this.timer = setTimeout(() => { this.hide() + this.$emit(EVENT_TIMEOUT) }, this.time) } }) diff --git a/src/modules/toast/api.js b/src/modules/toast/api.js index e6d340cf..885389d9 100644 --- a/src/modules/toast/api.js +++ b/src/modules/toast/api.js @@ -1,5 +1,5 @@ import createAPI from '../../common/helpers/create-api' export default function addToast (Vue, Toast) { - createAPI(Vue, Toast, [], true) + createAPI(Vue, Toast, ['timeout'], true) } diff --git a/test/unit/specs/toast.spec.js b/test/unit/specs/toast.spec.js index 98b3e5b3..eee4adc2 100644 --- a/test/unit/specs/toast.spec.js +++ b/test/unit/specs/toast.spec.js @@ -113,5 +113,29 @@ describe('Toast', () => { done() }) }) + + it('should trigger correct event', function (done) { + const timeoutHandle = sinon.spy() + + const vm = createToast({ + time: 1000 + }, { + timeout: timeoutHandle + }) + + vm.show() + + setTimeout(() => { + expect(timeoutHandle).to.be.calledOnce + done() + }, 1200) + }) + + function createToast(props = {}, events = {}) { + return instantiateComponent(Vue, Toast, { + props: props, + on: events + }) + } }) }) -- GitLab