Merge pull request #2000 from SergioCrisostomo/datepicker-refactor
refactor and reduce code logic
This commit is contained in:
commit
d9e0bcc987
1 changed files with 22 additions and 48 deletions
|
@ -10,10 +10,10 @@
|
||||||
<div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
|
<div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
|
||||||
<span
|
<span
|
||||||
:class="iconBtnCls('prev', '-double')"
|
:class="iconBtnCls('prev', '-double')"
|
||||||
@click="prevYear"><Icon type="ios-arrow-left"></Icon></span>
|
@click="changeYear(-1)"><Icon type="ios-arrow-left"></Icon></span>
|
||||||
<span
|
<span
|
||||||
:class="iconBtnCls('prev')"
|
:class="iconBtnCls('prev')"
|
||||||
@click="prevMonth"
|
@click="changeMonth(-1)"
|
||||||
v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
|
v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
|
||||||
<span
|
<span
|
||||||
:class="[datePrefixCls + '-header-label']"
|
:class="[datePrefixCls + '-header-label']"
|
||||||
|
@ -24,10 +24,10 @@
|
||||||
v-show="currentView === 'date'">{{ monthLabel }}</span>
|
v-show="currentView === 'date'">{{ monthLabel }}</span>
|
||||||
<span
|
<span
|
||||||
:class="iconBtnCls('next', '-double')"
|
:class="iconBtnCls('next', '-double')"
|
||||||
@click="nextYear"><Icon type="ios-arrow-right"></Icon></span>
|
@click="changeYear(+1)"><Icon type="ios-arrow-right"></Icon></span>
|
||||||
<span
|
<span
|
||||||
:class="iconBtnCls('next')"
|
:class="iconBtnCls('next')"
|
||||||
@click="nextMonth"
|
@click="changeMonth(+1)"
|
||||||
v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
|
v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
|
||||||
</div>
|
</div>
|
||||||
<div :class="[prefixCls + '-content']">
|
<div :class="[prefixCls + '-content']">
|
||||||
|
@ -87,7 +87,7 @@
|
||||||
import Mixin from './mixin';
|
import Mixin from './mixin';
|
||||||
import Locale from '../../../mixins/locale';
|
import Locale from '../../../mixins/locale';
|
||||||
|
|
||||||
import { initTimeDate } from '../util';
|
import { initTimeDate, siblingMonth } from '../util';
|
||||||
|
|
||||||
const prefixCls = 'ivu-picker-panel';
|
const prefixCls = 'ivu-picker-panel';
|
||||||
const datePrefixCls = 'ivu-date-picker';
|
const datePrefixCls = 'ivu-date-picker';
|
||||||
|
@ -144,8 +144,7 @@
|
||||||
newVal = new Date(newVal);
|
newVal = new Date(newVal);
|
||||||
if (!isNaN(newVal)) {
|
if (!isNaN(newVal)) {
|
||||||
this.date = newVal;
|
this.date = newVal;
|
||||||
this.year = newVal.getFullYear();
|
this.setMonthYear(newVal);
|
||||||
this.month = newVal.getMonth();
|
|
||||||
}
|
}
|
||||||
if (this.showTime) this.$refs.timePicker.value = newVal;
|
if (this.showTime) this.$refs.timePicker.value = newVal;
|
||||||
},
|
},
|
||||||
|
@ -163,6 +162,10 @@
|
||||||
resetDate () {
|
resetDate () {
|
||||||
this.date = new Date(this.date);
|
this.date = new Date(this.date);
|
||||||
},
|
},
|
||||||
|
setMonthYear(date){
|
||||||
|
this.month = date.getMonth();
|
||||||
|
this.year = date.getFullYear();
|
||||||
|
},
|
||||||
handleClear () {
|
handleClear () {
|
||||||
this.date = new Date();
|
this.date = new Date();
|
||||||
this.$emit('on-pick', '');
|
this.$emit('on-pick', '');
|
||||||
|
@ -178,42 +181,20 @@
|
||||||
this.currentView = 'date';
|
this.currentView = 'date';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.setMonthYear(this.date);
|
||||||
this.year = this.date.getFullYear();
|
|
||||||
this.month = this.date.getMonth();
|
|
||||||
if (reset) this.isTime = false;
|
if (reset) this.isTime = false;
|
||||||
},
|
},
|
||||||
prevYear () {
|
changeYear(dir){
|
||||||
if (this.currentView === 'year') {
|
if (this.currentView === 'year') {
|
||||||
this.$refs.yearTable.prevTenYear();
|
this.$refs.yearTable[dir == 1 ? 'nextTenYear' : 'prevTenYear']();
|
||||||
} else {
|
} else {
|
||||||
this.year--;
|
this.year+= dir;
|
||||||
this.date.setFullYear(this.year);
|
this.date = siblingMonth(this.date, dir * 12);
|
||||||
this.resetDate();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
nextYear () {
|
changeMonth(dir){
|
||||||
if (this.currentView === 'year') {
|
this.date = siblingMonth(this.date, dir);
|
||||||
this.$refs.yearTable.nextTenYear();
|
this.setMonthYear(this.date);
|
||||||
} else {
|
|
||||||
this.year++;
|
|
||||||
this.date.setFullYear(this.year);
|
|
||||||
this.resetDate();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
prevMonth () {
|
|
||||||
this.month--;
|
|
||||||
if (this.month < 0) {
|
|
||||||
this.month = 11;
|
|
||||||
this.year--;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
nextMonth () {
|
|
||||||
this.month++;
|
|
||||||
if (this.month > 11) {
|
|
||||||
this.month = 0;
|
|
||||||
this.year++;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
showYearPicker () {
|
showYearPicker () {
|
||||||
this.currentView = 'year';
|
this.currentView = 'year';
|
||||||
|
@ -245,13 +226,11 @@
|
||||||
},
|
},
|
||||||
handleMonthPick (month) {
|
handleMonthPick (month) {
|
||||||
this.month = month;
|
this.month = month;
|
||||||
const selectionMode = this.selectionMode;
|
|
||||||
if (selectionMode !== 'month') {
|
|
||||||
this.date.setMonth(month);
|
this.date.setMonth(month);
|
||||||
|
if (this.selectionMode !== 'month') {
|
||||||
this.currentView = 'date';
|
this.currentView = 'date';
|
||||||
this.resetDate();
|
this.resetDate();
|
||||||
} else {
|
} else {
|
||||||
this.date.setMonth(month);
|
|
||||||
this.year && this.date.setFullYear(this.year);
|
this.year && this.date.setFullYear(this.year);
|
||||||
this.resetDate();
|
this.resetDate();
|
||||||
const value = new Date(this.date.getFullYear(), month, 1);
|
const value = new Date(this.date.getFullYear(), month, 1);
|
||||||
|
@ -261,12 +240,8 @@
|
||||||
handleDatePick (value) {
|
handleDatePick (value) {
|
||||||
if (this.selectionMode === 'day') {
|
if (this.selectionMode === 'day') {
|
||||||
this.$emit('on-pick', new Date(value.getTime()));
|
this.$emit('on-pick', new Date(value.getTime()));
|
||||||
this.date.setFullYear(value.getFullYear());
|
this.date = new Date(value);
|
||||||
this.date.setMonth(value.getMonth());
|
|
||||||
this.date.setDate(value.getDate());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resetDate();
|
|
||||||
},
|
},
|
||||||
handleTimePick (date) {
|
handleTimePick (date) {
|
||||||
this.handleDatePick(date);
|
this.handleDatePick(date);
|
||||||
|
@ -278,8 +253,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.date && !this.year) {
|
if (this.date && !this.year) {
|
||||||
this.year = this.date.getFullYear();
|
this.setMonthYear(this.date);
|
||||||
this.month = this.date.getMonth();
|
|
||||||
}
|
}
|
||||||
if (this.showTime) {
|
if (this.showTime) {
|
||||||
// todo 这里可能有问题,并不能进入到这里,但不影响正常使用
|
// todo 这里可能有问题,并不能进入到这里,但不影响正常使用
|
||||||
|
|
Loading…
Add table
Reference in a new issue