Merge pull request #2171 from QingWei-Li/fix/datepicker/change

DatePicker: fix change event no trigger when setting the initial valu…
pull/2217/head
baiyaaaaa 2017-01-05 15:12:50 +08:00 committed by GitHub
commit 3574e62cb6
4 changed files with 45 additions and 41 deletions

View File

@ -10,8 +10,8 @@
@focus="handleFocus" @focus="handleFocus"
@blur="handleBlur" @blur="handleBlur"
@keydown.native="handleKeydown" @keydown.native="handleKeydown"
:value="visualValue" :value="displayValue"
@change.native="visualValue = $event.target.value" @change.native="displayValue = $event.target.value"
ref="reference"> ref="reference">
<i slot="icon" <i slot="icon"
class="el-input__icon" class="el-input__icon"
@ -27,7 +27,7 @@
<script> <script>
import Vue from 'vue'; import Vue from 'vue';
import Clickoutside from 'element-ui/src/utils/clickoutside'; import Clickoutside from 'element-ui/src/utils/clickoutside';
import { formatDate, parseDate, getWeekNumber, equalDate } from './util'; import { formatDate, parseDate, getWeekNumber, equalDate, isDate } from './util';
import Popper from 'element-ui/src/utils/vue-popper'; import Popper from 'element-ui/src/utils/vue-popper';
import Emitter from 'element-ui/src/mixins/emitter'; import Emitter from 'element-ui/src/mixins/emitter';
import ElInput from 'element-ui/packages/input'; import ElInput from 'element-ui/packages/input';
@ -213,7 +213,7 @@ export default {
return { return {
pickerVisible: false, pickerVisible: false,
showClose: false, showClose: false,
internalValue: '' currentValue: ''
}; };
}, },
@ -222,16 +222,22 @@ export default {
if (this.readonly || this.disabled) return; if (this.readonly || this.disabled) return;
val ? this.showPicker() : this.hidePicker(); val ? this.showPicker() : this.hidePicker();
}, },
internalValue(val) { currentValue(val) {
if (!val && this.picker && typeof this.picker.handleClear === 'function') { if (val) return;
if (this.picker && typeof this.picker.handleClear === 'function') {
this.picker.handleClear(); this.picker.handleClear();
} else {
this.$emit('input');
} }
}, },
value: { value: {
immediate: true, immediate: true,
handler(val) { handler(val) {
this.internalValue = val; this.currentValue = isDate(val) ? new Date(val) : val;
} }
},
displayValue(val) {
this.$emit('change', val);
} }
}, },
@ -246,7 +252,7 @@ export default {
}, },
valueIsEmpty() { valueIsEmpty() {
const val = this.internalValue; const val = this.currentValue;
if (Array.isArray(val)) { if (Array.isArray(val)) {
for (let i = 0, len = val.length; i < len; i++) { for (let i = 0, len = val.length; i < len; i++) {
if (val[i]) { if (val[i]) {
@ -284,9 +290,9 @@ export default {
return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1; return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1;
}, },
visualValue: { displayValue: {
get() { get() {
const value = this.internalValue; const value = this.currentValue;
if (!value) return; if (!value) return;
const formatter = ( const formatter = (
TYPE_VALUE_RESOLVER_MAP[this.type] || TYPE_VALUE_RESOLVER_MAP[this.type] ||
@ -318,9 +324,6 @@ export default {
}, },
created() { created() {
this.cachePicker = {};
this.cacheChange = {};
// vue-popper // vue-popper
this.options = { this.options = {
boundariesPadding: 0, boundariesPadding: 0,
@ -340,26 +343,25 @@ export default {
handleClickIcon() { handleClickIcon() {
if (this.readonly || this.disabled) return; if (this.readonly || this.disabled) return;
if (this.showClose) { if (this.showClose) {
this.internalValue = ''; this.currentValue = '';
this.showClose = false;
} else { } else {
this.pickerVisible = !this.pickerVisible; this.pickerVisible = !this.pickerVisible;
} }
}, },
dateIsUpdated(date, cache) { dateChanged(dateA, dateB) {
let updated = true; if (Array.isArray(dateA)) {
let len = dateA.length;
if (Array.isArray(date)) { if (!dateB) return true;
if (equalDate(cache.cacheDateMin, date[0]) && while (len--) {
equalDate(cache.cacheDateMax, date[1])) updated = false; if (!equalDate(dateA[len], dateB[len])) return true;
cache.cacheDateMin = new Date(date[0]); }
cache.cacheDateMax = new Date(date[1]);
} else { } else {
if (equalDate(cache.cacheDate, date)) updated = false; if (!equalDate(dateA, dateB)) return true;
cache.cacheDate = new Date(date);
} }
return updated; return false;
}, },
handleClose() { handleClose() {
@ -400,7 +402,7 @@ export default {
showPicker() { showPicker() {
if (this.$isServer) return; if (this.$isServer) return;
if (!this.picker) { if (!this.picker) {
this.panel.defaultValue = this.internalValue; this.panel.defaultValue = this.currentValue;
this.picker = new Vue(this.panel).$mount(document.createElement('div')); this.picker = new Vue(this.panel).$mount(document.createElement('div'));
this.picker.popperClass = this.popperClass; this.picker.popperClass = this.popperClass;
this.popperElm = this.picker.$el; this.popperElm = this.picker.$el;
@ -423,12 +425,6 @@ export default {
this.picker.selectableRange = ranges.map(range => parser(range, format)); this.picker.selectableRange = ranges.map(range => parser(range, format));
} }
if (this.type === 'time-select' && options) {
this.$watch('pickerOptions.minTime', val => {
this.picker.minTime = val;
});
}
for (const option in options) { for (const option in options) {
if (options.hasOwnProperty(option) && if (options.hasOwnProperty(option) &&
// time-picker // time-picker
@ -446,9 +442,7 @@ export default {
this.picker.$on('dodestroy', this.doDestroy); this.picker.$on('dodestroy', this.doDestroy);
this.picker.$on('pick', (date, visible = false) => { this.picker.$on('pick', (date, visible = false) => {
if (this.dateIsUpdated(date, this.cachePicker)) this.$emit('input', date); if (this.dateChanged(date, this.value)) this.$emit('input', date);
this.$nextTick(() => this.dateIsUpdated(date, this.cacheChange) && this.$emit('change', this.visualValue));
this.pickerVisible = this.picker.visible = visible; this.pickerVisible = this.picker.visible = visible;
this.picker.resetView && this.picker.resetView(); this.picker.resetView && this.picker.resetView();
}); });
@ -463,10 +457,10 @@ export default {
this.updatePopper(); this.updatePopper();
if (this.internalValue instanceof Date) { if (this.currentValue instanceof Date) {
this.picker.date = new Date(this.internalValue.getTime()); this.picker.date = new Date(this.currentValue.getTime());
} else { } else {
this.picker.value = this.internalValue; this.picker.value = this.currentValue;
} }
this.picker.resetView && this.picker.resetView(); this.picker.resetView && this.picker.resetView();

View File

@ -11,6 +11,12 @@ export default {
isRange: Boolean isRange: Boolean
}, },
data() {
return {
type: ''
};
},
created() { created() {
this.type = this.isRange ? 'timerange' : 'time'; this.type = this.isRange ? 'timerange' : 'time';
this.panel = this.isRange ? TimeRangePanel : TimePanel; this.panel = this.isRange ? TimeRangePanel : TimePanel;

View File

@ -6,7 +6,7 @@ export default {
name: 'ElTimeSelect', name: 'ElTimeSelect',
created() { beforeCreate() {
this.type = 'time-select'; this.type = 'time-select';
this.panel = Panel; this.panel = Panel;
} }

View File

@ -13,9 +13,13 @@ export const equalDate = function(dateA, dateB) {
}; };
export const toDate = function(date) { export const toDate = function(date) {
return isDate(date) ? date : null;
};
export const isDate = function(date) {
date = new Date(date); date = new Date(date);
if (isNaN(date.getTime())) return null; if (isNaN(date.getTime())) return false;
return date; return true;
}; };
export const formatDate = function(date, format) { export const formatDate = function(date, format) {