DefaultInputDatePicker.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <DefaultInput
  3. v-model="treatedDate"
  4. v-model:error="error"
  5. v-bind="$attrs"
  6. :input-ref="inputRef"
  7. :label="label"
  8. :mask="inputMask"
  9. clearable
  10. >
  11. <template #append>
  12. <q-icon
  13. :name="time ? 'mdi-calendar-clock' : 'mdi-calendar'"
  14. class="cursor-pointer"
  15. >
  16. <q-popup-proxy cover transition-show="scale" transition-hide="scale">
  17. <template v-if="!time">
  18. <q-date v-model="date" mask="YYYY-MM-DD" color="violet-normal" :options="dateOptions">
  19. <div class="row items-center justify-end">
  20. <q-btn v-close-popup label="OK" color="violet-normal" flat />
  21. </div>
  22. </q-date>
  23. </template>
  24. <template v-else>
  25. <q-tab-panels
  26. v-model="activePanel"
  27. animated
  28. transition-prev="slide-right"
  29. transition-next="slide-left"
  30. class="bg-white"
  31. >
  32. <q-tab-panel name="date" class="q-pa-none">
  33. <q-date
  34. v-model="date"
  35. mask="YYYY-MM-DD HH:mm"
  36. color="violet-normal"
  37. :options="dateOptions"
  38. @update:model-value="handleDateSelection"
  39. />
  40. </q-tab-panel>
  41. <q-tab-panel name="time" class="q-pa-none">
  42. <q-time v-model="date" mask="YYYY-MM-DD HH:mm" format24h color="violet-normal" />
  43. </q-tab-panel>
  44. </q-tab-panels>
  45. </template>
  46. </q-popup-proxy>
  47. </q-icon>
  48. </template>
  49. </DefaultInput>
  50. </template>
  51. <script setup>
  52. import { watch, ref, computed } from "vue";
  53. import { useI18n } from "vue-i18n";
  54. import masks from "src/helpers/masks";
  55. import DefaultInput from "./DefaultInput.vue";
  56. const { label, time, dateOptions } = defineProps({
  57. label: {
  58. type: String,
  59. default: () => useI18n().t("common.terms.date"),
  60. },
  61. time: {
  62. type: Boolean,
  63. default: false,
  64. },
  65. dateOptions: {
  66. type: [Array, Function],
  67. default: undefined,
  68. },
  69. });
  70. const treatedDate = defineModel({ type: [String, null] });
  71. const untreatedDate = defineModel("untreatedDate", { type: [String, null] });
  72. const error = defineModel("error", {
  73. type: [String, Object, Array, Boolean, null],
  74. });
  75. const inputRef = ref(null);
  76. const activePanel = ref("date");
  77. const date = ref();
  78. const handleDateSelection = () => {
  79. if (time) {
  80. activePanel.value = "time";
  81. }
  82. };
  83. const formatDate = (value) => {
  84. if (!value) return null;
  85. const [datePart, timePart] = value.split(" ");
  86. const formattedDate = datePart.split("-").reverse().join("/");
  87. return time && timePart ? `${formattedDate} ${timePart}` : formattedDate;
  88. };
  89. const unformatDate = (value) => {
  90. if (!value) return null;
  91. const [datePart, timePart] = value.split(" ");
  92. const formattedDate = datePart.split("/").reverse().join("-");
  93. return time && timePart ? `${formattedDate} ${timePart}` : formattedDate;
  94. };
  95. const inputMask = computed(() => {
  96. // if (!inputRef.value) return "";
  97. if (time) {
  98. return masks.Brasil.datetime;
  99. }
  100. return masks.Brasil.date;
  101. });
  102. watch(date, (value) => {
  103. if (!value) return;
  104. untreatedDate.value = value;
  105. treatedDate.value = formatDate(value);
  106. });
  107. watch(treatedDate, (value) => {
  108. if (!value) {
  109. date.value = null;
  110. untreatedDate.value = null;
  111. treatedDate.value = null;
  112. activePanel.value = "date";
  113. return;
  114. }
  115. date.value = unformatDate(value);
  116. });
  117. watch(
  118. untreatedDate,
  119. (value) => {
  120. if (!value) {
  121. date.value = null;
  122. untreatedDate.value = null;
  123. treatedDate.value = null;
  124. activePanel.value = "date";
  125. return;
  126. }
  127. date.value = value;
  128. treatedDate.value = formatDate(value);
  129. },
  130. { immediate: true }
  131. );
  132. </script>