Browse Source

date-picker : fix disabledDate bug 。

未改之前,设置禁用日期后,年和月的禁用状态不准。因为只选取了年和月的某一天进行判断。修改为年和月的每一天都进行判断。
liyangworld 8 years ago
parent
commit
3cf5fd94c6

+ 21 - 3
packages/date-picker/src/basic/month-table.vue

@@ -63,11 +63,29 @@
     methods: {
       getCellStyle(month) {
         const style = {};
-        const date = new Date(this.date);
 
+        var year = this.date.getFullYear();
+        var date = new Date(0);
+        date.setFullYear(year);
         date.setMonth(month);
-        style.disabled = typeof this.disabledDate === 'function' &&
-          this.disabledDate(date);
+        date.setHours(0);
+        var nextMonth = new Date(date);
+        nextMonth.setMonth(month + 1);
+
+        var flag = false;
+        if (typeof this.disabledDate === 'function') {
+
+          while (date < nextMonth) {
+            if (this.disabledDate(date)) {
+              date = new Date(date.getTime() + 8.64e7);
+            } else {
+              break;
+            }
+          }
+          if ((date - nextMonth) === 0) flag = true;
+        }
+
+        style.disabled = flag;
         style.current = this.month === month;
 
         return style;

+ 20 - 3
packages/date-picker/src/basic/year-table.vue

@@ -62,11 +62,28 @@
     methods: {
       getCellStyle(year) {
         const style = {};
-        const date = new Date(this.date);
 
+        var date = new Date(0);
         date.setFullYear(year);
-        style.disabled = typeof this.disabledDate === 'function' &&
-          this.disabledDate(date);
+        date.setHours(0);
+        var nextYear = new Date(date);
+        nextYear.setFullYear(year + 1);
+
+        var flag = false;
+        if (typeof this.disabledDate === 'function') {
+
+          while (date < nextYear) {
+            if (this.disabledDate(date)) {
+              date = new Date(date.getTime() + 8.64e7);
+            } else {
+              break;
+            }
+          }
+          if ((date - nextYear) === 0) flag = true;
+
+        }
+
+        style.disabled = flag;
         style.current = Number(this.year) === year;
 
         return style;