TimerTabs.vue 2.7 KB
<!-- com-timer-tabs-->
<template>
  <div class="com-timer-tabs-container">
    <van-tabs v-model="active" color="#3975C6" sticky @click="tabClickHandle">
      <van-tab :title="date.title" v-for="date in dates" :key="date.value"></van-tab>
    </van-tabs>
    <van-calendar v-model="isShowCalendar" @confirm="onConfirm" :show-title="false" :color="color" type="range" :allow-same-day="true" :min-date="min" :max-date="max" :style="{ height: popHeight }" />
  </div>
</template>

<script>
import { calendar } from '@/mixins'
import * as dayjs from 'dayjs'
function format(s, e) {
  return {
    startTime: s.format('YYYY-MM-DD 00:00:00'),
    endTime: e.format('YYYY-MM-DD 23:59:59')
  }
}
const aDates = Object.freeze([
  {
    title: '今天',
    value: 0,
    getDate() {
      return format(dayjs().startOf('date'), dayjs().startOf('date'))
    }
  },
  {
    title: '本月',
    value: 1,
    getDate() {
      return format(dayjs().startOf('month'), dayjs().endOf('month'))
    }
  },
  {
    title: '本周',
    value: 2,
    getDate() {
      if (dayjs().day() == 0) {
        return format(dayjs().add(-6, 'day'), dayjs())
      } else {
        return format(
          dayjs()
            .startOf('week')
            .add(1, 'day'),
          dayjs()
            .endOf('week')
            .add(1, 'day')
        )
      }
    }
  },
  {
    title: '自定义',
    value: 3
  }
])

export default {
  name: 'TimerTabs',
  mixins: [calendar],
  props: ['value', 'onChange', 'minDate', 'maxDate'],
  data() {
    return {
      dates: aDates,
      active: 0,
      tabIndex: 0
    }
  },
  computed: {
    min() {
      return this.minDate || new Date(2020, 0, 1)
    },
    max() {
      return this.maxDate || new Date()
    }
  },
  mounted() {
    this.$emit('update:value', aDates[this.tabIndex].getDate())
  },
  methods: {
    onClickLeft() {},
    onClickRight() {},
    tabClickHandle(index) {
      if (index === 3) {
        this.isShowCalendar = true
        return
      }
      this.broadcast(aDates[index].getDate())
    },
    dateChangeHandle(date) {
      this.broadcast(date)
    },
    broadcast(value) {
      this.$emit('update:value', value)
      this.onChange && this.onChange(value)
    },
    onConfirm([start, end]) {
      this.broadcast(format(dayjs(start), dayjs(end)))
      document.getElementsByClassName('van-overlay')[0].click() //hack
    }
  }
}
</script>
<style lang="scss" scoped>
.com-timer-tabs-container {
  // /deep/.self{
  //   .van-tabs{
  //     height: calc(100vh - 46px);
  //     display: flex;
  //     flex-direction: column;
  //     &__content{
  //       flex:1;
  //       overflow: auto;
  //       .van-tab__pane{
  //         height: 100%;
  //       }
  //     }
  //   }
  // }
}
</style>