fix(message): fix message component trigger many times bug #1269 (#1270)

* fix(message): fix message component trigger many times bug #1269

* fix(message): fix message component trigger many times bug #1269

* fix(message): delete wait method
This commit is contained in:
fabin5 2021-01-28 10:46:26 +08:00 committed by GitHub
parent 6681bd4a1f
commit f8177ab68c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 39 deletions

View File

@ -41,19 +41,21 @@ describe('Message on command', () => {
test('it should close all messages', async () => { test('it should close all messages', async () => {
const onClose = jest.fn() const onClose = jest.fn()
const instances = []
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
Message({ const instance = Message({
onClose, onClose,
}) })
instances.push(instance)
} }
const elements = document.querySelectorAll(selector) const elements = document.querySelectorAll(selector)
expect(elements.length).toBe(4) expect(elements.length).toBe(4)
Message.closeAll() Message.closeAll()
await Vue.nextTick() await Vue.nextTick()
for (let i = 0; i < instances.length; i++) {
for (let i = 0; i < elements.length; i++) { const instance = instances[i]
elements[i].parentElement.dispatchEvent(new Event('transitionend')) instance.close()
} }
expect(onClose).toHaveBeenCalledTimes(4) expect(onClose).toHaveBeenCalledTimes(4)

View File

@ -85,20 +85,6 @@ describe('Message.vue', () => {
expect(domExports.off).toHaveBeenCalled() expect(domExports.off).toHaveBeenCalled()
}) })
test('should listen to transitionend on close', async () => {
jest.spyOn(domExports, 'on')
jest.spyOn(domExports, 'off')
const wrapper = _mount({
slots: { default: AXIOM },
})
wrapper.vm.close()
await nextTick()
expect(domExports.on).toHaveBeenLastCalledWith(
wrapper.vm.$el,
'transitionend',
wrapper.vm.destroyElement,
)
})
}) })
describe('Message.type', () => { describe('Message.type', () => {
@ -132,7 +118,8 @@ describe('Message.vue', () => {
const closeBtn = wrapper.find('.el-message__closeBtn') const closeBtn = wrapper.find('.el-message__closeBtn')
expect(closeBtn.exists()).toBe(true) expect(closeBtn.exists()).toBe(true)
wrapper.vm.destroyElement() wrapper.vm.visible = false
wrapper.vm.onClose()
expect(onClose).toHaveBeenCalled() expect(onClose).toHaveBeenCalled()
}) })
@ -198,7 +185,7 @@ describe('Message.vue', () => {
wrapper.vm.close() wrapper.vm.close()
expect(wrapper.vm.closed).toBe(true) expect(wrapper.vm.closed).toBe(true)
await nextTick() await nextTick()
await wrapper.trigger('transitionend') await wrapper.vm.onClose()
expect(onClose).toHaveBeenCalledTimes(1) expect(onClose).toHaveBeenCalledTimes(1)
}) })
}) })

View File

@ -1,5 +1,5 @@
<template> <template>
<transition name="el-message-fade"> <transition name="el-message-fade" @after-leave="onClose">
<div <div
v-show="visible" v-show="visible"
:id="id" :id="id"
@ -38,14 +38,12 @@ import { defineComponent, computed, ref, PropType } from 'vue'
import type { MessageVM } from './types' import type { MessageVM } from './types'
import { EVENT_CODE } from '../../utils/aria' import { EVENT_CODE } from '../../utils/aria'
import { on, off } from '../../utils/dom' import { on, off } from '../../utils/dom'
const TypeMap: Indexable<string> = { const TypeMap: Indexable<string> = {
success: 'success', success: 'success',
info: 'info', info: 'info',
warning: 'warning', warning: 'warning',
error: 'error', error: 'error',
} }
export default defineComponent({ export default defineComponent({
name: 'ElMessage', name: 'ElMessage',
props: { props: {
@ -75,18 +73,15 @@ export default defineComponent({
? `el-message__icon el-icon-${TypeMap[type]}` ? `el-message__icon el-icon-${TypeMap[type]}`
: '' : ''
}) })
const customStyle = computed(() => { const customStyle = computed(() => {
return { return {
top: `${props.offset}px`, top: `${props.offset}px`,
zIndex: props.zIndex, zIndex: props.zIndex,
} }
}) })
const visible = ref(false) const visible = ref(false)
const closed = ref(false) const closed = ref(false)
const timer = ref(null) const timer = ref(null)
return { return {
typeClass, typeClass,
customStyle, customStyle,
@ -95,14 +90,6 @@ export default defineComponent({
timer, timer,
} }
}, },
watch: {
closed(newVal: boolean) {
if (newVal) {
this.visible = false
on(this.$el, 'transitionend', this.destroyElement)
}
},
},
mounted() { mounted() {
this.startTimer() this.startTimer()
this.visible = true this.visible = true
@ -112,11 +99,6 @@ export default defineComponent({
off(document, 'keydown', this.keydown) off(document, 'keydown', this.keydown)
}, },
methods: { methods: {
destroyElement() {
this.visible = false
off(this.$el, 'transitionend', this.destroyElement)
this.onClose()
},
// start counting down to destroy message instance // start counting down to destroy message instance
startTimer() { startTimer() {
if (this.duration > 0) { if (this.duration > 0) {
@ -135,6 +117,7 @@ export default defineComponent({
// Event handlers // Event handlers
close() { close() {
this.closed = true this.closed = true
this.visible = false
this.timer = null this.timer = null
}, },
keydown({ code }: KeyboardEvent) { keydown({ code }: KeyboardEvent) {