Skip to content

Commit 656dca4

Browse files
committed
Merge branch 'Neroslike-feat-addDatePicker'
2 parents dead093 + 64c4170 commit 656dca4

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/routes/+page.svelte

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
let calorieInput: HTMLInputElement;
1414
let showResetModal = false;
1515
let resetSuccess = false;
16+
let dateInputValue = '';
1617
1718
let startX: number;
1819
let endX: number;
@@ -49,6 +50,37 @@
4950
updateDateQuery(newDate.toDateString());
5051
}
5152
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+
5284
function generateCalendarDates(currentDateString: string, calorieMap: Record<string, number | null>) {
5385
const currentDate = new Date(currentDateString);
5486
const year = currentDate.getFullYear();
@@ -220,8 +252,11 @@
220252
on:touchend={handleTouchEnd}
221253
title="Swipe to navigate dates"
222254
>
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;">
225260
</h2>
226261
</div>
227262

src/routes/styles.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,17 @@ h4 {
122122
section {
123123
margin: 20px 5px;
124124
}
125+
126+
#date-picker {
127+
width: 0;
128+
height: 0;
129+
outline: none;
130+
opacity: 0;
131+
border: none;
132+
}
133+
134+
#date-picker-label {
135+
display: flex;
136+
justify-content: center;
137+
align-items: center;
138+
}

0 commit comments

Comments
 (0)