DateRange.vue 6.48 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<script lang="ts" setup>
import { onMounted, ref, reactive, computed } from 'vue';
// import { date } from 'quasar';
import { isEmpty } from 'src/common/utils';

interface Props {
  modelValue: any;
  dense?: boolean;
  disable?: boolean;
  readonly?: boolean;
  config?: any;
}
const props = withDefaults(defineProps<Props>(), {
  // modelValue: date.formatDate(Date.now(), 'YYYY/MM/DD HH:mm'),
  modelValue: null,
  dense: false,
  disable: false,
  readonly: false,
  config: () => {
    return {};
  },
});

const emit = defineEmits<{
  (e: 'update:modelValue', value: any): void;
}>();

const myDateRange = ref<any>(null);

const dates = computed({
  set(value: any) {
hucy's avatar
hucy committed
32
    // console.log('set', value);
hucy's avatar
hucy committed
33 34 35 36

    emit('update:modelValue', value);
  },
  get() {
hucy's avatar
hucy committed
37
    // console.log('get', props.modelValue);
hucy's avatar
hucy committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    let res = null;
    // 多选
    if (props.config?.multiple) {
      if (isEmpty(props.modelValue)) {
        res = null;
      } else {
        const typeofs = Object.prototype.toString.call(props.modelValue);
        if (typeofs === '[object Array]') {
          const rangeList = props.modelValue.map((item: any) => {
            const str = `${item.from}~${item.to}`;
            return str;
          });
          res = rangeList.join(',');
        } else {
          res = null;
        }
      }
    }
    // 单选
    else {
      if (isEmpty(props.modelValue)) {
        res = null;
      } else {
        const typeofs = Object.prototype.toString.call(props.modelValue);
        if (typeofs === '[object Object]') {
          const from = props.modelValue.from;
          const to = props.modelValue.to;
          if (!from || !to) {
            console.warn('无效时间范围');
            res = null;
          } else {
            const fromDate = new Date(from);
            const toDate = new Date(to);

            if (toDate <= fromDate) {
              console.warn('结束时间不能大于开始时间');
              res = null;
            } else {
              res = `${from}~${to}`;
            }
          }
        } else if (typeofs === '[object String]') {
          res = props.modelValue;
        } else {
          res = null;
        }
      }
    }
    return res;
  },
});

const dateMask = computed(() => {
  let obj = {
    date: 'YYYY/MM/DD',
  };
  if (!isEmpty(props.config) && props.config.dateMask) {
    const maskList = props.config.dateMask.split(' ');
    obj.date = maskList[0];
  }
  return obj;
});

const dateProxyDate = ref<any>(null);
const dateLocale = reactive({
  daysShort: '六_日_一_二_三_四_五'.split('_'),
  months:
    '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split(
      '_'
    ),
  monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
  firstDayOfWeek: 1,
  format24h: true,
  pluralDay: 'dias',
});

const toolTipData = computed(() => {
  let str = '';
  // 多选
  if (props.config?.multiple) {
    if (!isEmpty(dates.value)) {
      str = dates.value.replace(new RegExp(',', 'g'), '<br />');
    }
  }
  // 单选
  else {
  }

  return str;
});

onMounted(() => {
  const inputElement =
    myDateRange.value.$el.firstElementChild.firstElementChild.firstElementChild
      .firstElementChild;
  inputElement.setAttribute('unselectable', 'on');
  inputElement.setAttribute('readonly', true);
});

function updateProxy() {
  const _dates = dates.value;
  if (!isEmpty(_dates)) {
    // 多选
    if (props.config?.multiple) {
      console.log('updateProxy', _dates);
      const list1 = _dates.split(',');
      const rangeList = list1.map((item: any) => {
        const rangeArr = item.split('~');
        return {
          from: rangeArr[0],
          to: rangeArr[1],
        };
      });
      dateProxyDate.value = rangeList;
    }
    // 单选
    else {
      const dateList = _dates.split('~');
      if (dateList.length === 2) {
        dateProxyDate.value = {
          from: dateList[0],
          to: dateList[1],
        };
      } else if (dateList.length === 1) {
        dateProxyDate.value = dateList[0];
      } else {
        dateProxyDate.value = null;
      }
    }
  } else {
    dateProxyDate.value = null;
  }
}

function onClickDate() {
174
  // console.log('点击确定', dateProxyDate.value);
hucy's avatar
hucy committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  // 多选
  if (props.config?.multiple) {
    if (isEmpty(dateProxyDate.value)) {
      dates.value = null;
    } else {
      dates.value = dateProxyDate.value;
    }
  }
  // 单选
  else {
    if (isEmpty(dateProxyDate.value)) {
      dates.value = null;
    } else {
      dates.value = dateProxyDate.value;
    }
  }
}
</script>
<template>
  <div class="hcy-datetime-pick">
    <q-input
      v-model="dates"
      v-bind="config"
      ref="myDateRange"
      :dense="config.dense === undefined ? props.dense : config.dense"
      :disable="config.disable === undefined ? props.disable : config.disable"
      :readonly="
        config.readonly === undefined ? props.readonly : config.readonly
      "
    >
      <q-tooltip v-if="!isEmpty(dates) && toolTipData">
        <span v-html="toolTipData"></span
      ></q-tooltip>
      <template v-slot:append>
        <q-icon name="event" class="cursor-pointer">
          <q-popup-proxy
            v-if="!props.readonly"
            @before-show="updateProxy"
            transition-show="scale"
            transition-hide="scale"
            class="row"
          >
            <q-date
              flat
              square
              minimal
              range
              :multiple="config.multiple"
              v-model="dateProxyDate"
              :locale="dateLocale"
              :mask="dateMask.date"
              class="my-date"
            >
              <div class="row items-center justify-end q-gutter-sm">
                <q-btn
                  unelevated
                  label="确定"
                  size="sm"
                  color="primary"
                  @click="onClickDate"
                  v-close-popup
                />
              </div>
            </q-date>
          </q-popup-proxy>
        </q-icon>
      </template>
    </q-input>
  </div>
</template>

<style lang="scss" scoped>
.my-date {
  :deep(.q-date__main .q-date__content .q-date__view .q-date__navigation) {
    > .relative-position {
      color: $primary;
    }
  }
}
.my-time {
  :deep(.q-time__header) {
    background-color: white;
  }
  :deep(.q-time__main
      .q-time__content
      .q-time__container-child
      .q-time__clock
      .q-time__clock-circle
      .q-time__clock-position--active) {
    color: white !important;
  }
}
</style>