Sfoglia il codice sorgente

DatePicker: fix, add test for default-value

wacky6 8 anni fa
parent
commit
6ee73f7715
2 ha cambiato i file con 40 aggiunte e 2 eliminazioni
  1. 2 2
      packages/date-picker/src/picker.vue
  2. 38 0
      test/unit/specs/date-picker.spec.js

+ 2 - 2
packages/date-picker/src/picker.vue

@@ -354,7 +354,7 @@ export default {
     handleClickIcon() {
       if (this.readonly || this.disabled) return;
       if (this.showClose) {
-        this.currentValue = '';
+        this.currentValue = this.$options.defaultValue || '';
         this.showClose = false;
       } else {
         this.pickerVisible = !this.pickerVisible;
@@ -431,7 +431,7 @@ export default {
     },
 
     mountPicker() {
-      this.panel.defaultValue = this.currentValue;
+      this.panel.defaultValue = this.defaultValue || this.currentValue;
       this.picker = new Vue(this.panel).$mount();
       this.picker.popperClass = this.popperClass;
       this.popperElm = this.picker.$el;

+ 38 - 0
test/unit/specs/date-picker.spec.js

@@ -202,6 +202,44 @@ describe('DatePicker', () => {
     }, DELAY);
   });
 
+  it('default value', done => {
+    const toDateStr = date => {
+      let d = new Date(date);
+      return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`;
+    };
+    let today = new Date();
+    let nextMonth = new Date(today);
+    nextMonth.setDate(1);
+    if (nextMonth.getMonth() === 12) {
+      nextMonth.setFullYear(today.getFullYear + 1);
+      nextMonth.setMonth(1);
+    } else {
+      nextMonth.setMonth(today.getMonth() + 1);
+    }
+    let nextMonthStr = toDateStr(nextMonth);
+
+    vm = createVue({
+      template: `<el-date-picker v-model="value" ref="compo" default-value="${nextMonthStr}" />`,
+      data() {
+        return {
+          value: ''
+        };
+      }
+    }, true);
+
+    const input = vm.$el.querySelector('input');
+
+    input.focus();
+    setTimeout(_ => {
+      const $el = vm.$refs.compo.picker.$el;
+      $el.querySelector('td.current').click();
+      setTimeout(_ => {
+        expect(vm.value).to.equal(nextMonthStr);
+      });
+      done();
+    });
+  });
+
   describe('keydown', () => {
     let input;
     let keyDown = function(el, keyCode) {