71 lines
1.9 KiB
Vue
71 lines
1.9 KiB
Vue
<script setup lang="ts">
|
|
import {
|
|
DateField,
|
|
DateFieldGroup,
|
|
DateFieldInput,
|
|
DateFieldSegment,
|
|
DateFieldPrefix,
|
|
Label,
|
|
Description,
|
|
FieldError,
|
|
Form,
|
|
Button,
|
|
} from "@heroui-vue/components";
|
|
import { IconCalendar } from "@tabler/icons-vue";
|
|
import { shallowRef, computed, onBeforeUnmount } from "vue";
|
|
import {
|
|
today,
|
|
getLocalTimeZone,
|
|
type DateValue,
|
|
} from "@internationalized/date";
|
|
const value = shallowRef<DateValue | null>(null);
|
|
const todayDate = today(getLocalTimeZone());
|
|
const isInvalid = computed(
|
|
() => value.value !== null && value.value.compare(todayDate) < 0,
|
|
);
|
|
const isSubmitting = shallowRef(false);
|
|
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;
|
|
}, 1500);
|
|
}
|
|
onBeforeUnmount(() => clearTimeout(timer));
|
|
</script>
|
|
<template>
|
|
<Form class="flex w-[280px] flex-col gap-4" @submit.prevent="submit">
|
|
<DateField
|
|
v-model="value"
|
|
is-required
|
|
:is-invalid="isInvalid"
|
|
:min-value="todayDate"
|
|
class="w-full"
|
|
name="date"
|
|
>
|
|
<Label>预约日期</Label>
|
|
<DateFieldGroup>
|
|
<DateFieldPrefix>
|
|
<IconCalendar class="size-4 text-muted" />
|
|
</DateFieldPrefix>
|
|
<DateFieldInput v-slot="{ segment }">
|
|
<DateFieldSegment :segment="segment" />
|
|
</DateFieldInput>
|
|
</DateFieldGroup>
|
|
<FieldError v-if="isInvalid">日期须为今天或将来</FieldError>
|
|
<Description v-else>输入日期 from today onwards</Description>
|
|
</DateField>
|
|
<Button
|
|
class="w-full"
|
|
:is-disabled="!value || isInvalid"
|
|
:is-pending="isSubmitting"
|
|
type="submit"
|
|
variant="primary"
|
|
>
|
|
{{ isSubmitting ? "提交中…" : "Submit" }}
|
|
</Button>
|
|
</Form>
|
|
</template>
|