cwg-date-picker.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <up-datetime-picker :show="show" v-model="innerValue" :mode="mode" :minDate="minDate" :maxDate="maxDate"
  3. closeOnClickOverlay :formatter="formatter" :confirmText="confirmText" :cancelText="cancelText"
  4. confirmColor="#ea002a" @confirm="handleConfirm" @cancel="handleClose" @close="handleClose" />
  5. </template>
  6. <script setup lang="ts">
  7. import { computed } from 'vue'
  8. import { useI18n } from 'vue-i18n'
  9. import dayjs from "dayjs";
  10. const { t } = useI18n()
  11. const props = defineProps({
  12. modelValue: { type: [String, Number] },
  13. disabled: Boolean,
  14. show: Boolean,
  15. minDate: { type: Date, default: () => new Date(1920, 0, 1).getTime() },
  16. maxDate: { type: Date, default: () => new Date().getTime() },
  17. });
  18. function formatter(type, value) {
  19. if (type === 'year') {
  20. return `${value}${t('common.year')}`
  21. }
  22. if (type === 'month') {
  23. return `${value}${t('common.month')}`
  24. }
  25. if (type === 'day') {
  26. return `${value}${t('common.day')}`
  27. }
  28. return value
  29. }
  30. /** emits */
  31. const emit = defineEmits<{
  32. (e: 'update:modelValue', value?: number): void
  33. (e: 'update:show', value: boolean): void
  34. (e: 'confirm', value: {
  35. timestamp: number
  36. formatted: string
  37. raw: any
  38. }): void
  39. (e: 'cancel'): void
  40. }>()
  41. /** v-model 代理 */
  42. const innerValue = computed({
  43. get: () => dayjs(props.modelValue).valueOf() || new Date().getTime(),
  44. set: (value) => { emit('update:modelValue', value) }
  45. })
  46. /** 多语言 */
  47. const confirmText = computed(() => t('common.confirm'))
  48. const cancelText = computed(() => t('common.cancel'))
  49. const minDate = computed(() =>
  50. props.minDate ?? new Date(1920, 0, 1).getTime()
  51. )
  52. const maxDate = computed(() =>
  53. props.maxDate ?? Date.now()
  54. )
  55. /** 关闭 */
  56. function handleClose() {
  57. emit('update:show', false)
  58. emit('cancel')
  59. }
  60. /** 确认 */
  61. function handleConfirm(e: any) {
  62. const { value } = e;
  63. const dateValue =
  64. typeof value === "number" ? value : new Date(value).getTime();
  65. const formatted = dayjs(dateValue).format("YYYY-MM-DD");
  66. emit('confirm', { formatted, ...e })
  67. emit('update:show', false)
  68. }
  69. </script>