mirror of https://github.com/nocodb/nocodb
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.
82 lines
1.7 KiB
82 lines
1.7 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import dayjs from 'dayjs'
|
||
2 years ago
|
import { ReadonlyInj, computed, inject, onClickOutside, ref, watch } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue?: number | string | null
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
2 years ago
|
const readOnly = inject(ReadonlyInj, false)
|
||
2 years ago
|
|
||
|
let isYearInvalid = $ref(false)
|
||
|
|
||
|
const localState = $computed({
|
||
|
get() {
|
||
|
if (!modelValue) {
|
||
|
return undefined
|
||
|
}
|
||
|
|
||
|
const yearDate = dayjs(modelValue.toString(), 'YYYY')
|
||
|
if (!yearDate.isValid()) {
|
||
|
isYearInvalid = true
|
||
|
return undefined
|
||
|
}
|
||
|
|
||
|
return yearDate
|
||
|
},
|
||
|
set(val?: dayjs.Dayjs) {
|
||
|
if (!val) {
|
||
|
emit('update:modelValue', null)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if (val?.isValid()) {
|
||
2 years ago
|
emit('update:modelValue', Number(val.format('YYYY')))
|
||
2 years ago
|
}
|
||
|
},
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const open = ref<boolean>(false)
|
||
2 years ago
|
|
||
2 years ago
|
const randomClass = `picker_${Math.floor(Math.random() * 99999)}`
|
||
2 years ago
|
watch(
|
||
|
open,
|
||
|
(next) => {
|
||
|
if (next) {
|
||
2 years ago
|
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false))
|
||
2 years ago
|
}
|
||
|
},
|
||
|
{ flush: 'post' },
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const placeholder = computed(() => (isYearInvalid ? 'Invalid year' : ''))
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<a-date-picker
|
||
|
v-model:value="localState"
|
||
|
picker="year"
|
||
|
:bordered="false"
|
||
|
class="!w-full px-1"
|
||
2 years ago
|
:placeholder="placeholder"
|
||
2 years ago
|
:allow-clear="!readOnly"
|
||
2 years ago
|
:input-read-only="true"
|
||
2 years ago
|
:open="readOnly ? false : open"
|
||
2 years ago
|
:dropdown-class-name="randomClass"
|
||
2 years ago
|
@click="open = !open"
|
||
|
@change="open = !open"
|
||
2 years ago
|
>
|
||
2 years ago
|
<template #suffixIcon></template>
|
||
2 years ago
|
</a-date-picker>
|
||
|
</template>
|
||
2 years ago
|
|
||
|
<style scoped>
|
||
|
:deep(.ant-picker-input > input[disabled]) {
|
||
|
@apply !text-current;
|
||
|
}
|
||
|
</style>
|