Files
heroui-vue/apps/docs/demos/date-picker-form-example.vue
T

112 lines
3.3 KiB
Vue

<script setup lang="ts">
import {
Button,
Calendar,
CalendarCell,
CalendarGrid,
CalendarGridBody,
CalendarGridHeader,
CalendarHeader,
CalendarHeaderCell,
CalendarNavButton,
CalendarYearPickerCell,
CalendarYearPickerGrid,
CalendarYearPickerGridBody,
CalendarYearPickerTrigger,
CalendarYearPickerTriggerHeading,
CalendarYearPickerTriggerIndicator,
DateFieldGroup,
DateFieldInput,
DateFieldSegment,
DateFieldSuffix,
DatePicker,
DatePickerPopover,
DatePickerTrigger,
DatePickerTriggerIndicator,
Description,
FieldError,
Form,
Label,
} from "@heroui-vue/components";
import {
getLocalTimeZone,
today,
type DateValue,
} from "@internationalized/date";
import { computed, onBeforeUnmount, shallowRef } from "vue";
const value = shallowRef<DateValue | null>(null);
const isSubmitting = shallowRef(false);
const currentDate = today(getLocalTimeZone());
const isInvalid = computed(
() => value.value != null && value.value.compare(currentDate) < 0,
);
let timer: ReturnType<typeof setTimeout> | undefined;
function submit() {
if (!value.value || isInvalid.value || isSubmitting.value) return;
isSubmitting.value = true;
timer = setTimeout(() => {
value.value = null;
isSubmitting.value = false;
}, 1200);
}
onBeforeUnmount(() => clearTimeout(timer));
</script>
<template>
<Form class="flex w-72 flex-col gap-3" @submit.prevent="submit">
<DatePicker
v-model="value"
is-required
:is-invalid="isInvalid"
:min-value="currentDate"
name="appointmentDate"
>
<Label>预约日期</Label>
<DateFieldGroup full-width>
<DateFieldInput v-slot="{ segment }">
<DateFieldSegment :segment="segment" />
</DateFieldInput>
<DateFieldSuffix>
<DatePickerTrigger>
<DatePickerTriggerIndicator />
</DatePickerTrigger>
</DateFieldSuffix>
</DateFieldGroup>
<FieldError v-if="isInvalid">日期须为今天或将来。</FieldError>
<Description v-else>请选择有效的预约日期。</Description>
<DatePickerPopover>
<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>
</DatePickerPopover>
</DatePicker>
<Button
class="w-full"
:is-disabled="!value || isInvalid"
:is-pending="isSubmitting"
type="submit"
>
{{ isSubmitting ? "提交中…" : "提交" }}
</Button>
</Form>
</template>