组件
<script setup lang="ts">
import type { DateValue } from '@internationalized/date'
import { DateFormatter, getLocalTimeZone, today } from '@internationalized/date'
import { CalendarIcon } from 'lucide-vue-next'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { Calendar } from '@/components/ui/calendar'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
const defaultPlaceholder = today(getLocalTimeZone())
const date = ref() as Ref<DateValue>
const df = new DateFormatter('en-US', {
dateStyle: 'long',
})
</script>
<template>
<Popover v-slot="{ close }">
<PopoverTrigger as-child>
<Button
variant="outline"
:class="cn('w-[240px] justify-start text-left font-normal', !date && 'text-muted-foreground')"
>
<CalendarIcon />
{{ date ? df.format(date.toDate(getLocalTimeZone())) : "Pick a date" }}
</Button>
</PopoverTrigger>
<PopoverContent class="w-auto p-0" align="start">
<Calendar
v-model="date"
:default-placeholder="defaultPlaceholder"
layout="month-and-year"
initial-focus
@update:model-value="close"
/>
</PopoverContent>
</Popover>
</template>安装
🌐 Installation
日期选择器是由 <Popover /> 和 <Calendar /> 组件组合构建的。
🌐 The Date Picker is built using a composition of the <Popover /> and the <Calendar /> components.
请参阅 Popover 和 Calendar 组件的安装说明。
🌐 See installation instructions for the Popover and the Calendar components.
用法
🌐 Usage
<script setup lang="ts">
import { DateFormatter, getLocalTimeZone, today } from '@internationalized/date'
import { CalendarIcon } from 'lucide-vue-next'
import { ref } from 'vue'
import { Button } from '@/components/ui/button'
import { Calendar } from '@/components/ui/calendar'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
import { cn } from '@/lib/utils'
const date = ref<Date>()
const defaultPlaceholder = today(getLocalTimeZone())
</script>
<template>
<Popover>
<PopoverTrigger as-child>
<Button
variant="outline"
:class="cn(
'w-[280px] justify-start text-left font-normal',
!date && 'text-muted-foreground',
)"
>
<CalendarIcon class="mr-2 h-4 w-4" />
{{ date ? date.toDateString() : "Pick a date" }}
</Button>
</PopoverTrigger>
<PopoverContent class="w-auto p-0">
<Calendar
v-model="date"
:initial-focus="true"
:default-placeholder="defaultPlaceholder"
layout="month-and-year"
/>
</PopoverContent>
</Popover>
</template>