fix: 优化日期选择器状态同步与日历列表渲染
continuous-integration/drone/push Build is passing

This commit is contained in:
seahi committed 2026-08-14 18:05:12 +08:00
1 parent 0714b565da
commit 7bc1907e9d
1 file changed
+19 -14
+19 -14
View File
@@ -9,22 +9,25 @@
let isOpen = $state(false);
let displayDate = $state(new Date());
let currentMonth = $state(new Date().getMonth());
let currentYear = $state(new Date().getFullYear());
$effect(() => {
if (value) {
const parsed = new Date(value);
if (!isNaN(parsed.getTime())) {
displayDate = parsed;
if (isOpen) {
if (value) {
const parsed = new Date(value);
if (!isNaN(parsed.getTime())) {
currentMonth = parsed.getMonth();
currentYear = parsed.getFullYear();
}
} else {
const now = new Date();
currentMonth = now.getMonth();
currentYear = now.getFullYear();
}
} else {
displayDate = new Date();
}
});
let currentMonth = $state(displayDate.getMonth());
let currentYear = $state(displayDate.getFullYear());
let container: HTMLDivElement;
function handleOutsideClick(e: MouseEvent) {
@@ -66,6 +69,9 @@
let daysInMonth = $derived(new Date(currentYear, currentMonth + 1, 0).getDate());
let firstDayOfWeek = $derived(new Date(currentYear, currentMonth, 1).getDay()); // 0 is Sunday
let emptyDays = $derived(Array.from({ length: firstDayOfWeek }).map((v, i) => i));
let monthDays = $derived(Array.from({ length: daysInMonth }).map((v, i) => i + 1));
const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
</script>
@@ -100,16 +106,15 @@
</div>
<div class="calendar-grid">
{#each weekDays as wd}
{#each weekDays as wd (wd)}
<div class="weekday">{wd}</div>
{/each}
{#each Array(firstDayOfWeek) as _}
{#each emptyDays as i (i)}
<div class="empty-day"></div>
{/each}
{#each Array(daysInMonth) as _, i}
{@const day = i + 1}
{#each monthDays as day (day)}
{@const isSelected =
value ===
`${currentYear}-${String(currentMonth + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`}