| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | 
							- import { VantComponent } from '../common/component';
 
- import {
 
-   ROW_HEIGHT,
 
-   getNextDay,
 
-   compareDay,
 
-   copyDates,
 
-   calcDateNum,
 
-   formatMonthTitle,
 
-   compareMonth,
 
-   getMonths,
 
-   getDayByOffset,
 
- } from './utils';
 
- import Toast from '../toast/toast';
 
- import { requestAnimationFrame } from '../common/utils';
 
- VantComponent({
 
-   props: {
 
-     title: {
 
-       type: String,
 
-       value: '日期选择',
 
-     },
 
-     color: String,
 
-     show: {
 
-       type: Boolean,
 
-       observer(val) {
 
-         if (val) {
 
-           this.initRect();
 
-           this.scrollIntoView();
 
-         }
 
-       },
 
-     },
 
-     formatter: null,
 
-     confirmText: {
 
-       type: String,
 
-       value: '确定',
 
-     },
 
-     rangePrompt: String,
 
-     defaultDate: {
 
-       type: [Number, Array],
 
-       observer(val) {
 
-         this.setData({ currentDate: val });
 
-         this.scrollIntoView();
 
-       },
 
-     },
 
-     allowSameDay: Boolean,
 
-     confirmDisabledText: String,
 
-     type: {
 
-       type: String,
 
-       value: 'single',
 
-       observer: 'reset',
 
-     },
 
-     minDate: {
 
-       type: null,
 
-       value: Date.now(),
 
-     },
 
-     maxDate: {
 
-       type: null,
 
-       value: new Date(
 
-         new Date().getFullYear(),
 
-         new Date().getMonth() + 6,
 
-         new Date().getDate()
 
-       ).getTime(),
 
-     },
 
-     position: {
 
-       type: String,
 
-       value: 'bottom',
 
-     },
 
-     rowHeight: {
 
-       type: [Number, String],
 
-       value: ROW_HEIGHT,
 
-     },
 
-     round: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     poppable: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     showMark: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     showTitle: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     showConfirm: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     showSubtitle: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     safeAreaInsetBottom: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     closeOnClickOverlay: {
 
-       type: Boolean,
 
-       value: true,
 
-     },
 
-     maxRange: {
 
-       type: [Number, String],
 
-       value: null,
 
-     },
 
-   },
 
-   data: {
 
-     subtitle: '',
 
-     currentDate: null,
 
-     scrollIntoView: '',
 
-   },
 
-   created() {
 
-     this.setData({
 
-       currentDate: this.getInitialDate(),
 
-     });
 
-   },
 
-   mounted() {
 
-     if (this.data.show || !this.data.poppable) {
 
-       this.initRect();
 
-       this.scrollIntoView();
 
-     }
 
-   },
 
-   methods: {
 
-     reset() {
 
-       this.setData({ currentDate: this.getInitialDate() });
 
-       this.scrollIntoView();
 
-     },
 
-     initRect() {
 
-       if (this.contentObserver != null) {
 
-         this.contentObserver.disconnect();
 
-       }
 
-       const contentObserver = this.createIntersectionObserver({
 
-         thresholds: [0, 0.1, 0.9, 1],
 
-         observeAll: true,
 
-       });
 
-       this.contentObserver = contentObserver;
 
-       contentObserver.relativeTo('.van-calendar__body');
 
-       contentObserver.observe('.month', (res) => {
 
-         if (res.boundingClientRect.top <= res.relativeRect.top) {
 
-           // @ts-ignore
 
-           this.setData({ subtitle: formatMonthTitle(res.dataset.date) });
 
-         }
 
-       });
 
-     },
 
-     getInitialDate() {
 
-       const { type, defaultDate, minDate } = this.data;
 
-       if (type === 'range') {
 
-         const [startDay, endDay] = defaultDate || [];
 
-         return [
 
-           startDay || minDate,
 
-           endDay || getNextDay(new Date(minDate)).getTime(),
 
-         ];
 
-       }
 
-       if (type === 'multiple') {
 
-         return defaultDate || [minDate];
 
-       }
 
-       return defaultDate || minDate;
 
-     },
 
-     scrollIntoView() {
 
-       requestAnimationFrame(() => {
 
-         const {
 
-           currentDate,
 
-           type,
 
-           show,
 
-           poppable,
 
-           minDate,
 
-           maxDate,
 
-         } = this.data;
 
-         // @ts-ignore
 
-         const targetDate = type === 'single' ? currentDate : currentDate[0];
 
-         const displayed = show || !poppable;
 
-         if (!targetDate || !displayed) {
 
-           return;
 
-         }
 
-         const months = getMonths(minDate, maxDate);
 
-         months.some((month, index) => {
 
-           if (compareMonth(month, targetDate) === 0) {
 
-             this.setData({ scrollIntoView: `month${index}` });
 
-             return true;
 
-           }
 
-           return false;
 
-         });
 
-       });
 
-     },
 
-     onOpen() {
 
-       this.$emit('open');
 
-     },
 
-     onOpened() {
 
-       this.$emit('opened');
 
-     },
 
-     onClose() {
 
-       this.$emit('close');
 
-     },
 
-     onClosed() {
 
-       this.$emit('closed');
 
-     },
 
-     onClickDay(event) {
 
-       const { date } = event.detail;
 
-       const { type, currentDate, allowSameDay } = this.data;
 
-       if (type === 'range') {
 
-         // @ts-ignore
 
-         const [startDay, endDay] = currentDate;
 
-         if (startDay && !endDay) {
 
-           const compareToStart = compareDay(date, startDay);
 
-           if (compareToStart === 1) {
 
-             this.select([startDay, date], true);
 
-           } else if (compareToStart === -1) {
 
-             this.select([date, null]);
 
-           } else if (allowSameDay) {
 
-             this.select([date, date]);
 
-           }
 
-         } else {
 
-           this.select([date, null]);
 
-         }
 
-       } else if (type === 'multiple') {
 
-         let selectedIndex;
 
-         // @ts-ignore
 
-         const selected = currentDate.some((dateItem, index) => {
 
-           const equal = compareDay(dateItem, date) === 0;
 
-           if (equal) {
 
-             selectedIndex = index;
 
-           }
 
-           return equal;
 
-         });
 
-         if (selected) {
 
-           // @ts-ignore
 
-           const cancelDate = currentDate.splice(selectedIndex, 1);
 
-           this.setData({ currentDate });
 
-           this.unselect(cancelDate);
 
-         } else {
 
-           // @ts-ignore
 
-           this.select([...currentDate, date]);
 
-         }
 
-       } else {
 
-         this.select(date, true);
 
-       }
 
-     },
 
-     unselect(dateArray) {
 
-       const date = dateArray[0];
 
-       if (date) {
 
-         this.$emit('unselect', copyDates(date));
 
-       }
 
-     },
 
-     select(date, complete) {
 
-       if (complete && this.data.type === 'range') {
 
-         const valid = this.checkRange(date);
 
-         if (!valid) {
 
-           // auto selected to max range if showConfirm
 
-           if (this.data.showConfirm) {
 
-             this.emit([
 
-               date[0],
 
-               getDayByOffset(date[0], this.data.maxRange - 1),
 
-             ]);
 
-           } else {
 
-             this.emit(date);
 
-           }
 
-           return;
 
-         }
 
-       }
 
-       this.emit(date);
 
-       if (complete && !this.data.showConfirm) {
 
-         this.onConfirm();
 
-       }
 
-     },
 
-     emit(date) {
 
-       const getTime = (date) => (date instanceof Date ? date.getTime() : date);
 
-       this.setData({
 
-         currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
 
-       });
 
-       this.$emit('select', copyDates(date));
 
-     },
 
-     checkRange(date) {
 
-       const { maxRange, rangePrompt } = this.data;
 
-       if (maxRange && calcDateNum(date) > maxRange) {
 
-         Toast({
 
-           context: this,
 
-           message: rangePrompt || `选择天数不能超过 ${maxRange} 天`,
 
-         });
 
-         return false;
 
-       }
 
-       return true;
 
-     },
 
-     onConfirm() {
 
-       if (
 
-         this.data.type === 'range' &&
 
-         !this.checkRange(this.data.currentDate)
 
-       ) {
 
-         return;
 
-       }
 
-       wx.nextTick(() => {
 
-         // @ts-ignore
 
-         this.$emit('confirm', copyDates(this.data.currentDate));
 
-       });
 
-     },
 
-   },
 
- });
 
 
  |