From 77e43f2b871102554600b0d0a231be4647a703e3 Mon Sep 17 00:00:00 2001 From: Sergio Crisostomo Date: Fri, 19 Jan 2018 10:59:44 +0100 Subject: [PATCH] Correct year date navigation logic --- src/components/date-picker/panel/Date/date-range.vue | 44 +++++++++++++++++++++++++++++--------------- src/components/date-picker/panel/Date/date.vue | 6 +++++- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/components/date-picker/panel/Date/date-range.vue b/src/components/date-picker/panel/Date/date-range.vue index 7b23b3c..0e6300c 100644 --- a/src/components/date-picker/panel/Date/date-range.vue +++ b/src/components/date-picker/panel/Date/date-range.vue @@ -218,30 +218,44 @@ labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj)) }; }, - prevYear (direction) { - this.changePanelDate(direction, 'FullYear', -1); + prevYear (panel) { + console.log(this) + const increment = this.currentView === 'year' ? -10 : -1; + this.changePanelDate(panel, 'FullYear', increment); }, - nextYear (direction) { - this.changePanelDate(direction, 'FullYear', 1); + nextYear (panel) { + const increment = this.currentView === 'year' ? 10 : 1; + this.changePanelDate(panel, 'FullYear', increment); }, - prevMonth(direction){ - this.changePanelDate(direction, 'Month', -1); + prevMonth(panel){ + this.changePanelDate(panel, 'Month', -1); }, - nextMonth(direction){ - this.changePanelDate(direction, 'Month', 1); + nextMonth(panel){ + this.changePanelDate(panel, 'Month', 1); }, changePanelDate(panel, type, increment){ const current = new Date(this[`${panel}PanelDate`]); current[`set${type}`](current[`get${type}`]() + increment); this[`${panel}PanelDate`] = current; - // change other panels if they overlap - const otherPanel = panel === 'left' ? 'right' : 'left'; - if (panel === 'left' && this.leftPanelDate >= this.rightPanelDate){ - this.changePanelDate(otherPanel, type, 1); - } - if (panel === 'right' && this.rightPanelDate <= this.leftPanelDate){ - this.changePanelDate(otherPanel, type, -1); + + if (this.splitPanels){ + // change other panel if dates overlap + const otherPanel = panel === 'left' ? 'right' : 'left'; + if (panel === 'left' && this.leftPanelDate >= this.rightPanelDate){ + this.changePanelDate(otherPanel, type, 1); + } + if (panel === 'right' && this.rightPanelDate <= this.leftPanelDate){ + this.changePanelDate(otherPanel, type, -1); + } + } else { + // keep the panels together + const otherPanel = panel === 'left' ? 'right' : 'left'; + const otherCurrent = new Date(this[`${otherPanel}PanelDate`]); + otherCurrent[`set${type}`](otherCurrent[`get${type}`]() + increment); + if (current[`get${type}`]() !== otherCurrent[`get${type}`]()){ + this[`${otherPanel}PanelDate`] = otherCurrent; + } } }, showYearPicker () { diff --git a/src/components/date-picker/panel/Date/date.vue b/src/components/date-picker/panel/Date/date.vue index cd22f57..118799d 100644 --- a/src/components/date-picker/panel/Date/date.vue +++ b/src/components/date-picker/panel/Date/date.vue @@ -145,7 +145,11 @@ }, methods: { changeYear(dir){ - this.panelDate = siblingMonth(this.panelDate, dir * 12); + if (this.selectionMode === 'year'){ + this.panelDate = new Date(this.panelDate.getFullYear() + dir * 10, 0, 1); + } else { + this.panelDate = siblingMonth(this.panelDate, dir * 12); + } }, changeMonth(dir){ this.panelDate = siblingMonth(this.panelDate, dir); -- libgit2 0.21.4