Files
heroui-vue/apps/docs/demos/calendar-controlled.vue
T
2026-10-24 20:59:19 +08:00

67 lines
1.8 KiB
Vue

<script setup lang="ts">
import {
Calendar,
CalendarHeader,
CalendarHeading,
CalendarNavButton,
CalendarGrid,
CalendarGridHeader,
CalendarGridBody,
CalendarHeaderCell,
CalendarCell,
Button,
ButtonGroup,
Description,
} from "@heroui-vue/components";
import { shallowRef } from "vue";
import {
parseDate,
today,
getLocalTimeZone,
startOfMonth,
startOfWeek,
type DateValue,
} from "@internationalized/date";
const value = shallowRef<DateValue | null>(null);
const focusedDate = shallowRef<DateValue>(parseDate("2025-12-25"));
function jump(date: DateValue) {
value.value = date;
focusedDate.value = date;
}
</script>
<template>
<div class="flex flex-col items-center gap-4">
<ButtonGroup full-width size="sm" variant="tertiary">
<Button @click="jump(today(getLocalTimeZone()))">今天</Button>
<Button @click="jump(startOfWeek(today(getLocalTimeZone()), 'en-US'))">
本周
</Button>
<Button @click="jump(startOfMonth(today(getLocalTimeZone())))">
本月
</Button>
</ButtonGroup>
<Calendar
aria-label="活动日期"
v-model="value"
v-model:focused-value="focusedDate"
>
<CalendarHeader>
<CalendarHeading />
<CalendarNavButton slot="previous" />
<CalendarNavButton slot="next" />
</CalendarHeader>
<CalendarGrid>
<CalendarGridHeader v-slot="{ day }">
<CalendarHeaderCell>{{ day }}</CalendarHeaderCell>
</CalendarGridHeader>
<CalendarGridBody v-slot="{ date }">
<CalendarCell :date="date" />
</CalendarGridBody>
</CalendarGrid>
</Calendar>
<Description class="text-center">
{{ value ? `已选择:${value.toString()}` : "未选择日期" }}
</Description>
</div>
</template>