|
1 | 1 | <template>
|
2 |
| - <div class="date-input-container"> |
3 |
| - <label for="date-input"></label> |
4 |
| - <input type="date" id="date-input" v-model="selectedDate" @change="emitDateChange" /> |
| 2 | + <div class="date-picker"> |
| 3 | + <input |
| 4 | + type="text" |
| 5 | + v-model="formattedDate" |
| 6 | + @focus="showCalendar" |
| 7 | + class="date-input" |
| 8 | + readonly |
| 9 | + /> |
| 10 | + <div v-if="isCalendarVisible" class="calendar"> |
| 11 | + <div class="calendar-header"> |
| 12 | + <button @click="prevMonth"><</button> |
| 13 | + <span>{{ monthNames[currentMonth] }} {{ currentYear }}</span> |
| 14 | + <button @click="nextMonth">></button> |
| 15 | + </div> |
| 16 | + <div class="calendar-grid"> |
| 17 | + <div class="day" v-for="(day, index) in weekDays" :key="index">{{ day }}</div> |
| 18 | + <div |
| 19 | + v-for="date in displayedDates" |
| 20 | + :key="date.dateString" |
| 21 | + class="day" |
| 22 | + :class="{ 'hover': date.isHover, 'selected': date.isSelected }" |
| 23 | + @mouseover="hoverDate(date.dateString)" |
| 24 | + @mouseleave="clearHover" |
| 25 | + @click="selectDate(date.dateString)" |
| 26 | + > |
| 27 | + {{ date.day }} |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + </div> |
5 | 31 | </div>
|
6 | 32 | </template>
|
7 | 33 |
|
8 | 34 | <script>
|
9 | 35 | export default {
|
| 36 | + props: { |
| 37 | + initialDate: { |
| 38 | + type: String, |
| 39 | + default: () => new Date().toISOString().split('T')[0], // Default to today |
| 40 | + }, |
| 41 | + }, |
10 | 42 | data() {
|
11 | 43 | return {
|
12 |
| - selectedDate: null, |
| 44 | + isCalendarVisible: false, |
| 45 | + currentYear: new Date().getFullYear(), |
| 46 | + currentMonth: new Date().getMonth(), |
| 47 | + selectedDate: this.initialDate, |
| 48 | + hoveredDate: null, |
13 | 49 | };
|
14 | 50 | },
|
| 51 | + computed: { |
| 52 | + formattedDate() { |
| 53 | + // Display the selected date directly without modification |
| 54 | + return this.hoveredDate || this.formatDate(this.incrementDate(this.selectedDate)); |
| 55 | + }, |
| 56 | + monthNames() { |
| 57 | + return [ |
| 58 | + 'January', 'February', 'March', 'April', 'May', 'June', |
| 59 | + 'July', 'August', 'September', 'October', 'November', 'December', |
| 60 | + ]; |
| 61 | + }, |
| 62 | + weekDays() { |
| 63 | + return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
| 64 | + }, |
| 65 | + displayedDates() { |
| 66 | + const dates = []; |
| 67 | + const firstDayOfMonth = new Date(this.currentYear, this.currentMonth, 1); |
| 68 | + const lastDayOfMonth = new Date(this.currentYear, this.currentMonth + 1, 0); |
| 69 | +
|
| 70 | + // Fill in the days leading up to the first day of the month |
| 71 | + for (let i = 0; i < firstDayOfMonth.getDay(); i++) { |
| 72 | + dates.push({ day: '', dateString: '' }); |
| 73 | + } |
| 74 | +
|
| 75 | + // Fill in the days of the month |
| 76 | + for (let day = 1; day <= lastDayOfMonth.getDate(); day++) { |
| 77 | + const dateString = new Date(this.currentYear, this.currentMonth, day).toISOString().split('T')[0]; |
| 78 | +
|
| 79 | + dates.push({ |
| 80 | + day, |
| 81 | + dateString, |
| 82 | + isSelected: dateString === this.selectedDate, |
| 83 | + isHover: dateString === this.hoveredDate, |
| 84 | + }); |
| 85 | + } |
| 86 | +
|
| 87 | + return dates; |
| 88 | + }, |
| 89 | + }, |
15 | 90 | methods: {
|
16 |
| - emitDateChange() { |
| 91 | + formatDate(date) { |
| 92 | + const options = { year: 'numeric', month: 'short', day: 'numeric' }; |
| 93 | + return new Date(date).toLocaleDateString(undefined, options); |
| 94 | + }, |
| 95 | + showCalendar() { |
| 96 | + this.isCalendarVisible = true; |
| 97 | + }, |
| 98 | + hoverDate(dateString) { |
| 99 | + this.hoveredDate = dateString; // Set the hovered date |
| 100 | + }, |
| 101 | + clearHover() { |
| 102 | + this.hoveredDate = null; // Clear the hover state |
| 103 | + }, |
| 104 | + selectDate(dateString) { |
| 105 | + this.selectedDate = dateString; // Set the selected date |
| 106 | + this.hoveredDate = null; // Clear hover state |
| 107 | + this.isCalendarVisible = false; // Hide calendar after selection |
| 108 | +
|
17 | 109 | this.$emit('date-selected', this.selectedDate); // Emit the selected date to the parent
|
18 |
| - } |
19 |
| - } |
| 110 | + this.$emit('update:initialDate', this.selectedDate); // Sync with the parent |
| 111 | + }, |
| 112 | + prevMonth() { |
| 113 | + if (this.currentMonth === 0) { |
| 114 | + this.currentMonth = 11; |
| 115 | + this.currentYear--; |
| 116 | + } else { |
| 117 | + this.currentMonth--; |
| 118 | + } |
| 119 | + }, |
| 120 | + nextMonth() { |
| 121 | + if (this.currentMonth === 11) { |
| 122 | + this.currentMonth = 0; |
| 123 | + this.currentYear++; |
| 124 | + } else { |
| 125 | + this.currentMonth++; |
| 126 | + } |
| 127 | + }, |
| 128 | + incrementDate(dateString) { |
| 129 | + const date = new Date(dateString); |
| 130 | + date.setDate(date.getDate()+1); // Increment the date by 1 |
| 131 | + return date.toISOString().split('T')[0]; // Return the incremented date as a string in YYYY-MM-DD format |
| 132 | + }, |
| 133 | + }, |
| 134 | + watch: { |
| 135 | + initialDate(newValue) { |
| 136 | + this.selectedDate = newValue; |
| 137 | + const date = new Date(newValue); |
| 138 | + this.currentYear = date.getFullYear(); |
| 139 | + this.currentMonth = date.getMonth(); |
| 140 | + }, |
| 141 | + }, |
| 142 | + mounted() { |
| 143 | + this.selectedDate = this.initialDate; // Set the initial selected date |
| 144 | + }, |
20 | 145 | };
|
21 | 146 | </script>
|
22 | 147 |
|
23 | 148 | <style scoped>
|
24 |
| -/* Add your styling here */ |
| 149 | +.date-picker { |
| 150 | + position: relative; |
| 151 | +} |
| 152 | +
|
| 153 | +.date-input { |
| 154 | + background-color: #1e1e1e; |
| 155 | + color: #cfcfcf; |
| 156 | + border: 1px solid #4a4a4a; |
| 157 | + border-radius: 4px; |
| 158 | + padding: 8px; |
| 159 | + width: 90%; |
| 160 | + cursor: text; |
| 161 | +} |
| 162 | +
|
| 163 | +.calendar { |
| 164 | + position: absolute; |
| 165 | + top: 40px; |
| 166 | + left: -10%; |
| 167 | + width: 90%; |
| 168 | + max-width: 300px; |
| 169 | + background-color: #2b2b2b; |
| 170 | + border: 1px solid #4a4a4a; |
| 171 | + border-radius: 4px; |
| 172 | + padding-left: 2px; |
| 173 | + padding-right: 25%; |
| 174 | + z-index: 1; |
| 175 | + box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3); |
| 176 | +} |
| 177 | +
|
| 178 | +.calendar-header { |
| 179 | + display: flex; |
| 180 | + justify-content: space-between; |
| 181 | + align-items: center; |
| 182 | + font-size: 1.0em; |
| 183 | +} |
| 184 | +
|
| 185 | +.calendar-grid { |
| 186 | + display: grid; |
| 187 | + grid-template-columns: repeat(7, 1fr); |
| 188 | + gap: 2px; |
| 189 | +} |
| 190 | +
|
| 191 | +.day { |
| 192 | + padding: 1px; |
| 193 | + text-align: center; |
| 194 | + cursor: pointer; |
| 195 | + transition: background-color 0.3s; |
| 196 | + min-height: 5%; |
| 197 | +} |
| 198 | +
|
| 199 | +.day.hover { |
| 200 | + background-color: rgba(0, 123, 255, 0.5); |
| 201 | +} |
| 202 | +
|
| 203 | +.day.selected { |
| 204 | + background-color: rgba(0, 86, 179, 0.7); |
| 205 | +} |
25 | 206 | </style>
|
0 commit comments