vue3+antdesginVue+ts a-range-picker(日期时间组件)动态选择跨度不能大于1年

vue3+antdesginVue+ts a-range-picker(日期时间组件)动态选择跨度不能大于1年

话不多说,先上效果
vue3+antdesginVue+ts a-range-picker(日期时间组件)动态选择跨度不能大于1年_第1张图片

  <a-form layout="inline">
      <a-form-item label="时间" name="time">
        <a-range-picker :value="hackValue || time" :disabled-date="disabledDate" @change="onChange"
          @openChange="onOpenChange" @calendarChange="onCalendarChange" />
      a-form-item>
      <a-form-item>
        <a-button type="primary" @click="searchTable">查询a-button>
      a-form-item>
    a-form>
<script lang="ts" setup>
import {  ref, onMounted, } from "vue";
import dayjs, { Dayjs } from "dayjs";

const time = ref<any>([dayjs(new Date()), dayjs(new Date())]);
const hackValue = ref<any>();

const disabledDate = (current: Dayjs) => {
  if (!dates.value || (dates.value as any).length === 0) {
    return false;
  }
  const tooLate = dates.value[0] && current.diff(dates.value[0], 'years') > 0;
  const tooEarly = dates.value[1] && dates.value[1].diff(current, 'years') > 0;
  return tooEarly || tooLate;
};

const onOpenChange = (open: boolean) => {
  if (open) {
    dates.value = [] as any;
    hackValue.value = [] as any;
  } else {
    hackValue.value = undefined;
  }
};

const onChange = (val: any) => {
  time.value = val;
};

const onCalendarChange = (val: any) => {
  dates.value = val;
};
</script >

你可能感兴趣的:(vue,vue.js,typescript,anti-design-vue)