|
13 | 13 | let calorieInput: HTMLInputElement; |
14 | 14 | let showResetModal = false; |
15 | 15 | let resetSuccess = false; |
| 16 | + let dateInputValue = ''; |
16 | 17 | |
17 | 18 | let startX: number; |
18 | 19 | let endX: number; |
|
49 | 50 | updateDateQuery(newDate.toDateString()); |
50 | 51 | } |
51 | 52 |
|
| 53 | + /** |
| 54 | + * Handles the date picker input when the label is clicked. |
| 55 | + * @param event |
| 56 | + */ |
| 57 | + function handleDatePicker(event: MouseEvent) { |
| 58 | + const input = (event.target as HTMLLabelElement).nextElementSibling as HTMLInputElement; |
| 59 | + input.showPicker(); |
| 60 | + input.onchange = () => { |
| 61 | + if (input.value) { |
| 62 | + const [year, month, day] = input.value.split('-').map(Number); |
| 63 | + const selectedDate = new Date(year, month - 1, day); // month is 0-based |
| 64 | + if (!isNaN(selectedDate.valueOf())) { |
| 65 | + updateDateQuery(selectedDate.toDateString()); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | +
|
| 71 | + /** |
| 72 | + * Converts a date string in the format "Wed Aug 07 2024" to "YYYY-MM-DD". |
| 73 | + * @param dateStr |
| 74 | + */ |
| 75 | + function toISODateString(dateStr: string): string { |
| 76 | + const date = new Date(dateStr); |
| 77 | + if (isNaN(date.valueOf())) return ''; |
| 78 | + return date.toISOString().slice(0, 10); |
| 79 | + } |
| 80 | +
|
| 81 | + // Initialize dateInputValue with the current pathname in ISO format |
| 82 | + $: dateInputValue = toISODateString(pathname); |
| 83 | +
|
52 | 84 | function generateCalendarDates(currentDateString: string, calorieMap: Record<string, number | null>) { |
53 | 85 | const currentDate = new Date(currentDateString); |
54 | 86 | const year = currentDate.getFullYear(); |
|
220 | 252 | on:touchend={handleTouchEnd} |
221 | 253 | title="Swipe to navigate dates" |
222 | 254 | > |
223 | | - <span class="date-main">{formatCurrentDate(pathname)}</span> |
224 | | - <span class="date-weekday">{getWeekday(pathname)}</span> |
| 255 | + <label id="date-picker-label" for="date-picker" on:click={handleDatePicker}> |
| 256 | + <span class="date-main">{formatCurrentDate(pathname)}</span> |
| 257 | + <span class="date-weekday">{getWeekday(pathname)}</span> |
| 258 | + </label> |
| 259 | + <input type="date" id="date-picker" name="date-picker" bind:value={dateInputValue} style="display: none;"> |
225 | 260 | </h2> |
226 | 261 | </div> |
227 | 262 |
|
|
0 commit comments