Pass Strings to @on-change and Dates to v-model

This commit is contained in:
Sergio Crisostomo 2018-02-27 11:01:06 +01:00
parent c10c5aae55
commit b42322fe06
2 changed files with 17 additions and 11 deletions

View file

@ -178,7 +178,7 @@
}; };
}, },
computed: { computed: {
publicValue(){ publicVModelValue(){
if (this.multiple){ if (this.multiple){
return this.internalValue.slice(); return this.internalValue.slice();
} else { } else {
@ -189,6 +189,11 @@
return (isRange || this.multiple) ? val : val[0]; return (isRange || this.multiple) ? val : val[0];
} }
}, },
publicStringValue(){
const {formatDate, publicVModelValue, type} = this;
if (type.match(/^time/)) return publicVModelValue;
return Array.isArray(publicVModelValue) ? publicVModelValue.map(formatDate) : formatDate(publicVModelValue);
},
opened () { opened () {
return this.open === null ? this.visible : this.open; return this.open === null ? this.visible : this.open;
}, },
@ -296,8 +301,8 @@
}, },
emitChange () { emitChange () {
this.$nextTick(() => { this.$nextTick(() => {
this.$emit('on-change', this.publicValue); this.$emit('on-change', this.publicStringValue);
this.dispatch('FormItem', 'on-form-change', this.publicValue); this.dispatch('FormItem', 'on-form-change', this.publicStringValue);
}); });
}, },
parseDate(val) { parseDate(val) {
@ -388,7 +393,7 @@
type(type){ type(type){
this.onSelectionModeChange(type); this.onSelectionModeChange(type);
}, },
publicValue(now, before){ publicVModelValue(now, before){
const newValue = JSON.stringify(now); const newValue = JSON.stringify(now);
const oldValue = JSON.stringify(before); const oldValue = JSON.stringify(before);
const shouldEmitInput = newValue !== oldValue || typeof now !== typeof before; const shouldEmitInput = newValue !== oldValue || typeof now !== typeof before;
@ -397,9 +402,9 @@
}, },
mounted () { mounted () {
const initialValue = this.value; const initialValue = this.value;
const parsedValue = this.publicValue; const parsedValue = this.publicVModelValue;
if (typeof initialValue !== typeof parsedValue || JSON.stringify(initialValue) !== JSON.stringify(parsedValue)){ if (typeof initialValue !== typeof parsedValue || JSON.stringify(initialValue) !== JSON.stringify(parsedValue)){
this.$emit('input', this.publicValue); // to update v-model this.$emit('input', this.publicVModelValue); // to update v-model
} }
if (this.open !== null) this.visible = this.open; if (this.open !== null) this.visible = this.open;
} }

View file

@ -80,12 +80,13 @@ describe('DatePicker.vue', () => {
vm.$nextTick(() => { vm.$nextTick(() => {
// DATE // DATE
expect(dateValue instanceof Date).to.equal(true); expect(typeof dateValue).to.equal('string');
expect(dateToString(dateValue)).to.equal(nowDate); expect(dateValue).to.equal(nowDate);
// DATERANGE // DATERANGE
expect(Array.isArray(dateRangeValue)).to.equal(true); expect(Array.isArray(dateRangeValue)).to.equal(true);
expect(dateToString(dateRangeValue[0])).to.equal(nowDate); expect(dateRangeValue[0]).to.equal(nowDate);
expect(dateToString(dateRangeValue[1])).to.equal(dateToString(nextWeek)); expect(dateRangeValue[1]).to.equal(dateToString(nextWeek));
// TIME // TIME
expect(typeof timeValue).to.equal('string'); expect(typeof timeValue).to.equal('string');
expect(timeValue).to.equal(nowTime); expect(timeValue).to.equal(nowTime);
@ -339,7 +340,7 @@ describe('DatePicker.vue', () => {
expect(value2[1] instanceof Date).to.equal(true); expect(value2[1] instanceof Date).to.equal(true);
expect(value2.map(dateToString).join('|')).to.equal([new Date(), new Date()].map(dateToString).join('|')); expect(value2.map(dateToString).join('|')).to.equal([new Date(), new Date()].map(dateToString).join('|'));
expect(dateToString(vm.value3)).to.equal('2017-10-10'); expect(dateToString(value3)).to.equal('2017-10-10');
expect(value4[0] instanceof Date).to.equal(true); expect(value4[0] instanceof Date).to.equal(true);
expect(value4[1] instanceof Date).to.equal(true); expect(value4[1] instanceof Date).to.equal(true);