多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.8 KiB

<script lang="ts" setup>
interface Props {
9 months ago
selectedDate?: Date | null
isDisabled?: boolean
pageDate?: Date
yearPicker?: boolean
}
const props = withDefaults(defineProps<Props>(), {
selectedDate: null,
isDisabled: false,
pageDate: new Date(),
9 months ago
yearPicker: false,
})
const emit = defineEmits(['update:selected-date', 'update:page-date'])
const pageDate = useVModel(props, 'pageDate', emit)
const selectedDate = useVModel(props, 'selectedDate', emit)
const years = computed(() => {
9 months ago
const date = new Date(pageDate.value)
const startYear = date.getFullYear()
const endYear = date.getFullYear() + 11
const years = []
for (let i = startYear; i <= endYear; i++) {
years.push({
label: i,
9 months ago
value: new Date(i, 0, 1),
})
}
9 months ago
return years
})
const currentYear = computed(() => {
9 months ago
return pageDate.value.getFullYear()
})
const months = computed(() => {
9 months ago
const date = new Date(pageDate.value)
return [
{
label: 'January',
9 months ago
value: new Date(date.getFullYear(), 0, 1),
},
{
label: 'February',
9 months ago
value: new Date(date.getFullYear(), 1, 1),
},
{
label: 'March',
9 months ago
value: new Date(date.getFullYear(), 2, 1),
},
{
label: 'April',
9 months ago
value: new Date(date.getFullYear(), 3, 1),
},
{
label: 'May',
9 months ago
value: new Date(date.getFullYear(), 4, 1),
},
{
label: 'June',
9 months ago
value: new Date(date.getFullYear(), 5, 1),
},
{
label: 'July',
9 months ago
value: new Date(date.getFullYear(), 6, 1),
},
{
label: 'August',
9 months ago
value: new Date(date.getFullYear(), 7, 1),
},
{
label: 'September',
9 months ago
value: new Date(date.getFullYear(), 8, 1),
},
{
label: 'October',
9 months ago
value: new Date(date.getFullYear(), 9, 1),
},
{
label: 'November',
9 months ago
value: new Date(date.getFullYear(), 10, 1),
},
{
label: 'December',
9 months ago
value: new Date(date.getFullYear(), 11, 1),
},
]
})
const compareDates = (date1: Date, date2: Date) => {
9 months ago
if (!date1 || !date2) return false
return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth()
}
const isMonthSelected = (date: Date) => {
9 months ago
if (!selectedDate.value) return false
return compareDates(date, selectedDate.value)
}
const paginateMonth = (action: 'next' | 'prev') => {
9 months ago
const date = new Date(pageDate.value)
if (action === 'next') {
9 months ago
date.setFullYear(date.getFullYear() + 1)
} else {
9 months ago
date.setFullYear(date.getFullYear() - 1)
}
9 months ago
pageDate.value = date
}
const paginateYear = (action: 'next' | 'prev') => {
9 months ago
const date = new Date(pageDate.value)
if (action === 'next') {
9 months ago
date.setFullYear(date.getFullYear() + 12)
} else {
9 months ago
date.setFullYear(date.getFullYear() - 12)
}
9 months ago
pageDate.value = date
}
const paginate = (action: 'next' | 'prev') => {
if (props.yearPicker) {
9 months ago
paginateYear(action)
} else {
9 months ago
paginateMonth(action)
}
9 months ago
}
const compareYear = (date1: Date, date2: Date) => {
9 months ago
if (!date1 || !date2) return false
return date1.getFullYear() === date2.getFullYear()
}
</script>
<template>
<div class="p-4 flex flex-col gap-4">
<div class="flex justify-between items-center">
<NcButton size="small" type="secondary" @click="paginate('prev')">
9 months ago
<component :is="iconMap.doubleLeftArrow" class="h-4 w-4" />
</NcButton>
<span class="font-bold text-gray-700">{{ yearPicker ? 'Select Year' : currentYear }}</span>
<NcButton size="small" type="secondary" @click="paginate('next')">
9 months ago
<component :is="iconMap.doubleRightArrow" class="h-4 w-4" />
</NcButton>
</div>
<div class="border-1 border-gray-200 rounded-y-xl max-w-[350px]">
<div class="grid grid-cols-4 gap-2 p-2">
9 months ago
<template v-if="!yearPicker">
<span
v-for="month in months"
:key="month.value"
:class="{
'!bg-brand-50 !border-2 !border-brand-500': isMonthSelected(month.value),
}"
class="h-9 rounded-lg flex font-medium items-center justify-center hover:(border-1 border-gray-200 bg-gray-100) text-gray-500 cursor-pointer"
@click="selectedDate = month.value"
>
{{ month.label.slice(0, 3) }}
</span>
</template>
<template v-else>
<span
v-for="year in years"
:key="year"
:class="{
'!bg-brand-50 !border-2 !border-brand-500': compareYear(year.value, selectedDate),
}"
class="h-9 rounded-lg flex font-medium items-center justify-center hover:(border-1 border-gray-200 bg-gray-100) text-gray-500 cursor-pointer"
@click="selectedDate = year.value"
>
{{ year.label }}
</span>
</template>
</div>
</div>
</div>
</template>
9 months ago
<style lang="scss" scoped></style>