Commit 4a1734b77edac0a1ac9465d5f17856b6a3725425
1 parent
a1c88eba
fix time and time-range parsers
Showing
2 changed files
with
12 additions
and
4 deletions
Show diff stats
src/components/date-picker/picker.vue
| @@ -160,11 +160,17 @@ | @@ -160,11 +160,17 @@ | ||
| 160 | // check if its empty values | 160 | // check if its empty values |
| 161 | if (isEmptyArray(val)) return true; | 161 | if (isEmptyArray(val)) return true; |
| 162 | 162 | ||
| 163 | + // check if is time format | ||
| 164 | + if (val[0].match(/^[\d:]+$/) && val[1].match(/^[\d:]+$/)) return true; | ||
| 165 | + | ||
| 163 | // check if its valid value | 166 | // check if its valid value |
| 164 | const [start, end] = val.map(v => new Date(v)); | 167 | const [start, end] = val.map(v => new Date(v)); |
| 165 | return !isNaN(start.getTime()) && !isNaN(end.getTime()); | 168 | return !isNaN(start.getTime()) && !isNaN(end.getTime()); |
| 166 | } else { | 169 | } else { |
| 167 | - if (typeof val === 'string') val = val.trim(); | 170 | + if (typeof val === 'string') { |
| 171 | + val = val.trim(); | ||
| 172 | + if (val.match(/^[\d:]+$/)) return true; // time format | ||
| 173 | + } | ||
| 168 | const date = new Date(val); | 174 | const date = new Date(val); |
| 169 | return val === '' || val === null || !isNaN(date.getTime()); | 175 | return val === '' || val === null || !isNaN(date.getTime()); |
| 170 | } | 176 | } |
| @@ -176,10 +182,10 @@ | @@ -176,10 +182,10 @@ | ||
| 176 | } | 182 | } |
| 177 | }, | 183 | }, |
| 178 | data(){ | 184 | data(){ |
| 179 | - | ||
| 180 | const isRange = this.type.includes('range'); | 185 | const isRange = this.type.includes('range'); |
| 181 | const emptyArray = isRange ? [null, null] : [null]; | 186 | const emptyArray = isRange ? [null, null] : [null]; |
| 182 | - const initialValue = isEmptyArray(this.value || []) ? emptyArray : this.parseDate(this.value); | 187 | + const initialValue = isEmptyArray((isRange ? this.value : [this.value]) || []) ? emptyArray : this.parseDate(this.value); |
| 188 | + | ||
| 183 | return { | 189 | return { |
| 184 | prefixCls: prefixCls, | 190 | prefixCls: prefixCls, |
| 185 | showClose: false, | 191 | showClose: false, |
| @@ -329,6 +335,8 @@ | @@ -329,6 +335,8 @@ | ||
| 329 | } else { | 335 | } else { |
| 330 | if (typeof val === 'string') { | 336 | if (typeof val === 'string') { |
| 331 | val = parser(val, format); | 337 | val = parser(val, format); |
| 338 | + } else if (type === 'timerange') { | ||
| 339 | + val = parser(val, format); | ||
| 332 | } else { | 340 | } else { |
| 333 | val = val.map(date => new Date(date)); // try to parse | 341 | val = val.map(date => new Date(date)); // try to parse |
| 334 | val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed | 342 | val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed |
src/components/date-picker/util.js
| @@ -167,7 +167,7 @@ const RANGE_FORMATTER = function(value, format) { | @@ -167,7 +167,7 @@ const RANGE_FORMATTER = function(value, format) { | ||
| 167 | return ''; | 167 | return ''; |
| 168 | }; | 168 | }; |
| 169 | const RANGE_PARSER = function(text, format) { | 169 | const RANGE_PARSER = function(text, format) { |
| 170 | - const array = text.split(RANGE_SEPARATOR); | 170 | + const array = Array.isArray(text) ? text : text.split(RANGE_SEPARATOR); |
| 171 | if (array.length === 2) { | 171 | if (array.length === 2) { |
| 172 | const range1 = array[0]; | 172 | const range1 = array[0]; |
| 173 | const range2 = array[1]; | 173 | const range2 = array[1]; |