Commit 7600961f694e49dbcf95a3e28df3bb390ecbd4e0
Committed by
GitHub
Merge pull request #3029 from SergioCrisostomo/fix-datepicker-output
Pass correct arguments to `on-change` callback
Showing
4 changed files
with
94 additions
and
5 deletions
Show diff stats
src/components/date-picker/picker.vue
... | ... | @@ -183,7 +183,9 @@ |
183 | 183 | return this.internalValue.slice(); |
184 | 184 | } else { |
185 | 185 | const isRange = this.type.includes('range'); |
186 | - const val = this.internalValue.map(date => date instanceof Date ? new Date(date) : (date || '')); | |
186 | + let val = this.internalValue.map(date => date instanceof Date ? new Date(date) : (date || '')); | |
187 | + | |
188 | + if (this.type.match(/^time/)) val = val.map(this.formatDate); | |
187 | 189 | return (isRange || this.multiple) ? val : val[0]; |
188 | 190 | } |
189 | 191 | }, |
... | ... | @@ -293,8 +295,8 @@ |
293 | 295 | ); |
294 | 296 | }, |
295 | 297 | emitChange () { |
296 | - this.$emit('on-change', this.visualValue, this.publicValue); | |
297 | 298 | this.$nextTick(() => { |
299 | + this.$emit('on-change', this.publicValue); | |
298 | 300 | this.dispatch('FormItem', 'on-form-change', this.publicValue); |
299 | 301 | }); |
300 | 302 | }, | ... | ... |
src/components/date-picker/util.js
... | ... | @@ -163,6 +163,8 @@ const RANGE_FORMATTER = function(value, format) { |
163 | 163 | if (start && end) { |
164 | 164 | return formatDate(start, format) + RANGE_SEPARATOR + formatDate(end, format); |
165 | 165 | } |
166 | + } else if (!Array.isArray(value) && value instanceof Date){ | |
167 | + return formatDate(value, format); | |
166 | 168 | } |
167 | 169 | return ''; |
168 | 170 | }; | ... | ... |
test/unit/specs/date-picker.spec.js
1 | -import { createVue, destroyVM, stringToDate, dateToString, promissedTick } from '../util'; | |
1 | +import { createVue, destroyVM, stringToDate, dateToString, dateToTimeString, promissedTick } from '../util'; | |
2 | 2 | |
3 | 3 | describe('DatePicker.vue', () => { |
4 | 4 | let vm; |
... | ... | @@ -26,6 +26,79 @@ describe('DatePicker.vue', () => { |
26 | 26 | }); |
27 | 27 | }); |
28 | 28 | |
29 | + it('should pass correct arguments to on-change event', done => { | |
30 | + const now = new Date(); | |
31 | + const nowDate = dateToString(now); | |
32 | + const nowTime = dateToTimeString(now); | |
33 | + const nextHour = dateToTimeString(now.getTime() + 36e5); | |
34 | + const nextWeek = new Date(now.getTime() + 6048e5); | |
35 | + | |
36 | + let dateValue, dateRangeValue, timeValue, timeRangeValue; | |
37 | + vm = createVue({ | |
38 | + template: ` | |
39 | + <div> | |
40 | + <date-picker type="date" @on-change="onChangeDate"></date-picker> | |
41 | + <date-picker type="daterange" @on-change="onChangeDateRange"></date-picker> | |
42 | + <time-picker type="time" @on-change="onChangeTime"></time-picker> | |
43 | + <time-picker type="timerange" @on-change="onChangeTimeRange"></time-picker> | |
44 | + </div> | |
45 | + `, | |
46 | + methods: { | |
47 | + onChangeDate(val) { | |
48 | + dateValue = val; | |
49 | + }, | |
50 | + onChangeDateRange(val) { | |
51 | + dateRangeValue = val; | |
52 | + }, | |
53 | + onChangeTime(val) { | |
54 | + timeValue = val; | |
55 | + }, | |
56 | + onChangeTimeRange(val) { | |
57 | + timeRangeValue = val; | |
58 | + }, | |
59 | + } | |
60 | + }, true); | |
61 | + | |
62 | + vm.$nextTick(() => { | |
63 | + const [datePicker, dateRangePicker, timePicker, timeRangePicker] = vm.$children; | |
64 | + | |
65 | + datePicker.handleInputChange({target: {value: nowDate}}); | |
66 | + dateRangePicker.handleInputChange({target: {value: [ | |
67 | + nowDate, | |
68 | + dateToString(nextWeek) | |
69 | + ].join(' - ') | |
70 | + }}); | |
71 | + | |
72 | + timePicker.handleInputChange({target: {value: nowTime}}); | |
73 | + const timeRangeString = [ | |
74 | + nowTime, | |
75 | + nextHour | |
76 | + ].join(' - '); | |
77 | + timeRangePicker.handleInputChange({target: { | |
78 | + value: timeRangeString | |
79 | + }}); | |
80 | + | |
81 | + vm.$nextTick(() => { | |
82 | + // DATE | |
83 | + expect(dateValue instanceof Date).to.equal(true); | |
84 | + expect(dateToString(dateValue)).to.equal(nowDate); | |
85 | + // DATERANGE | |
86 | + expect(Array.isArray(dateRangeValue)).to.equal(true); | |
87 | + expect(dateToString(dateRangeValue[0])).to.equal(nowDate); | |
88 | + expect(dateToString(dateRangeValue[1])).to.equal(dateToString(nextWeek)); | |
89 | + // TIME | |
90 | + expect(typeof timeValue).to.equal('string'); | |
91 | + expect(timeValue).to.equal(nowTime); | |
92 | + // TIMERANGE | |
93 | + expect(Array.isArray(timeRangeValue)).to.equal(true); | |
94 | + expect(timeRangeValue[0]).to.equal(nowTime); | |
95 | + expect(timeRangeValue[1]).to.equal(nextHour); | |
96 | + | |
97 | + done(); | |
98 | + }); | |
99 | + }); | |
100 | + }); | |
101 | + | |
29 | 102 | it('should create a DatePicker component of type="datetimerange"', done => { |
30 | 103 | vm = createVue(` |
31 | 104 | <Date-Picker type="datetimerange"></Date-Picker> |
... | ... | @@ -134,7 +207,7 @@ describe('DatePicker.vue', () => { |
134 | 207 | |
135 | 208 | it('should fire `on-change` when reseting value', done => { |
136 | 209 | const now = new Date(); |
137 | - const nowDate = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map(nr => (nr > 9 ? nr : '0' + nr)).join('-'); | |
210 | + const nowDate = dateToString(now); | |
138 | 211 | let onChangeCalled = false; |
139 | 212 | vm = createVue({ |
140 | 213 | template: '<date-picker :value="date" type="date" @on-change="onChange"></date-picker>', | ... | ... |
test/unit/util.js
... | ... | @@ -14,6 +14,8 @@ const createElm = function() { |
14 | 14 | return elm; |
15 | 15 | }; |
16 | 16 | |
17 | +const pad = (nr) => nr < 10 ? '0' + nr : nr; | |
18 | + | |
17 | 19 | /** |
18 | 20 | * 回收 vm |
19 | 21 | * @param {Object} vm |
... | ... | @@ -72,10 +74,20 @@ exports.stringToDate = function(str) { |
72 | 74 | * @param {Date} |
73 | 75 | */ |
74 | 76 | exports.dateToString = function(d) { |
75 | - return [d.getFullYear(), d.getMonth() + 1, d.getDate()].map(nr => nr > 9 ? nr : '0' + nr).join('-'); | |
77 | + return [d.getFullYear(), d.getMonth() + 1, d.getDate()].map(pad).join('-'); | |
76 | 78 | }; |
77 | 79 | |
78 | 80 | /** |
81 | + * Transform Date to HH:MM:SS string | |
82 | + * @param {Date} | |
83 | + */ | |
84 | +exports.dateToTimeString = function(d){ | |
85 | + const date = new Date(d); | |
86 | + return [date.getHours(), date.getMinutes(), date.getSeconds()].map(pad).join(':'); | |
87 | + | |
88 | +} | |
89 | + | |
90 | +/** | |
79 | 91 | * 触发一个事件 |
80 | 92 | * mouseenter, mouseleave, mouseover, keyup, change, click 等 |
81 | 93 | * @param {Element} elm | ... | ... |