Skip to content

Commit b074be6

Browse files
authored
fix(MdDatePicker): close datepicker on blur (#2372)
- fixes issue #2371 - fixes issue #2375 - fixes issue #2343 - fixes issue #2372
1 parent 6ddf96b commit b074be6

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/components/MdDatepicker/MdDatepicker.vue

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<template>
22
<md-field :class="['md-datepicker', { 'md-native': !mdOverrideNative }]" :md-clearable="mdClearable" @md-clear="onClear">
3-
<md-date-icon class="md-date-icon" @click.native="toggleDialog" />
4-
<md-input :type="type" ref="input" v-model="inputDate" @focus.native="onFocus" :pattern="pattern" />
3+
<md-date-icon class="md-date-icon" @click.native="toggleDialog(true)" />
4+
<md-input :type="type" ref="input" v-model="inputDate" @focus.native="onFocus" @focusout.native="onFocusOut" :pattern="pattern" />
55

66
<slot />
77

88
<keep-alive>
99
<md-datepicker-dialog
1010
v-if="showDialog"
11+
ref="mdRef"
1112
:md-date.sync="localDate"
1213
:md-disabled-dates="mdDisabledDates"
1314
:mdImmediately="mdImmediately"
14-
@md-closed="toggleDialog"
15+
@md-closed="toggleDialog(false)"
1516
:md-placement="mdPlacement"
1617
/>
1718
</keep-alive>
1819

19-
<md-overlay class="md-datepicker-overlay" md-fixed :md-active="showDialog" @click="toggleDialog" />
20+
<md-overlay class="md-datepicker-overlay" md-fixed :md-active="showDialog" @click="toggleDialog(false)" />
2021
</md-field>
2122
</template>
2223

@@ -50,6 +51,10 @@
5051
type: Boolean,
5152
default: true
5253
},
54+
mdCloseOnBlur: {
55+
type: Boolean,
56+
default: true
57+
},
5358
mdOverrideNative: {
5459
type: Boolean,
5560
default: true
@@ -141,7 +146,7 @@
141146
}
142147
},
143148
watch: {
144-
inputDate (value) {
149+
inputDate () {
145150
this.inputDateToLocalDate()
146151
},
147152
localDate () {
@@ -186,9 +191,10 @@
186191
}
187192
},
188193
methods: {
189-
toggleDialog () {
194+
toggleDialog (newState = null) {
190195
if (!isFirefox || this.mdOverrideNative) {
191-
this.showDialog = !this.showDialog
196+
// If new state (boolean) is provide, assign that to showDialog, else just toggle
197+
this.showDialog = newState === null ? !this.showDialog : newState
192198
if (this.showDialog) {
193199
this.$emit('md-opened')
194200
} else {
@@ -200,7 +206,12 @@
200206
},
201207
onFocus () {
202208
if (this.mdOpenOnFocus) {
203-
this.toggleDialog()
209+
this.toggleDialog(true)
210+
}
211+
},
212+
onFocusOut (e) {
213+
if (this.mdCloseOnBlur && this.$refs.mdRef.$el !== e.relatedTarget) {
214+
this.toggleDialog(false)
204215
}
205216
},
206217
inputDateToLocalDate () {

src/components/MdDatepicker/MdDatepickerDialog.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<md-popover :md-settings="popperSettings" md-active>
33
<transition name="md-datepicker-dialog" appear @enter="setContentStyles" @after-leave="resetDate">
4-
<div class="md-datepicker-dialog" :class="[$mdActiveTheme]">
4+
<div tabindex="-1" class="md-datepicker-dialog" :class="[$mdActiveTheme]">
55
<div class="md-datepicker-header">
66
<span class="md-datepicker-year-select" :class="{ 'md-selected': currentView === 'year' }" @click="currentView = 'year'">{{ selectedYear }}</span>
77
<div class="md-datepicker-date-select" :class="{ 'md-selected': currentView !== 'year' }" @click="currentView = 'day'">

0 commit comments

Comments
 (0)