Fix parser and formater

This commit is contained in:
Sergio Crisostomo 2018-01-19 10:30:40 +01:00
parent 4ec8bc8af8
commit 8f6aeda4be

View file

@ -267,13 +267,11 @@
} }
}, },
data(){ data(){
const initialValue = this.formatDate(this.value);
return { return {
prefixCls: prefixCls, prefixCls: prefixCls,
showClose: false, showClose: false,
visible: false, visible: false,
internalValue: initialValue, internalValue: this.parseDate(this.value),
disableClickOutSide: false, // fixed when click a date,trigger clickoutside to close picker disableClickOutSide: false, // fixed when click a date,trigger clickoutside to close picker
disableCloseUnderTransfer: false, // transfer Drop, disableCloseUnderTransfer: false, // transfer Drop,
selectionMode: this.onSelectionModeChange(this.type) selectionMode: this.onSelectionModeChange(this.type)
@ -282,10 +280,11 @@
computed: { computed: {
publicValue(){ publicValue(){
if (this.multiple){ if (this.multiple){
return this.internalValue.map(date => this.formatDate(date)); return this.internalValue.slice();
} else { } else {
const isRange = this.type.includes('range'); const isRange = this.type.includes('range');
return isRange ? this.formatDate(this.internalValue) : this.formatDate(this.internalValue[0]); const val = this.internalValue.map(date => date instanceof Date ? new Date(date) : date);
return (isRange || this.multiple) ? val : val[0];
} }
}, },
@ -307,13 +306,7 @@
if (!value) return; if (!value) return;
if (this.multiple) return value.map(date => this.formatDate(date)).join(', '); if (this.multiple) return value.map(date => this.formatDate(date)).join(', ');
return this.formatDate(value);
const formatter = (
TYPE_VALUE_RESOLVER_MAP[this.type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).formatter;
const format = DEFAULT_FORMATS[this.type];
return formatter(value, this.format || format);
}, },
isConfirm(){ isConfirm(){
return this.confirm || this.type === 'datetime' || this.type === 'datetimerange' || this.multiple; return this.confirm || this.type === 'datetime' || this.type === 'datetimerange' || this.multiple;
@ -353,7 +346,7 @@
if (newValue !== oldValue) { if (newValue !== oldValue) {
this.emitChange(); this.emitChange();
this.internalValue = this.formatDate(newValue); this.internalValue = this.parseDate(newValue);
} }
}, },
handleInputMouseenter () { handleInputMouseenter () {
@ -390,9 +383,8 @@
this.dispatch('FormItem', 'on-form-change', this.publicValue); this.dispatch('FormItem', 'on-form-change', this.publicValue);
}); });
}, },
formatDate (val) { parseDate(val) {
const isRange = this.type.includes('range'); const isRange = this.type.includes('range');
const type = this.type; const type = this.type;
const parser = ( const parser = (
TYPE_VALUE_RESOLVER_MAP[type] || TYPE_VALUE_RESOLVER_MAP[type] ||
@ -408,11 +400,20 @@
val = val.map(date => new Date(date)); // try to parse val = val.map(date => new Date(date)); // try to parse
val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed
} }
} else if (typeof val === 'string' && type.indexOf('time') !== 0 ){ } else if (typeof val === 'string' && type.indexOf('time') !== 0){
val = parser(val, this.format || DEFAULT_FORMATS[type]) || val; val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
} }
return isRange ? val : [val]; return isRange ? val : [val];
}, },
formatDate(value){
const {formatter} = (
TYPE_VALUE_RESOLVER_MAP[this.type] ||
TYPE_VALUE_RESOLVER_MAP['default']
);
const format = DEFAULT_FORMATS[this.type];
return formatter(value, this.format || format);
},
onPick(dates, visible = false) { onPick(dates, visible = false) {
if (this.multiple){ if (this.multiple){
@ -442,30 +443,20 @@
this.$emit('on-open-change', state); this.$emit('on-open-change', state);
}, },
value(val) { value(val) {
const type = this.type; this.internalValue = this.parseDate(val);
const parser = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).parser;
if (val && type === 'time' && !(val instanceof Date)) {
val = parser(val, this.format || DEFAULT_FORMATS[type]);
} else if (val && type.match(/range$/) && Array.isArray(val) && val.filter(Boolean).length === 2 && !(val[0] instanceof Date) && !(val[1] instanceof Date)) {
val = val.join(RANGE_SEPARATOR);
val = parser(val, this.format || DEFAULT_FORMATS[type]);
} else if (typeof val === 'string' && type.indexOf('time') !== 0 ){
val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
}
this.internalValue = val;
this.$emit('input', val);
}, },
open (val) { open (val) {
this.visible = val === true; this.visible = val === true;
}, },
type(type){ type(type){
this.onSelectionModeChange(type); this.onSelectionModeChange(type);
} },
publicValue(now, before){
const newValue = JSON.stringify(now);
const oldValue = JSON.stringify(before);
if (newValue !== oldValue) this.$emit('input', now); // to update v-model
},
}, },
mounted () { mounted () {
if (this.open !== null) this.visible = this.open; if (this.open !== null) this.visible = this.open;