diff --git a/src/components/date-picker/base/date-table.vue b/src/components/date-picker/base/date-table.vue index 028eafe..8a4c44c 100644 --- a/src/components/date-picker/base/date-table.vue +++ b/src/components/date-picker/base/date-table.vue @@ -73,8 +73,11 @@ }); } }, - cells (cells) { - this.readCells = cells; + cells: { + handler (cells) { + this.readCells = cells; + }, + immediate: true } }, computed: { diff --git a/src/components/date-picker/picker.vue b/src/components/date-picker/picker.vue index c05b421..bfb0089 100644 --- a/src/components/date-picker/picker.vue +++ b/src/components/date-picker/picker.vue @@ -248,21 +248,47 @@ const value = event.target.value; let correctValue = ''; - const format = this.format || DEFAULT_FORMATS[this.type]; - const parsedDate = parseDate(value, format); + let correctDate = ''; + const type = this.type; + const format = this.format || DEFAULT_FORMATS[type]; - if (parsedDate instanceof Date) { - const options = this.options; - if (options.disabledDate && typeof options.disabledDate === 'function' && options.disabledDate(new Date(parsedDate))) { - correctValue = oldValue; + if (type === 'daterange' || type === 'timerange' || type === 'datetimerange') { + const parser = ( + TYPE_VALUE_RESOLVER_MAP[type] || + TYPE_VALUE_RESOLVER_MAP['default'] + ).parser; + + const formatter = ( + TYPE_VALUE_RESOLVER_MAP[type] || + TYPE_VALUE_RESOLVER_MAP['default'] + ).formatter; + + const parsedValue = parser(value, format); + + if (parsedValue) { + // todo 判断disabledDate + correctValue = formatter(parsedValue, format); } else { - correctValue = formatDate(parsedDate, format); + correctValue = oldValue; } + + correctDate = parsedValue; } else { - correctValue = oldValue; - } + const parsedDate = parseDate(value, format); - const correctDate = parseDate(correctValue, format); + if (parsedDate instanceof Date) { + const options = this.options; + if (options.disabledDate && typeof options.disabledDate === 'function' && options.disabledDate(new Date(parsedDate))) { + correctValue = oldValue; + } else { + correctValue = formatDate(parsedDate, format); + } + } else { + correctValue = oldValue; + } + + correctDate = parseDate(correctValue, format); + } this.visualValue = correctValue; event.target.value = correctValue; diff --git a/test/routers/date.vue b/test/routers/date.vue index f7c02f0..4814e6f 100644 --- a/test/routers/date.vue +++ b/test/routers/date.vue @@ -17,10 +17,12 @@ @@ -34,37 +36,52 @@ value: '2016-12-25', value2: ['2016-12-17', '2017-01-05'], options2: { +// disabledDate(time) { +//// console.log(time) +//// return time.getFullYear() < 2016; +//// return time.getTime() < Date.now() - 8.64e7; +//// return time && time.valueOf() < Date.now(); +// if (time.getDate() === 22 || time.getDate() === 23) { +// return true; +// } else { +// return false; +// } +// }, shortcuts: [ { - text: '今天', + text: '最近一周', value () { -// return new Date(); - return '1/2/19' + const end = new Date(); + const start = new Date(); + start.setTime(start.getTime() - 3600 * 1000 * 24 * 7); + return [start, end]; }, onClick (picker) { - console.log('点击了今天'); + console.log('点击了最近一周'); } }, { - text: '昨天', + text: '最近一个月', value () { - const date = new Date(); - date.setTime(date.getTime() - 3600 * 1000 * 24); - return date; + const end = new Date(); + const start = new Date(); + start.setTime(start.getTime() - 3600 * 1000 * 24 * 30); + return [start, end]; }, onClick () { - console.log('点击了昨天'); + console.log('点击了最近一个月'); } }, { text: '最近三个月', value () { - const date = new Date(); - date.setTime(date.getTime() - 3600 * 1000 * 24 * 7); - return date; + const end = new Date(); + const start = new Date(); + start.setTime(start.getTime() - 3600 * 1000 * 24 * 90); + return [start, end]; }, onClick () { - console.log('点击了一周前'); + console.log('点击了最近三个月'); } } ] -- libgit2 0.21.4