Commit 8f6aeda4be082f94b858117ad747858226cdc126

Authored by Sergio Crisostomo
1 parent 4ec8bc8a

Fix parser and formater

Showing 1 changed file with 24 additions and 33 deletions   Show diff stats
src/components/date-picker/picker.vue
... ... @@ -267,13 +267,11 @@
267 267 }
268 268 },
269 269 data(){
270   - const initialValue = this.formatDate(this.value);
271   -
272 270 return {
273 271 prefixCls: prefixCls,
274 272 showClose: false,
275 273 visible: false,
276   - internalValue: initialValue,
  274 + internalValue: this.parseDate(this.value),
277 275 disableClickOutSide: false, // fixed when click a date,trigger clickoutside to close picker
278 276 disableCloseUnderTransfer: false, // transfer 模式下,点击Drop也会触发关闭,
279 277 selectionMode: this.onSelectionModeChange(this.type)
... ... @@ -282,10 +280,11 @@
282 280 computed: {
283 281 publicValue(){
284 282 if (this.multiple){
285   - return this.internalValue.map(date => this.formatDate(date));
  283 + return this.internalValue.slice();
286 284 } else {
287 285 const isRange = this.type.includes('range');
288   - return isRange ? this.formatDate(this.internalValue) : this.formatDate(this.internalValue[0]);
  286 + const val = this.internalValue.map(date => date instanceof Date ? new Date(date) : date);
  287 + return (isRange || this.multiple) ? val : val[0];
289 288 }
290 289 },
291 290  
... ... @@ -307,13 +306,7 @@
307 306 if (!value) return;
308 307  
309 308 if (this.multiple) return value.map(date => this.formatDate(date)).join(', ');
310   -
311   - const formatter = (
312   - TYPE_VALUE_RESOLVER_MAP[this.type] ||
313   - TYPE_VALUE_RESOLVER_MAP['default']
314   - ).formatter;
315   - const format = DEFAULT_FORMATS[this.type];
316   - return formatter(value, this.format || format);
  309 + return this.formatDate(value);
317 310 },
318 311 isConfirm(){
319 312 return this.confirm || this.type === 'datetime' || this.type === 'datetimerange' || this.multiple;
... ... @@ -353,7 +346,7 @@
353 346  
354 347 if (newValue !== oldValue) {
355 348 this.emitChange();
356   - this.internalValue = this.formatDate(newValue);
  349 + this.internalValue = this.parseDate(newValue);
357 350 }
358 351 },
359 352 handleInputMouseenter () {
... ... @@ -390,9 +383,8 @@
390 383 this.dispatch('FormItem', 'on-form-change', this.publicValue);
391 384 });
392 385 },
393   - formatDate (val) {
  386 + parseDate(val) {
394 387 const isRange = this.type.includes('range');
395   -
396 388 const type = this.type;
397 389 const parser = (
398 390 TYPE_VALUE_RESOLVER_MAP[type] ||
... ... @@ -408,11 +400,20 @@
408 400 val = val.map(date => new Date(date)); // try to parse
409 401 val = val.map(date => isNaN(date.getTime()) ? null : date); // check if parse passed
410 402 }
411   - } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){
  403 + } else if (typeof val === 'string' && type.indexOf('time') !== 0){
412 404 val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
413 405 }
  406 +
414 407 return isRange ? val : [val];
415 408 },
  409 + formatDate(value){
  410 + const {formatter} = (
  411 + TYPE_VALUE_RESOLVER_MAP[this.type] ||
  412 + TYPE_VALUE_RESOLVER_MAP['default']
  413 + );
  414 + const format = DEFAULT_FORMATS[this.type];
  415 + return formatter(value, this.format || format);
  416 + },
416 417 onPick(dates, visible = false) {
417 418  
418 419 if (this.multiple){
... ... @@ -442,30 +443,20 @@
442 443 this.$emit('on-open-change', state);
443 444 },
444 445 value(val) {
445   - const type = this.type;
446   - const parser = (
447   - TYPE_VALUE_RESOLVER_MAP[type] ||
448   - TYPE_VALUE_RESOLVER_MAP['default']
449   - ).parser;
450   -
451   - if (val && type === 'time' && !(val instanceof Date)) {
452   - val = parser(val, this.format || DEFAULT_FORMATS[type]);
453   - } else if (val && type.match(/range$/) && Array.isArray(val) && val.filter(Boolean).length === 2 && !(val[0] instanceof Date) && !(val[1] instanceof Date)) {
454   - val = val.join(RANGE_SEPARATOR);
455   - val = parser(val, this.format || DEFAULT_FORMATS[type]);
456   - } else if (typeof val === 'string' && type.indexOf('time') !== 0 ){
457   - val = parser(val, this.format || DEFAULT_FORMATS[type]) || val;
458   - }
  446 + this.internalValue = this.parseDate(val);
459 447  
460   - this.internalValue = val;
461   - this.$emit('input', val);
462 448 },
463 449 open (val) {
464 450 this.visible = val === true;
465 451 },
466 452 type(type){
467 453 this.onSelectionModeChange(type);
468   - }
  454 + },
  455 + publicValue(now, before){
  456 + const newValue = JSON.stringify(now);
  457 + const oldValue = JSON.stringify(before);
  458 + if (newValue !== oldValue) this.$emit('input', now); // to update v-model
  459 + },
469 460 },
470 461 mounted () {
471 462 if (this.open !== null) this.visible = this.open;
... ...