DateTimePick.vue 3.87 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 32 33 34 35 36 37 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
<script lang="ts" setup>
import { onMounted, ref, reactive, computed } from 'vue';
import { date } from 'quasar';

interface Props {
  modelValue: string;
  format24h?: boolean;
  withSeconds?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
  modelValue: date.formatDate(Date.now(), 'YYYY/MM/DD HH:mm'),
  format24h: false,
  withSeconds: false,
});

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

const dates = computed({
  set(value: string) {
    emit('update:modelValue', value);
  },
  get() {
    return props.modelValue;
  },
});

const timeMask = computed(() => {
  if (props.withSeconds) {
    return 'HH:mm:ss';
  } else {
    return 'HH:mm';
  }
});

const dateRules = computed(() => {
  let dateRegular: RegExp;
  if (props.withSeconds) {
    dateRegular =
      /^-?[\d]+\/[0-1]\d\/[0-3]\d ([0-1]?\d|2[0-3]):[0-5]\d:[0-5]\d$/;
  } else {
    dateRegular = /^-?[\d]+\/[0-1]\d\/[0-3]\d (\d{2}):(\d{2})$/;
  }
  return [(v: string) => dateRegular.test(v)];
});

const dateProxyDate = ref('');
const timeProxyDate = ref('');
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',
});

onMounted(() => {
  getNowDate();
});

function getNowDate() {
  dateProxyDate.value = date.formatDate(Date.now(), 'YYYY/MM/DD');
  timeProxyDate.value = date.formatDate(Date.now(), timeMask.value);
}

function updateProxy() {
  const _dates = dates.value;
  const dateArr = _dates.split(' ');

  dateProxyDate.value = dateArr[0];
  timeProxyDate.value = dateArr[1];
}

function onClickDate() {
  const datesStr = `${dateProxyDate.value} ${timeProxyDate.value}`;
  dates.value = datesStr;
}
</script>
<template>
  <div class="hcy-datetime-pick">
    <q-input filled dense v-model="dates" :rules="dateRules">
      <template v-slot:append>
        <q-icon name="event" class="cursor-pointer">
          <q-popup-proxy
            @before-show="updateProxy"
            transition-show="scale"
            transition-hide="scale"
            class="row"
          >
            <q-date
              flat
              square
              minimal
              v-model="dateProxyDate"
              :locale="dateLocale"
              mask="YYYY/MM/DD"
              class="my-date"
            >
            </q-date>
            <q-time
              flat
              square
              v-model="timeProxyDate"
              :mask="timeMask"
              :format24h="format24h"
              :with-seconds="withSeconds"
              text-color="primary"
              class="my-time"
            >
              <div class="row items-center justify-end q-gutter-sm">
                <q-btn
                  label="此刻"
                  size="sm"
                  color="primary"
                  flat
                  @click="getNowDate"
                />
                <q-btn
                  unelevated
                  label="确定"
                  size="sm"
                  color="primary"
                  @click="onClickDate"
                  v-close-popup
                />
              </div>
            </q-time>
          </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>