feat: migrate public DatePicker with shared field and calendar owners
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Calendar,
|
||||
CalendarCell,
|
||||
CalendarGrid,
|
||||
CalendarGridBody,
|
||||
CalendarGridHeader,
|
||||
CalendarHeader,
|
||||
CalendarHeaderCell,
|
||||
CalendarNavButton,
|
||||
CalendarYearPickerCell,
|
||||
CalendarYearPickerGrid,
|
||||
CalendarYearPickerGridBody,
|
||||
CalendarYearPickerTrigger,
|
||||
CalendarYearPickerTriggerHeading,
|
||||
CalendarYearPickerTriggerIndicator,
|
||||
DateFieldGroup,
|
||||
DateFieldInput,
|
||||
DateFieldSegment,
|
||||
DateFieldSuffix,
|
||||
DatePicker,
|
||||
DatePickerPopover,
|
||||
DatePickerTrigger,
|
||||
DatePickerTriggerIndicator,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
Select,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
Switch,
|
||||
SwitchContent,
|
||||
SwitchControl,
|
||||
SwitchThumb,
|
||||
TimeField,
|
||||
TimeFieldGroup,
|
||||
TimeFieldInput,
|
||||
TimeFieldSegment,
|
||||
} from "@heroui-vue/components";
|
||||
import {
|
||||
getLocalTimeZone,
|
||||
parseDate,
|
||||
parseZonedDateTime,
|
||||
type DateValue,
|
||||
} from "@internationalized/date";
|
||||
import { computed, ref } from "vue";
|
||||
|
||||
type Granularity = "day" | "hour" | "minute" | "second";
|
||||
const granularity = ref<Granularity>("minute");
|
||||
const hourCycle = ref(12);
|
||||
const hideTimeZone = ref(false);
|
||||
const shouldForceLeadingZeros = ref(false);
|
||||
const cycle = computed(() => (hourCycle.value === 24 ? 24 : 12) as 12 | 24);
|
||||
const timeGranularity = computed(() =>
|
||||
granularity.value !== "day" ? granularity.value : undefined,
|
||||
);
|
||||
const showTimeField = computed(() => !!timeGranularity.value);
|
||||
const defaultValue = computed<DateValue>(() => {
|
||||
const localTimeZone = getLocalTimeZone();
|
||||
if (granularity.value === "day") return parseDate("2026-02-03");
|
||||
return parseZonedDateTime(`2026-02-03T08:45:00[${localTimeZone}]`);
|
||||
});
|
||||
const granularityOptions: { label: string; value: Granularity }[] = [
|
||||
{ label: "日", value: "day" },
|
||||
{ label: "时", value: "hour" },
|
||||
{ label: "分", value: "minute" },
|
||||
{ label: "秒", value: "second" },
|
||||
];
|
||||
const hourCycleOptions = [
|
||||
{ label: "12 小时制", value: 12 },
|
||||
{ label: "24 小时制", value: 24 },
|
||||
];
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex flex-col gap-4">
|
||||
<DatePicker
|
||||
:key="granularity"
|
||||
class="w-fit min-w-72"
|
||||
:default-value="defaultValue"
|
||||
:granularity="granularity"
|
||||
:hide-time-zone="hideTimeZone"
|
||||
:hour-cycle="cycle"
|
||||
name="date"
|
||||
:should-force-leading-zeros="shouldForceLeadingZeros"
|
||||
>
|
||||
<template #default="{ state }">
|
||||
<Label>日期和时间</Label>
|
||||
<DateFieldGroup full-width>
|
||||
<DateFieldInput v-slot="{ segment }">
|
||||
<DateFieldSegment :segment="segment" />
|
||||
</DateFieldInput>
|
||||
<DateFieldSuffix>
|
||||
<DatePickerTrigger>
|
||||
<DatePickerTriggerIndicator />
|
||||
</DatePickerTrigger>
|
||||
</DateFieldSuffix>
|
||||
</DateFieldGroup>
|
||||
<DatePickerPopover class="flex flex-col gap-3">
|
||||
<Calendar aria-label="活动日期">
|
||||
<CalendarHeader>
|
||||
<CalendarYearPickerTrigger>
|
||||
<CalendarYearPickerTriggerHeading />
|
||||
<CalendarYearPickerTriggerIndicator />
|
||||
</CalendarYearPickerTrigger>
|
||||
<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>
|
||||
<CalendarYearPickerGrid>
|
||||
<CalendarYearPickerGridBody v-slot="{ year, date }">
|
||||
<CalendarYearPickerCell :year="year" :date="date" />
|
||||
</CalendarYearPickerGridBody>
|
||||
</CalendarYearPickerGrid>
|
||||
</Calendar>
|
||||
<div v-if="showTimeField" class="flex items-center justify-between">
|
||||
<Label>时间</Label>
|
||||
<TimeField
|
||||
aria-label="时间"
|
||||
:granularity="timeGranularity"
|
||||
:hide-time-zone="hideTimeZone"
|
||||
:hour-cycle="cycle"
|
||||
name="time"
|
||||
:should-force-leading-zeros="shouldForceLeadingZeros"
|
||||
:value="state.timeValue"
|
||||
@change="state.setTimeValue($event)"
|
||||
>
|
||||
<TimeFieldGroup variant="secondary">
|
||||
<TimeFieldInput v-slot="{ segment }">
|
||||
<TimeFieldSegment :segment="segment" />
|
||||
</TimeFieldInput>
|
||||
</TimeFieldGroup>
|
||||
</TimeField>
|
||||
</div>
|
||||
</DatePickerPopover>
|
||||
</template>
|
||||
</DatePicker>
|
||||
|
||||
<div class="flex flex-wrap gap-4">
|
||||
<div class="flex flex-col gap-1">
|
||||
<Select
|
||||
class="w-[120px]"
|
||||
:model-value="granularity"
|
||||
variant="secondary"
|
||||
@update:model-value="granularity = $event as Granularity"
|
||||
>
|
||||
<Label>粒度</Label>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover>
|
||||
<ListBox>
|
||||
<ListBoxItem
|
||||
v-for="option in granularityOptions"
|
||||
:id="option.value"
|
||||
:key="option.value"
|
||||
:text-value="option.label"
|
||||
>
|
||||
{{ option.label }}
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</ListBox>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-1">
|
||||
<Select
|
||||
class="w-[120px]"
|
||||
:model-value="hourCycle"
|
||||
variant="secondary"
|
||||
@update:model-value="hourCycle = Number($event)"
|
||||
>
|
||||
<Label>小时制</Label>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover>
|
||||
<ListBox>
|
||||
<ListBoxItem
|
||||
v-for="option in hourCycleOptions"
|
||||
:id="option.value"
|
||||
:key="option.value"
|
||||
:text-value="option.label"
|
||||
>
|
||||
{{ option.label }}
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</ListBox>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex min-w-80 flex-col gap-2">
|
||||
<Switch v-model:checked="hideTimeZone">
|
||||
<SwitchContent>
|
||||
<SwitchControl><SwitchThumb /></SwitchControl>
|
||||
隐藏时区
|
||||
</SwitchContent>
|
||||
</Switch>
|
||||
<Switch v-model:checked="shouldForceLeadingZeros">
|
||||
<SwitchContent>
|
||||
<SwitchControl><SwitchThumb /></SwitchControl>
|
||||
强制前导零
|
||||
</SwitchContent>
|
||||
</Switch>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user