diff --git a/document/components/docs/en-US/toast.md b/document/components/docs/en-US/toast.md index c16e4ab5644f0b3c7b2b0080f76d0814c1c2df35..ec1ba5a84a6fd570bb37092f6496febb38cd811d 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 f6614d93ae2cb861948cdd2636db3e85025808f8..d513fcb9bb5373a3dc31a66c15980236b7610d22 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 e159452a8a9e63b428278ab6ab0c7cef22a3df6a..872c559c08cb6745b4059777fa0bc3ec8da4e951 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 4b00665cc58064331c112660db73399b155b8b16..4c1c4c54828aafdd793acaa1e1d39b1a3fea2868 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 e6d340cf15ac03723d1af7eb095dce1997c0cef5..885389d9a50e9e624831d89a15a851cb6def4806 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 98b3e5b3d6815b767073bfd51a1cb2d6971f3e80..eee4adc2abab339116b63163432edd904a7f8541 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 + }) + } }) })