多维表格
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.

146 lines
4.2 KiB

<script lang="ts" setup>
import dayjs from 'dayjs'
interface Props {
7 months ago
selectedDate?: Date | null
isDisabled?: boolean
pageDate?: Date
yearPicker?: boolean
}
const props = withDefaults(defineProps<Props>(), {
selectedDate: null,
isDisabled: false,
pageDate: new Date(),
7 months ago
yearPicker: false,
})
const emit = defineEmits(['update:selectedDate', 'update:pageDate'])
7 months ago
const pageDate = useVModel(props, 'pageDate', emit)
const selectedDate = useVModel(props, 'selectedDate', emit)
const years = computed(() => {
const date = dayjs(pageDate.value)
const startOfYear = date.startOf('year')
const years: dayjs.Dayjs[] = []
for (let i = 0; i < 12; i++) {
years.push(dayjs(startOfYear).add(i, 'year'))
}
7 months ago
return years
})
const currentYear = computed(() => {
7 months ago
return pageDate.value.getFullYear()
})
const months = computed(() => {
const date = dayjs(pageDate.value)
const months: dayjs.Dayjs[] = []
for (let i = 0; i < 12; i++) {
months.push(date.set('month', i))
}
return months
})
const compareDates = (date1: dayjs.Dayjs, date2: dayjs.Dayjs) => {
7 months ago
if (!date1 || !date2) return false
return date1.isSame(date2, 'month') && date1.isSame(date2, 'year')
}
const isMonthSelected = (date: dayjs.Dayjs) => {
7 months ago
if (!selectedDate.value) return false
return compareDates(date, dayjs(selectedDate.value))
}
const paginateMonth = (action: 'next' | 'prev') => {
let date = dayjs(pageDate.value)
if (action === 'next') {
date = date.add(1, 'year')
} else {
date = date.subtract(1, 'year')
}
pageDate.value = date.toDate()
emit('update:pageDate', date.toDate())
7 months ago
}
const paginateYear = (action: 'next' | 'prev') => {
let date = dayjs(pageDate.value)
if (action === 'next') {
date = date.add(12, 'year').clone()
} else {
date = date.subtract(12, 'year').clone()
}
pageDate.value = date.toDate()
emit('update:pageDate', date.toDate())
7 months ago
}
const paginate = (action: 'next' | 'prev') => {
if (props.yearPicker) {
7 months ago
paginateYear(action)
} else {
7 months ago
paginateMonth(action)
}
7 months ago
}
const compareYear = (date1: dayjs.Dayjs, date2: dayjs.Dayjs) => {
7 months ago
if (!date1 || !date2) return false
return date1.isSame(date2, 'year')
}
</script>
<template>
<div class="p-4 flex flex-col gap-4">
<div class="flex justify-between items-center">
<NcTooltip>
<NcButton size="small" type="secondary" @click="paginate('prev')">
<component :is="iconMap.doubleLeftArrow" class="h-4 w-4" />
</NcButton>
<template #title>
<span>{{ $t('labels.previousYear') }}</span>
</template>
</NcTooltip>
<span class="font-bold text-gray-700">{{ yearPicker ? 'Select Year' : currentYear }}</span>
<NcTooltip>
<NcButton size="small" type="secondary" @click="paginate('next')">
<component :is="iconMap.doubleRightArrow" class="h-4 w-4" />
</NcButton>
<template #title>
<span>{{ $t('labels.nextYear') }}</span>
</template>
</NcTooltip>
</div>
<div class="border-1 border-gray-200 rounded-y-xl max-w-[350px]">
<div class="grid grid-cols-4 gap-2 p-2">
7 months ago
<template v-if="!yearPicker">
<span
v-for="(month, id) in months"
:key="id"
7 months ago
:class="{
'!bg-brand-50 !border-2 !border-brand-500': isMonthSelected(month),
7 months ago
}"
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.toDate()"
7 months ago
>
{{ month.format('MMM') }}
7 months ago
</span>
</template>
<template v-else>
<span
v-for="(year, id) in years"
:key="id"
7 months ago
:class="{
'!bg-brand-50 !border-2 !border-brand-500': compareYear(year, dayjs(selectedDate)),
7 months ago
}"
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.toDate()"
7 months ago
>
{{ year.format('YYYY') }}
7 months ago
</span>
</template>
</div>
</div>
</div>
</template>
7 months ago
<style lang="scss" scoped></style>