munch-ease-frontend/src/components/DatePicker.vue

112 lines
2.4 KiB
Vue
Raw Normal View History

2024-01-14 01:46:45 +00:00
<template>
<div class="date-picker">
<input
type="text"
v-model="selectedDate"
@focus="showDatePicker = true"
@blur="showDatePicker = false"
placeholder="Select a date"
/>
<div v-if="showDatePicker" class="date-picker-dropdown">
<ul>
<li
v-for="(day, index) in nextSevenDays"
:key="index"
2024-01-17 06:03:11 +00:00
@mousedown="selectDate(day)">
2024-01-14 01:46:45 +00:00
{{ formatDay(day) }}
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
props: {
date: {
type: Date,
default: new Date(),
},
},
data() {
return {
selectedDate: this.formatDay(this.date),
showDatePicker: false,
};
},
computed: {
nextSevenDays() {
const today = new Date();
const nextSevenDays = [];
for (let i = 0; i < 7; i++) {
const date = new Date(today);
date.setDate(today.getDate() + i);
nextSevenDays.push(date);
}
return nextSevenDays;
},
},
methods: {
formatDay(date) {
const options = { weekday: "long", day: "numeric", month: "numeric" };
return date.toLocaleDateString("en-AU", options);
},
selectDate(date) {
this.selectedDate = this.formatSelectedDate(date);
this.showDatePicker = false;
// Emit custom event
this.$emit("date-selected", date);
},
formatSelectedDate(date) {
const options = { weekday: "long", day: "numeric", month: "numeric" };
return date.toLocaleDateString("en-AU", options);
},
},
};
</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:last-child {
border-bottom: none;
}
</style>