date-picker: fix typo, fix value-format parsing

This commit is contained in:
wacky6.AriesMBP 2017-10-17 03:18:22 +11:00 committed by 杨奕
parent f0b7debd23
commit 7ceecce564
3 changed files with 22 additions and 10 deletions

View File

@ -69,7 +69,7 @@
<script>
import Vue from 'vue';
import Clickoutside from 'element-ui/src/utils/clickoutside';
import { formatDate, parseDate, isDate, getWeekNumber } from './util';
import { formatDate, parseDate, isDate, isDateObject, getWeekNumber } from './util';
import Popper from 'element-ui/src/utils/vue-popper';
import Emitter from 'element-ui/src/mixins/emitter';
import Focus from 'element-ui/src/mixins/focus';
@ -229,23 +229,23 @@ const PLACEMENT_MAP = {
right: 'bottom-end'
};
const parseAsFormatAndType = (value, cutsomFormat, type, rangeSeparator = '-') => {
const parseAsFormatAndType = (value, customFormat, type, rangeSeparator = '-') => {
if (!value) return null;
const parser = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).parser;
const format = cutsomFormat || DEFAULT_FORMATS[type];
const format = customFormat || DEFAULT_FORMATS[type];
return parser(value, format, rangeSeparator);
};
const formatAsFormatAndType = (value, cutsomFormat, type) => {
const formatAsFormatAndType = (value, customFormat, type) => {
if (!value) return null;
const formatter = (
TYPE_VALUE_RESOLVER_MAP[type] ||
TYPE_VALUE_RESOLVER_MAP['default']
).formatter;
const format = cutsomFormat || DEFAULT_FORMATS[type];
const format = customFormat || DEFAULT_FORMATS[type];
return formatter(value, format);
};
@ -421,7 +421,7 @@ export default {
},
parsedValue() {
const isParsed = isDate(this.value) || (Array.isArray(this.value) && this.value.every(isDate));
const isParsed = isDateObject(this.value) || (Array.isArray(this.value) && this.value.every(isDateObject));
if (this.valueFormat && !isParsed) {
return parseAsFormatAndType(this.value, this.valueFormat, this.type, this.rangeSeparator) || this.value;
} else {

View File

@ -31,6 +31,10 @@ export const isDate = function(date) {
return true;
};
export const isDateObject = function(val) {
return val instanceof Date;
};
export const formatDate = function(date, format) {
date = toDate(date);
if (!date) return '';

View File

@ -411,20 +411,28 @@ describe('DatePicker', () => {
ref="compo"
v-model="value"
type="date"
value-format="dd-MM-yyyy" />`,
format="yyyy-MM-dd"
value-format="dd/MM yyyy" />`,
data() {
return {
value: '01-02-2000'
value: '01/02 2000'
};
}
}, true);
vm.$refs.compo.$el.querySelector('input').focus();
const input = vm.$refs.compo.$el.querySelector('input');
expect(input.value).to.equal('2000-02-01');
expect(vm.$refs.compo.parsedValue).to.be.an.instanceof(Date);
input.focus();
setTimeout(_ => {
const date = vm.$refs.compo.picker.date;
expect(date.getFullYear()).to.equal(2000);
expect(date.getMonth()).to.equal(1);
expect(date.getDate()).to.equal(1);
done();
vm.$refs.compo.picker.$el.querySelector('.el-date-table .current').click();
setTimeout(_ => {
expect(input.value).to.equal('2000-02-01');
done();
}, DELAY);
}, DELAY);
});