feat: migrate public DateRangePicker with shared range field and calendar owners
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
<script setup lang="ts">
|
||||
// Adapted from HeroUI ce90ea3 date-range-picker/format-options.tsx, Apache-2.0.
|
||||
import {
|
||||
CalendarYearPickerCell,
|
||||
CalendarYearPickerGrid,
|
||||
CalendarYearPickerGridBody,
|
||||
CalendarYearPickerTrigger,
|
||||
CalendarYearPickerTriggerHeading,
|
||||
CalendarYearPickerTriggerIndicator,
|
||||
DateFieldGroup,
|
||||
DateFieldInput,
|
||||
DateFieldInputContainer,
|
||||
DateFieldSegment,
|
||||
DateFieldSuffix,
|
||||
DateRangePicker,
|
||||
DateRangePickerPopover,
|
||||
DateRangePickerRangeSeparator,
|
||||
DateRangePickerTrigger,
|
||||
DateRangePickerTriggerIndicator,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
RangeCalendar,
|
||||
RangeCalendarCell,
|
||||
RangeCalendarGrid,
|
||||
RangeCalendarGridBody,
|
||||
RangeCalendarGridHeader,
|
||||
RangeCalendarHeader,
|
||||
RangeCalendarHeaderCell,
|
||||
RangeCalendarNavButton,
|
||||
Select,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
Separator,
|
||||
Switch,
|
||||
SwitchContent,
|
||||
SwitchControl,
|
||||
SwitchThumb,
|
||||
TimeField,
|
||||
TimeFieldGroup,
|
||||
TimeFieldInput,
|
||||
TimeFieldSegment,
|
||||
} from "@heroui-vue/components";
|
||||
import {
|
||||
DateFormatter,
|
||||
getLocalTimeZone,
|
||||
parseDate,
|
||||
parseZonedDateTime,
|
||||
type DateValue,
|
||||
} from "@internationalized/date";
|
||||
import { computed, ref } from "vue";
|
||||
|
||||
type Granularity = "day" | "hour" | "minute" | "second";
|
||||
type HourCycle = 12 | 24;
|
||||
type DateRange = { start: DateValue; end: DateValue };
|
||||
|
||||
const granularity = ref<Granularity>("minute");
|
||||
const hourCycle = ref<HourCycle>(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<DateRange>(() => {
|
||||
const localTimeZone = getLocalTimeZone();
|
||||
if (granularity.value === "day")
|
||||
return {
|
||||
end: parseDate("2025-02-10"),
|
||||
start: parseDate("2025-02-03"),
|
||||
};
|
||||
return {
|
||||
end: parseZonedDateTime(`2026-02-10T18:45:00[${localTimeZone}]`),
|
||||
start: parseZonedDateTime(`2026-02-03T08:45:00[${localTimeZone}]`),
|
||||
};
|
||||
});
|
||||
const dateFormatter = new DateFormatter("zh-CN", {
|
||||
day: "numeric",
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
});
|
||||
function formatDate(range: DateRange) {
|
||||
const localTimeZone = getLocalTimeZone();
|
||||
return dateFormatter.formatRange(
|
||||
range.start.toDate(localTimeZone),
|
||||
range.end.toDate(localTimeZone),
|
||||
);
|
||||
}
|
||||
const granularityOptions: { label: string; value: Granularity }[] = [
|
||||
{ label: "日", value: "day" },
|
||||
{ label: "时", value: "hour" },
|
||||
{ label: "分", value: "minute" },
|
||||
{ label: "秒", value: "second" },
|
||||
];
|
||||
const hourCycleOptions: { label: string; value: HourCycle }[] = [
|
||||
{ label: "12 小时制", value: 12 },
|
||||
{ label: "24 小时制", value: 24 },
|
||||
];
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex w-full flex-col gap-4">
|
||||
<DateRangePicker
|
||||
:key="granularity"
|
||||
class="w-max min-w-80"
|
||||
:default-value="defaultValue"
|
||||
end-name="endDate"
|
||||
:granularity="granularity"
|
||||
:hide-time-zone="hideTimeZone"
|
||||
:hour-cycle="cycle"
|
||||
:should-force-leading-zeros="shouldForceLeadingZeros"
|
||||
start-name="startDate"
|
||||
>
|
||||
<template #default="{ state }">
|
||||
<Label>日期范围</Label>
|
||||
<DateFieldGroup>
|
||||
<DateFieldInputContainer>
|
||||
<DateFieldInput endpoint="start" v-slot="{ segment }">
|
||||
<DateFieldSegment :segment="segment" />
|
||||
</DateFieldInput>
|
||||
<DateRangePickerRangeSeparator />
|
||||
<DateFieldInput endpoint="end" v-slot="{ segment }">
|
||||
<DateFieldSegment :segment="segment" />
|
||||
</DateFieldInput>
|
||||
</DateFieldInputContainer>
|
||||
<DateFieldSuffix>
|
||||
<DateRangePickerTrigger>
|
||||
<DateRangePickerTriggerIndicator />
|
||||
</DateRangePickerTrigger>
|
||||
</DateFieldSuffix>
|
||||
</DateFieldGroup>
|
||||
<!-- Portaled content keeps an auto-sized wrapper; the pinned example's
|
||||
w-full maps to the 252px calendar plus the popover's 24px padding. -->
|
||||
<DateRangePickerPopover class="flex w-[276px] flex-col gap-3">
|
||||
<RangeCalendar aria-label="出行日期" class="w-full">
|
||||
<RangeCalendarHeader>
|
||||
<CalendarYearPickerTrigger>
|
||||
<CalendarYearPickerTriggerHeading />
|
||||
<CalendarYearPickerTriggerIndicator />
|
||||
</CalendarYearPickerTrigger>
|
||||
<RangeCalendarNavButton slot="previous" />
|
||||
<RangeCalendarNavButton slot="next" />
|
||||
</RangeCalendarHeader>
|
||||
<RangeCalendarGrid>
|
||||
<RangeCalendarGridHeader v-slot="{ day }">
|
||||
<RangeCalendarHeaderCell>{{ day }}</RangeCalendarHeaderCell>
|
||||
</RangeCalendarGridHeader>
|
||||
<RangeCalendarGridBody v-slot="{ date }">
|
||||
<RangeCalendarCell :date="date" />
|
||||
</RangeCalendarGridBody>
|
||||
</RangeCalendarGrid>
|
||||
<CalendarYearPickerGrid>
|
||||
<CalendarYearPickerGridBody v-slot="{ year, date }">
|
||||
<CalendarYearPickerCell :year="year" :date="date" />
|
||||
</CalendarYearPickerGridBody>
|
||||
</CalendarYearPickerGrid>
|
||||
</RangeCalendar>
|
||||
<div v-if="showTimeField" class="flex flex-col gap-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<Label>开始时间</Label>
|
||||
<TimeField
|
||||
aria-label="开始时间"
|
||||
:granularity="timeGranularity"
|
||||
:hide-time-zone="hideTimeZone"
|
||||
:hour-cycle="cycle"
|
||||
name="startTime"
|
||||
:should-force-leading-zeros="shouldForceLeadingZeros"
|
||||
:value="state.timeRange?.start ?? null"
|
||||
@change="
|
||||
state.setTimeRange({
|
||||
end: state.timeRange?.end ?? null,
|
||||
start: $event,
|
||||
})
|
||||
"
|
||||
>
|
||||
<TimeFieldGroup variant="secondary">
|
||||
<TimeFieldInput v-slot="{ segment }">
|
||||
<TimeFieldSegment :segment="segment" />
|
||||
</TimeFieldInput>
|
||||
</TimeFieldGroup>
|
||||
</TimeField>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<Label>结束时间</Label>
|
||||
<TimeField
|
||||
aria-label="结束时间"
|
||||
:granularity="timeGranularity"
|
||||
:hide-time-zone="hideTimeZone"
|
||||
:hour-cycle="cycle"
|
||||
name="endTime"
|
||||
:should-force-leading-zeros="shouldForceLeadingZeros"
|
||||
:value="state.timeRange?.end ?? null"
|
||||
@change="
|
||||
state.setTimeRange({
|
||||
end: $event,
|
||||
start: state.timeRange?.start ?? null,
|
||||
})
|
||||
"
|
||||
>
|
||||
<TimeFieldGroup variant="secondary">
|
||||
<TimeFieldInput v-slot="{ segment }">
|
||||
<TimeFieldSegment :segment="segment" />
|
||||
</TimeFieldInput>
|
||||
</TimeFieldGroup>
|
||||
</TimeField>
|
||||
</div>
|
||||
</div>
|
||||
<span class="mt-1 text-xs text-muted">
|
||||
已选:
|
||||
{{
|
||||
state.value && state.value.start && state.value.end
|
||||
? formatDate({ end: state.value.end, start: state.value.start })
|
||||
: "未选择日期"
|
||||
}}
|
||||
</span>
|
||||
</DateRangePickerPopover>
|
||||
</template>
|
||||
</DateRangePicker>
|
||||
|
||||
<Separator class="my-5" />
|
||||
|
||||
<Label class="text-xs font-medium text-muted">格式选项</Label>
|
||||
|
||||
<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) as HourCycle"
|
||||
>
|
||||
<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-[529px] 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