Skip to content

Commit f62ec6a

Browse files
VdustRmarcosmoura
authored andcommitted
feat(MdDatepicker): add immediately option (#1607)
* feat(MdDatepicker): add immediately option click to select date without confirm and close the dialog immediately fix #1606 * fix(MdDatepicker): hide confirm button when mdImmediately is on
1 parent 7259038 commit f62ec6a

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

docs/app/pages/Components/Datepicker/Datepicker.vue

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<example src="./examples/BasicDatepicker.vue" />
22
<example src="./examples/LabeledDatepicker.vue" />
33
<example src="./examples/CancelOpenDatepicker.vue" />
4+
<example src="./examples/CloseOnSelectDatepicker.vue" />
45
<example src="./examples/DisabledDatesDatepicker.vue" />
56

67
<template>
@@ -23,6 +24,12 @@
2324
<code-example title="With initial date selected" :component="examples['cancel-open-datepicker']" />
2425
</div>
2526

27+
<div class="page-container-section">
28+
<h2>Immediately selection</h2>
29+
<p>Datepicker dialog could be closed instantly after a date is selected. Date will be selected immediately without any confirm:</p>
30+
<code-example title="Close dialog on select" :component="examples['close-on-select-datepicker']" />
31+
</div>
32+
2633
<div class="page-container-section">
2734
<h2>Disabled dates</h2>
2835
<p>Sometimes you may need to disable certain dates from being selected. Let's suppose that you only want to let user to select work days:</p>
@@ -66,6 +73,12 @@
6673
description: 'Disable the on focus event. Will open only if the user clicks on the icon.',
6774
defaults: 'true'
6875
},
76+
{
77+
name: 'md-immediately',
78+
type: 'Boolean',
79+
description: 'Select the date without confirm and close the dialog immediately.',
80+
defaults: 'false'
81+
},
6982
{
7083
name: 'md-override-native',
7184
type: 'Boolean',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<template>
2+
<div>
3+
<md-datepicker v-model="selectedDate" md-immediately />
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'CloseOnSelectDatepicker',
10+
data: () => ({
11+
selectedDate: new Date('2018/03/26')
12+
})
13+
}
14+
</script>

src/components/MdDatepicker/MdDatepicker.vue

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<slot />
77

88
<keep-alive>
9-
<md-datepicker-dialog :md-date.sync="selectedDate" :md-disabled-dates="mdDisabledDates" v-if="showDialog" @md-closed="toggleDialog" />
9+
<md-datepicker-dialog
10+
v-if="showDialog"
11+
:md-date.sync="selectedDate"
12+
:md-disabled-dates="mdDisabledDates"
13+
:mdImmediately="mdImmediately"
14+
@md-closed="toggleDialog"
15+
/>
1016
</keep-alive>
1117

1218
<md-overlay class="md-datepicker-overlay" md-fixed :md-active="showDialog" @click="toggleDialog" />
@@ -44,6 +50,10 @@
4450
mdOverrideNative: {
4551
type: Boolean,
4652
default: true
53+
},
54+
mdImmediately: {
55+
type: Boolean,
56+
default: false
4757
}
4858
},
4959
data: () => ({
@@ -77,6 +87,8 @@
7787
if (isValid(parsedDate)) {
7888
this.selectedDate = parsedDate
7989
}
90+
} else {
91+
this.selectedDate = null
8092
}
8193
}
8294
},

src/components/MdDatepicker/MdDatepickerDialog.vue

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<md-popover :md-settings="popperSettings" md-active>
3-
<transition name="md-datepicker-dialog" appear>
3+
<transition name="md-datepicker-dialog" appear @enter="setContentStyles" @after-leave="resetDate">
44
<div 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>
@@ -80,7 +80,7 @@
8080

8181
<md-dialog-actions class="md-datepicker-body-footer">
8282
<md-button class="md-primary" @click="onCancel">Cancel</md-button>
83-
<md-button class="md-primary" @click="onConfirm">Ok</md-button>
83+
<md-button v-if="!mdImmediately" class="md-primary" @click="onConfirm">Ok</md-button>
8484
</md-dialog-actions>
8585
</div>
8686
</div>
@@ -130,7 +130,11 @@
130130
},
131131
props: {
132132
mdDate: Date,
133-
mdDisabledDates: [Array, Function]
133+
mdDisabledDates: [Array, Function],
134+
mdImmediately: {
135+
type: Boolean,
136+
default: false
137+
}
134138
},
135139
data: () => ({
136140
currentDate: null,
@@ -314,7 +318,11 @@
314318
selectDate (day) {
315319
this.currentDate = setDate(this.currentDate, day)
316320
this.selectedDate = this.currentDate
317-
this.$emit('update:mdDate', this.selectedDate)
321+
322+
if (this.mdImmediately) {
323+
this.$emit('update:mdDate', this.selectedDate)
324+
this.closeDialog()
325+
}
318326
},
319327
closeDialog () {
320328
this.$emit('md-closed')
@@ -326,20 +334,19 @@
326334
this.closeDialog()
327335
},
328336
onConfirm () {
329-
this.closeDialog()
330337
this.$emit('update:mdDate', this.selectedDate)
338+
this.closeDialog()
339+
},
340+
resetDate () {
341+
this.currentDate = this.mdDate || new Date()
342+
this.selectedDate = this.mdDate
343+
this.currentView = 'day'
331344
}
332345
},
333346
created () {
334347
this.setAvailableYears()
335-
this.currentDate = this.mdDate || new Date()
336-
this.selectedDate = this.mdDate
337-
this.currentView = 'day'
338-
339-
window.setTimeout(() => {
340-
this.setContentStyles()
341-
}, 50)
342-
},
348+
this.resetDate()
349+
}
343350
})
344351
</script>
345352

0 commit comments

Comments
 (0)