Commit c61015d19dc156998ca52bd74c6b53cd6addb92d
Committed by
GitHub
Merge pull request #2172 from SergioCrisostomo/fix-2164
format string dates in daterange also
Showing
3 changed files
with
50 additions
and
2 deletions
Show diff stats
src/components/date-picker/picker.vue
@@ -513,7 +513,7 @@ | @@ -513,7 +513,7 @@ | ||
513 | 513 | ||
514 | if (val && type === 'time' && !(val instanceof Date)) { | 514 | if (val && type === 'time' && !(val instanceof Date)) { |
515 | val = parser(val, this.format || DEFAULT_FORMATS[type]); | 515 | val = parser(val, this.format || DEFAULT_FORMATS[type]); |
516 | - } else if (val && type === 'timerange' && Array.isArray(val) && val.length === 2 && !(val[0] instanceof Date) && !(val[1] instanceof Date)) { | 516 | + } else if (val && type.match(/range$/) && Array.isArray(val) && val.filter(Boolean).length === 2 && !(val[0] instanceof Date) && !(val[1] instanceof Date)) { |
517 | val = val.join(RANGE_SEPARATOR); | 517 | val = val.join(RANGE_SEPARATOR); |
518 | val = parser(val, this.format || DEFAULT_FORMATS[type]); | 518 | val = parser(val, this.format || DEFAULT_FORMATS[type]); |
519 | } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){ | 519 | } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){ |
test/unit/specs/date-picker.spec.js
1 | -import { createVue, destroyVM, stringToDate, promissedTick } from '../util'; | 1 | +import { createVue, destroyVM, stringToDate, dateToString, promissedTick } from '../util'; |
2 | 2 | ||
3 | describe('DatePicker.vue', () => { | 3 | describe('DatePicker.vue', () => { |
4 | let vm; | 4 | let vm; |
@@ -191,6 +191,46 @@ describe('DatePicker.vue', () => { | @@ -191,6 +191,46 @@ describe('DatePicker.vue', () => { | ||
191 | }); | 191 | }); |
192 | }); | 192 | }); |
193 | 193 | ||
194 | + it('should convert strings to Date objects', done => { | ||
195 | + vm = createVue({ | ||
196 | + template: ` | ||
197 | + <div> | ||
198 | + <date-picker v-model="value1" type="daterange" style="width: 200px"></date-picker> | ||
199 | + <date-picker v-model="value2" type="daterange" placement="bottom-end" style="width: 200px"></date-picker> | ||
200 | + <date-picker v-model="value3" type="datetime" placement="bottom-end" style="width: 200px"></date-picker> | ||
201 | + <date-picker v-model="value4" type="datetimerange" placement="bottom-end" style="width: 200px"></date-picker> | ||
202 | + </div> | ||
203 | + `, | ||
204 | + data() { | ||
205 | + return { | ||
206 | + value1: ['2017-10-10', '2017-10-20'], | ||
207 | + value2: [new Date(), new Date()], | ||
208 | + value3: '2017-10-10 10:00:00', | ||
209 | + value4: ['2027-10-10 10:00:00', '2027-10-20 10:00:00'] | ||
210 | + }; | ||
211 | + } | ||
212 | + }); | ||
213 | + | ||
214 | + vm.$nextTick(() => { | ||
215 | + const {value1, value2, value3, value4} = vm; | ||
216 | + | ||
217 | + expect(value1[0] instanceof Date).to.equal(true); | ||
218 | + expect(value1[1] instanceof Date).to.equal(true); | ||
219 | + expect(value1.map(dateToString).join('|')).to.equal('2017-10-10|2017-10-20'); | ||
220 | + | ||
221 | + expect(value2[0] instanceof Date).to.equal(true); | ||
222 | + expect(value2[1] instanceof Date).to.equal(true); | ||
223 | + expect(value2.map(dateToString).join('|')).to.equal([new Date(), new Date()].map(dateToString).join('|')); | ||
224 | + | ||
225 | + expect(dateToString(vm.value3)).to.equal('2017-10-10'); | ||
226 | + | ||
227 | + expect(value4[0] instanceof Date).to.equal(true); | ||
228 | + expect(value4[1] instanceof Date).to.equal(true); | ||
229 | + expect(value4.map(dateToString).join('|')).to.equal('2027-10-10|2027-10-20'); | ||
230 | + done(); | ||
231 | + }); | ||
232 | + }); | ||
233 | + | ||
194 | it('should render date-picker label correctly in zh-CN', done => { | 234 | it('should render date-picker label correctly in zh-CN', done => { |
195 | vm = createVue(` | 235 | vm = createVue(` |
196 | <Date-picker type="date"></Date-picker> | 236 | <Date-picker type="date"></Date-picker> |
test/unit/util.js
@@ -68,6 +68,14 @@ exports.stringToDate = function(str) { | @@ -68,6 +68,14 @@ exports.stringToDate = function(str) { | ||
68 | }; | 68 | }; |
69 | 69 | ||
70 | /** | 70 | /** |
71 | + * Transform Date to yyyy-mm-dd string | ||
72 | + * @param {Date} | ||
73 | + */ | ||
74 | +exports.dateToString = function(d) { | ||
75 | + return [d.getFullYear(), d.getMonth() + 1, d.getDate()].map(nr => nr > 9 ? nr : '0' + nr).join('-'); | ||
76 | +}; | ||
77 | + | ||
78 | +/** | ||
71 | * 触发一个事件 | 79 | * 触发一个事件 |
72 | * mouseenter, mouseleave, mouseover, keyup, change, click 等 | 80 | * mouseenter, mouseleave, mouseover, keyup, change, click 等 |
73 | * @param {Element} elm | 81 | * @param {Element} elm |