Files
heroui-vue/apps/docs/demos/date-range-picker-format-options.vue
T
maoyingjie 2e3fdd4800 fix(demos): inherit ConfigProvider locale and restore the format-options popover width
HD-03 scope, all against heroui ce90ea3 cn sources:

- calendar-disabled/read-only: restore the today() default value and the
  baseline description lines (日历已禁用 / 日历为只读) inside the
  flex flex-col items-center wrapper
- calendar-controlled: restore the second button row (设为今天/设为圣诞节/
  清空) and the baseline feedback wording (已选日期:…(未选))
- locale inheritance: add a public useLocale() accessor (the components'
  ConfigProvider → en-US/ltr chain) and use it for startOfWeek in
  calendar-controlled, isWeekend in booking-calendar/unavailable-dates and
  the DateFormatter in date-range-picker-format-options instead of the
  hardcoded 'en-US' / 'zh-CN' literals
- date-range-picker-format-options: restore the pinned w-full popover class;
  the pinned source positions the popover against the initial containing
  block (full viewport width — verified on the reference docs at desktop and
  narrow), while Reka's shrink-to-fit positioning wrapper collapses the same
  class. A scoped adapter rule (.hv-date-range-picker__popover.w-full →
  100vw, layering-safe like the dropdown static-wrapper adapter) restores
  the baseline effect: viewport-wide popover, 252px calendar, stretched
  time rows, identical boundary clamping

- tests: locale dynamic-switching regressions covering zh-CN/en-US for week
  start, weekend rules (with ar-EG flipping the weekend for a observable
  rule change) and range formatting; full suite 110 files / 1627 tests,
  typecheck, build:docs and the date-range-picker / api-root-aliases /
  calendar tarball contracts pass
2026-10-28 16:35:26 +08:00

308 lines
9.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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,
useLocale,
} 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 { locale } = useLocale();
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 = computed(
() =>
new DateFormatter(locale.value, {
day: "numeric",
month: "short",
year: "numeric",
}),
);
function formatDate(range: DateRange) {
const localTimeZone = getLocalTimeZone();
return dateFormatter.value.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 fills the popover's positioning container — see the
.hv-date-range-picker__popover.w-full adapter in date-range-picker.css,
which maps it to the same viewport width as the upstream chain. -->
<DateRangePickerPopover class="flex w-full 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>