DefaultInputDatePicker.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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">
  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. @update:model-value="handleDateSelection"
  38. />
  39. </q-tab-panel>
  40. <q-tab-panel name="time" class="q-pa-none">
  41. <q-time v-model="date" mask="YYYY-MM-DD HH:mm" format24h color="violet-normal" />
  42. </q-tab-panel>
  43. </q-tab-panels>
  44. </template>
  45. </q-popup-proxy>
  46. </q-icon>
  47. </template>
  48. </DefaultInput>
  49. </template>
  50. <script setup>
  51. import { watch, ref, computed } from "vue";
  52. import { useI18n } from "vue-i18n";
  53. import masks from "src/helpers/masks";
  54. import DefaultInput from "./DefaultInput.vue";
  55. const { label, time } = defineProps({
  56. label: {
  57. type: String,
  58. default: () => useI18n().t("common.terms.date"),
  59. },
  60. time: {
  61. type: Boolean,
  62. default: false,
  63. },
  64. });
  65. const treatedDate = defineModel({ type: [String, null] });
  66. const untreatedDate = defineModel("untreatedDate", { type: [String, null] });
  67. const error = defineModel("error", {
  68. type: [String, Object, Array, Boolean, null],
  69. });
  70. const inputRef = ref(null);
  71. const activePanel = ref("date");
  72. const date = ref();
  73. const handleDateSelection = () => {
  74. if (time) {
  75. activePanel.value = "time";
  76. }
  77. };
  78. const formatDate = (value) => {
  79. if (!value) return null;
  80. const [datePart, timePart] = value.split(" ");
  81. const formattedDate = datePart.split("-").reverse().join("/");
  82. return time && timePart ? `${formattedDate} ${timePart}` : formattedDate;
  83. };
  84. const unformatDate = (value) => {
  85. if (!value) return null;
  86. const [datePart, timePart] = value.split(" ");
  87. const formattedDate = datePart.split("/").reverse().join("-");
  88. return time && timePart ? `${formattedDate} ${timePart}` : formattedDate;
  89. };
  90. const inputMask = computed(() => {
  91. if (!inputRef.value) return "";
  92. if (time) {
  93. return masks.Brasil.datetime;
  94. }
  95. return masks.Brasil.date;
  96. });
  97. watch(date, (value) => {
  98. if (!value) return;
  99. untreatedDate.value = value;
  100. treatedDate.value = formatDate(value);
  101. });
  102. watch(treatedDate, (value) => {
  103. if (!value) {
  104. date.value = null;
  105. untreatedDate.value = null;
  106. treatedDate.value = null;
  107. activePanel.value = "date";
  108. return;
  109. }
  110. date.value = unformatDate(value);
  111. });
  112. watch(
  113. untreatedDate,
  114. (value) => {
  115. if (!value) {
  116. date.value = null;
  117. untreatedDate.value = null;
  118. treatedDate.value = null;
  119. activePanel.value = "date";
  120. return;
  121. }
  122. date.value = value;
  123. treatedDate.value = formatDate(value);
  124. },
  125. { immediate: true }
  126. );
  127. </script>