Skip to content

Commit ef9314e

Browse files
committed
feat: add calendar-change event
1 parent ee45d10 commit ef9314e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/calendar.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<script>
7474
import { isValidDate, isDateObejct, formatDate } from '@/utils/index'
7575
import locale from '@/mixins/locale'
76+
import emitter from '@/mixins/emitter'
7677
import scrollIntoView from '@/utils/scroll-into-view'
7778
import PanelDate from '@/panel/date'
7879
import PanelYear from '@/panel/year'
@@ -82,7 +83,7 @@ import PanelTime from '@/panel/time'
8283
export default {
8384
name: 'CalendarPanel',
8485
components: { PanelDate, PanelYear, PanelMonth, PanelTime },
85-
mixins: [locale],
86+
mixins: [locale, emitter],
8687
props: {
8788
value: {
8889
default: null,
@@ -203,7 +204,7 @@ export default {
203204
},
204205
methods: {
205206
handelPanelChange (panel, oldPanel) {
206-
this.$parent.$emit('panel-change', panel, oldPanel)
207+
this.dispatch('DatePicker', 'panel-change', [panel, oldPanel])
207208
if (panel === 'YEAR') {
208209
this.firstYear = Math.floor(this.calendarYear / 10) * 10
209210
} else if (panel === 'TIME') {
@@ -231,11 +232,13 @@ export default {
231232
} else {
232233
this.showPanelNone()
233234
}
234-
this.updateNow(this.value)
235235
},
236236
// 根据value更新日历
237237
updateNow (value) {
238-
this.now = value ? new Date(value) : new Date()
238+
const now = value ? new Date(value) : new Date()
239+
const oldNow = new Date(this.now)
240+
this.dispatch('DatePicker', 'calendar-change', [now, oldNow])
241+
this.now = now
239242
},
240243
getCriticalTime (value) {
241244
if (!value) {
@@ -334,10 +337,10 @@ export default {
334337
this.$emit('select-time', time, true)
335338
},
336339
changeCalendarYear (year) {
337-
this.now = new Date(year, this.calendarMonth)
340+
this.updateNow(new Date(year, this.calendarMonth))
338341
},
339342
changeCalendarMonth (month) {
340-
this.now = new Date(this.calendarYear, month)
343+
this.updateNow(new Date(this.calendarYear, month))
341344
},
342345
getSibling () {
343346
const calendars = this.$parent.$children.filter(v => v.$options.name === this.$options.name)

src/mixins/emitter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
export default {
3+
methods: {
4+
dispatch (componentName, eventName, params) {
5+
let parent = this.$parent || this.$root
6+
let name = parent.$options.name
7+
8+
while (parent && (!name || name !== componentName)) {
9+
parent = parent.$parent
10+
11+
if (parent) {
12+
name = parent.$options.name
13+
}
14+
}
15+
if (name && name === componentName) {
16+
parent = parent || this
17+
parent.$emit.apply(parent, [eventName].concat(params))
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)