Skip to content

Commit

Permalink
fix: the clickoutside don't work sometimes (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiong10 committed Oct 14, 2019
1 parent b5e726d commit d9619f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
33 changes: 0 additions & 33 deletions src/directives/clickoutside.js

This file was deleted.

32 changes: 27 additions & 5 deletions src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:style="{
'width': computedWidth
}"
v-clickoutside="closePopup">
>
<div class="mx-input-wrapper"
@click.stop="showPopup">
<input
Expand Down Expand Up @@ -115,7 +115,6 @@

<script>
import fecha from 'fecha'
import clickoutside from '@/directives/clickoutside'
import { isValidDate, isValidRangeDate, isDateObejct, isPlainObject, formatDate, parseDate, throttle } from '@/utils/index'
import { transformDate } from '@/utils/transform'
import CalendarPanel from './calendar.vue'
Expand All @@ -127,9 +126,6 @@ export default {
name: 'DatePicker',
components: { CalendarPanel },
mixins: [locale],
directives: {
clickoutside
},
props: {
value: null,
valueType: {
Expand Down Expand Up @@ -345,6 +341,30 @@ export default {
this.popupElm = this.$refs.calendar
document.body.appendChild(this.popupElm)
}
// clickoutside close popup
let mousedownTarget
this._bindDocmentMousedown = evt => {
mousedownTarget = evt.target
}
this._bindDocumentMouseup = evt => {
const mouseupTarget = evt.target
const el = this.$el
const { popupElm } = this
if (
!mousedownTarget ||
!mouseupTarget ||
el.contains(mousedownTarget) ||
el.contains(mouseupTarget) ||
(popupElm && (popupElm.contains(mousedownTarget) || popupElm.contains(mouseupTarget)))
) {
return
}
this.closePopup()
mousedownTarget = null
}
document.addEventListener('mousedown', this._bindDocmentMousedown)
document.addEventListener('mouseup', this._bindDocumentMouseup)
this._displayPopup = throttle(() => {
if (this.popupVisible) {
this.displayPopup()
Expand All @@ -357,6 +377,8 @@ export default {
if (this.popupElm && this.popupElm.parentNode === document.body) {
document.body.removeChild(this.popupElm)
}
document.removeEventListener('mousedown', this._bindDocmentMousedown)
document.removeEventListener('mouseup', this._bindDocumentMouseup)
window.removeEventListener('resize', this._displayPopup)
window.removeEventListener('scroll', this._displayPopup)
},
Expand Down

0 comments on commit d9619f8

Please sign in to comment.