Commit b52e02e4367002c01d04793c2e8059965aa26b80
1 parent
5426dcf9
Fix month|year preselection/shortcut when picking a date
Showing
4 changed files
with
56 additions
and
21 deletions
Show diff stats
src/components/date-picker/panel/Date/date-panel-mixin.js
src/components/date-picker/panel/Date/date-range.vue
... | ... | @@ -31,7 +31,7 @@ |
31 | 31 | v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span> |
32 | 32 | </div> |
33 | 33 | <component |
34 | - :is="pickerTable" | |
34 | + :is="leftPickerTable" | |
35 | 35 | ref="leftYearTable" |
36 | 36 | v-if="currentView !== 'time'" |
37 | 37 | :table-date="leftPanelDate" |
... | ... | @@ -41,7 +41,7 @@ |
41 | 41 | :show-week-numbers="showWeekNumbers" |
42 | 42 | :value="dates" |
43 | 43 | @on-change-range="handleChangeRange" |
44 | - @on-pick="handleRangePick" | |
44 | + @on-pick="panelPickerHandlers.left" | |
45 | 45 | @on-pick-click="handlePickClick" |
46 | 46 | ></component> |
47 | 47 | </div> |
... | ... | @@ -69,7 +69,7 @@ |
69 | 69 | v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span> |
70 | 70 | </div> |
71 | 71 | <component |
72 | - :is="pickerTable" | |
72 | + :is="rightPickerTable" | |
73 | 73 | ref="rightYearTable" |
74 | 74 | v-if="currentView !== 'time'" |
75 | 75 | :table-date="rightPanelDate" |
... | ... | @@ -79,7 +79,7 @@ |
79 | 79 | :show-week-numbers="showWeekNumbers" |
80 | 80 | :value="dates" |
81 | 81 | @on-change-range="handleChangeRange" |
82 | - @on-pick="handleRangePick" | |
82 | + @on-pick="panelPickerHandlers.right" | |
83 | 83 | @on-pick-click="handlePickClick"></component> |
84 | 84 | </div> |
85 | 85 | <div :class="[prefixCls + '-content']" v-show="isTime"> |
... | ... | @@ -147,6 +147,8 @@ |
147 | 147 | dates: this.value, |
148 | 148 | rangeState: {from: this.value[0], to: this.value[1], selecting: minDate && !maxDate}, |
149 | 149 | currentView: this.selectionMode || 'range', |
150 | + leftPickerTable: `${this.selectionMode}-table`, | |
151 | + rightPickerTable: `${this.selectionMode}-table`, | |
150 | 152 | leftPanelDate: leftPanelDate, |
151 | 153 | rightPanelDate: new Date(leftPanelDate.getFullYear(), leftPanelDate.getMonth() + 1, leftPanelDate.getDate()) |
152 | 154 | }; |
... | ... | @@ -161,9 +163,6 @@ |
161 | 163 | } |
162 | 164 | ]; |
163 | 165 | }, |
164 | - pickerTable(){ | |
165 | - return `${this.currentView}-table`; | |
166 | - }, | |
167 | 166 | leftDatePanelLabel(){ |
168 | 167 | return this.panelLabelConfig('left'); |
169 | 168 | }, |
... | ... | @@ -172,6 +171,13 @@ |
172 | 171 | }, |
173 | 172 | timeDisabled(){ |
174 | 173 | return !(this.dates[0] && this.dates[1]); |
174 | + }, | |
175 | + panelPickerHandlers(){ | |
176 | + const tableType = `${this.currentView}-table`; | |
177 | + return { | |
178 | + left: this.leftPickerTable === tableType ? this.handleRangePick : this.handlePreSelection.bind(this, 'left'), | |
179 | + right: this.leftPickerTable === tableType ? this.handleRangePick : this.handlePreSelection.bind(this, 'right'), | |
180 | + } | |
175 | 181 | } |
176 | 182 | }, |
177 | 183 | watch: { |
... | ... | @@ -190,6 +196,9 @@ |
190 | 196 | const rightMonth = this.rightPanelDate.getMonth(); |
191 | 197 | const isSameYear = this.leftPanelDate.getFullYear() === this.rightPanelDate.getFullYear(); |
192 | 198 | |
199 | + this.leftPickerTable = `${currentView}-table`; | |
200 | + this.rightPickerTable = `${currentView}-table`; | |
201 | + | |
193 | 202 | if (currentView === 'date' && isSameYear && leftMonth === rightMonth){ |
194 | 203 | this.changePanelDate('right', 'Month', 1); |
195 | 204 | } |
... | ... | @@ -257,11 +266,15 @@ |
257 | 266 | } |
258 | 267 | } |
259 | 268 | }, |
260 | - showYearPicker () { | |
261 | - this.currentView = 'year'; | |
269 | + showYearPicker (panel) { | |
270 | + this[`${panel}PickerTable`] = 'year-table'; | |
271 | + }, | |
272 | + showMonthPicker (panel) { | |
273 | + this[`${panel}PickerTable`] = 'month-table'; | |
262 | 274 | }, |
263 | - showMonthPicker () { | |
264 | - this.currentView = 'month'; | |
275 | + handlePreSelection(panel, value){ | |
276 | + this[`${panel}PanelDate`] = value; | |
277 | + this[`${panel}PickerTable`] = `${this.currentView}-table`; | |
265 | 278 | }, |
266 | 279 | handleRangePick (val) { |
267 | 280 | if (this.rangeState.selecting || this.currentView === 'time'){ | ... | ... |
src/components/date-picker/panel/Date/date.vue
... | ... | @@ -37,7 +37,7 @@ |
37 | 37 | :value="dates" |
38 | 38 | :selection-mode="selectionMode" |
39 | 39 | :disabled-date="disabledDate" |
40 | - @on-pick="handlePick" | |
40 | + @on-pick="panelPickerHandlers" | |
41 | 41 | @on-pick-click="handlePickClick" |
42 | 42 | ></component> |
43 | 43 | </div> |
... | ... | @@ -92,11 +92,14 @@ |
92 | 92 | // in the mixin |
93 | 93 | }, |
94 | 94 | data () { |
95 | - const dates = this.value.slice().sort(); | |
95 | + const {selectionMode, value} = this; | |
96 | + | |
97 | + const dates = value.slice().sort(); | |
96 | 98 | return { |
97 | 99 | prefixCls: prefixCls, |
98 | 100 | datePrefixCls: datePrefixCls, |
99 | - currentView: this.selectionMode || 'date', | |
101 | + currentView: selectionMode || 'date', | |
102 | + pickerTable: this.getTableType(selectionMode), | |
100 | 103 | dates: dates, |
101 | 104 | panelDate: this.startDate || dates[0] || new Date() |
102 | 105 | }; |
... | ... | @@ -110,8 +113,8 @@ |
110 | 113 | } |
111 | 114 | ]; |
112 | 115 | }, |
113 | - pickerTable(){ | |
114 | - return this.currentView.match(/^time/) ? 'time-picker' : `${this.currentView}-table`; | |
116 | + panelPickerHandlers(){ | |
117 | + return this.pickerTable === `${this.currentView}-table` ? this.handlePick : this.handlePreSelection | |
115 | 118 | }, |
116 | 119 | datePanelLabel () { |
117 | 120 | const locale = this.t('i.locale'); |
... | ... | @@ -120,7 +123,7 @@ |
120 | 123 | const { labels, separator } = formatDateLabels(locale, datePanelLabel, date); |
121 | 124 | |
122 | 125 | const handler = type => { |
123 | - return () => (this.currentView = type); | |
126 | + return () => this.pickerTable = this.getTableType(type); | |
124 | 127 | }; |
125 | 128 | |
126 | 129 | return { |
... | ... | @@ -139,8 +142,9 @@ |
139 | 142 | selectionMode(){ |
140 | 143 | this.currentView = this.selectionMode; |
141 | 144 | }, |
142 | - currentView (val) { | |
143 | - this.$emit('on-selection-mode-change', val); | |
145 | + currentView (currentView) { | |
146 | + this.$emit('on-selection-mode-change', currentView); | |
147 | + this.pickertable = this.getTableType(currentView); | |
144 | 148 | } |
145 | 149 | }, |
146 | 150 | methods: { |
... | ... | @@ -151,16 +155,29 @@ |
151 | 155 | this.panelDate = siblingMonth(this.panelDate, dir * 12); |
152 | 156 | } |
153 | 157 | }, |
158 | + getTableType(currentView){ | |
159 | + return currentView.match(/^time/) ? 'time-picker' : `${currentView}-table`; | |
160 | + }, | |
154 | 161 | changeMonth(dir){ |
155 | 162 | this.panelDate = siblingMonth(this.panelDate, dir); |
156 | 163 | }, |
164 | + handlePreSelection(value){ | |
165 | + this.panelDate = value; | |
166 | + this.pickerTable = this.getTableType(this.currentView); | |
167 | + }, | |
157 | 168 | handlePick (value) { |
158 | - const {selectionMode, panelDate} = this; | |
169 | + const {selectionMode, panelDate, pickerType} = this; | |
159 | 170 | if (selectionMode === 'year') value = new Date(value.getFullYear(), 0, 1); |
160 | 171 | else if (selectionMode === 'month') value = new Date(panelDate.getFullYear(), value.getMonth(), 1); |
161 | 172 | else value = new Date(value); |
162 | 173 | |
163 | - this.$emit('on-pick', value); | |
174 | + if (pickerType === selectionMode){ | |
175 | + this.$emit('on-pick', value); | |
176 | + } else { | |
177 | + this.selectionMode | |
178 | + } | |
179 | + | |
180 | + | |
164 | 181 | }, |
165 | 182 | }, |
166 | 183 | }; | ... | ... |