All files / munch-ease-frontend/src/components/meals DatePicker.vue

0% Statements 0/111
0% Branches 0/1
0% Functions 0/1
0% Lines 0/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                               
<template>
  <div class="date-picker">
    <input
      v-model="selectedDate"
      type="text"
      placeholder="Select a date"
      @focus="showDatePicker = true"
      @blur="showDatePicker = false"
    >
    <div
      v-if="showDatePicker"
      class="date-picker-dropdown"
    >
      <ul>
        <li
          v-for="(day, index) in days"
          :key="index"
          @mousedown="selectDate(day)"
        >
          {{ formatDay(day) }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'

const props = defineProps<{ date?: Date }>()
const emit = defineEmits<{ (e: 'date-selected', date: Date): void }>()

function formatDay(date: Date): string {
  const options: Intl.DateTimeFormatOptions = { weekday: 'long', day: 'numeric', month: 'numeric' }
  return date.toLocaleDateString('en-AU', options)
}

const initialDate = props.date ?? new Date()
const selectedDate = ref<string>(formatDay(initialDate))
const showDatePicker = ref(false)

watch(
  () => props.date,
  (newDate) => {
    if (newDate) selectedDate.value = formatDay(newDate)
  }
)

const days = computed<Date[]>(() => {
  const today = new Date()
  const result: Date[] = []
  for (let i = 0; i < 15; i++) {
    const d = new Date(today)
    d.setDate(today.getDate() + i)
    result.push(d)
  }
  return result
})

function selectDate(date: Date) {
  selectedDate.value = formatDay(date)
  showDatePicker.value = false
  emit('date-selected', date)
}
</script>

<style scoped>
.date-picker {
  position: relative;
}

.date-picker input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
}

.date-picker-dropdown {
  position: absolute;
  top: 100%;
  width: 100%;
  margin: auto;
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.date-picker-dropdown ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.date-picker-dropdown li {
  padding: 8px;
  cursor: pointer;
  border-bottom: 1px solid #ccc;
}

.date-picker-dropdown li:hover {
  background-color: #eee;
}

.date-picker-dropdown li:last-child {
  border-bottom: none;
}
</style>