mirror of https://github.com/ElemeFE/element
date-picker: fix typo, fix value-format parsing
parent
f0b7debd23
commit
7ceecce564
|
@ -69,7 +69,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, isDate, getWeekNumber } from './util';
|
import { formatDate, parseDate, isDate, isDateObject, getWeekNumber } 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 Focus from 'element-ui/src/mixins/focus';
|
import Focus from 'element-ui/src/mixins/focus';
|
||||||
|
@ -229,23 +229,23 @@ const PLACEMENT_MAP = {
|
||||||
right: 'bottom-end'
|
right: 'bottom-end'
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseAsFormatAndType = (value, cutsomFormat, type, rangeSeparator = '-') => {
|
const parseAsFormatAndType = (value, customFormat, type, rangeSeparator = '-') => {
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
const parser = (
|
const parser = (
|
||||||
TYPE_VALUE_RESOLVER_MAP[type] ||
|
TYPE_VALUE_RESOLVER_MAP[type] ||
|
||||||
TYPE_VALUE_RESOLVER_MAP['default']
|
TYPE_VALUE_RESOLVER_MAP['default']
|
||||||
).parser;
|
).parser;
|
||||||
const format = cutsomFormat || DEFAULT_FORMATS[type];
|
const format = customFormat || DEFAULT_FORMATS[type];
|
||||||
return parser(value, format, rangeSeparator);
|
return parser(value, format, rangeSeparator);
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatAsFormatAndType = (value, cutsomFormat, type) => {
|
const formatAsFormatAndType = (value, customFormat, type) => {
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
const formatter = (
|
const formatter = (
|
||||||
TYPE_VALUE_RESOLVER_MAP[type] ||
|
TYPE_VALUE_RESOLVER_MAP[type] ||
|
||||||
TYPE_VALUE_RESOLVER_MAP['default']
|
TYPE_VALUE_RESOLVER_MAP['default']
|
||||||
).formatter;
|
).formatter;
|
||||||
const format = cutsomFormat || DEFAULT_FORMATS[type];
|
const format = customFormat || DEFAULT_FORMATS[type];
|
||||||
return formatter(value, format);
|
return formatter(value, format);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -421,7 +421,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
parsedValue() {
|
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) {
|
if (this.valueFormat && !isParsed) {
|
||||||
return parseAsFormatAndType(this.value, this.valueFormat, this.type, this.rangeSeparator) || this.value;
|
return parseAsFormatAndType(this.value, this.valueFormat, this.type, this.rangeSeparator) || this.value;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -31,6 +31,10 @@ export const isDate = function(date) {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isDateObject = function(val) {
|
||||||
|
return val instanceof Date;
|
||||||
|
};
|
||||||
|
|
||||||
export const formatDate = function(date, format) {
|
export const formatDate = function(date, format) {
|
||||||
date = toDate(date);
|
date = toDate(date);
|
||||||
if (!date) return '';
|
if (!date) return '';
|
||||||
|
|
|
@ -411,20 +411,28 @@ describe('DatePicker', () => {
|
||||||
ref="compo"
|
ref="compo"
|
||||||
v-model="value"
|
v-model="value"
|
||||||
type="date"
|
type="date"
|
||||||
value-format="dd-MM-yyyy" />`,
|
format="yyyy-MM-dd"
|
||||||
|
value-format="dd/MM yyyy" />`,
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
value: '01-02-2000'
|
value: '01/02 2000'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, true);
|
}, 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(_ => {
|
setTimeout(_ => {
|
||||||
const date = vm.$refs.compo.picker.date;
|
const date = vm.$refs.compo.picker.date;
|
||||||
expect(date.getFullYear()).to.equal(2000);
|
expect(date.getFullYear()).to.equal(2000);
|
||||||
expect(date.getMonth()).to.equal(1);
|
expect(date.getMonth()).to.equal(1);
|
||||||
expect(date.getDate()).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);
|
}, DELAY);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue