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.
83 lines
1.8 KiB
83 lines
1.8 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import dayjs from 'dayjs'
|
||
2 years ago
|
import { ColumnInj, ReadonlyInj, computed, inject, ref, watch } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue?: string | null
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
2 years ago
|
const columnMeta = inject(ColumnInj, null)!
|
||
2 years ago
|
|
||
2 years ago
|
const readOnly = inject(ReadonlyInj, false)
|
||
2 years ago
|
|
||
2 years ago
|
let isDateInvalid = $ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const dateFormat = columnMeta?.value?.meta?.date_format ?? 'YYYY-MM-DD'
|
||
2 years ago
|
|
||
2 years ago
|
const localState = $computed({
|
||
2 years ago
|
get() {
|
||
2 years ago
|
if (!modelValue) {
|
||
2 years ago
|
return undefined
|
||
|
}
|
||
|
|
||
2 years ago
|
if (!dayjs(modelValue).isValid()) {
|
||
|
isDateInvalid = true
|
||
|
return undefined
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return /^\d+$/.test(modelValue) ? dayjs(+modelValue) : dayjs(modelValue)
|
||
2 years ago
|
},
|
||
2 years ago
|
set(val?: dayjs.Dayjs) {
|
||
2 years ago
|
if (!val) {
|
||
|
emit('update:modelValue', null)
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
if (val.isValid()) {
|
||
2 years ago
|
emit('update:modelValue', val?.format('YYYY-MM-DD'))
|
||
2 years ago
|
}
|
||
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(() => (isDateInvalid ? 'Invalid date' : ''))
|
||
2 years ago
|
</script>
|
||
|
|
||
2 years ago
|
<template>
|
||
2 years ago
|
<a-date-picker
|
||
|
v-model:value="localState"
|
||
|
:bordered="false"
|
||
|
class="!w-full px-1"
|
||
|
:format="dateFormat"
|
||
2 years ago
|
:placeholder="placeholder"
|
||
2 years ago
|
:allow-clear="!readOnly"
|
||
2 years ago
|
:input-read-only="true"
|
||
2 years ago
|
:dropdown-class-name="randomClass"
|
||
2 years ago
|
:open="readOnly ? false : open"
|
||
2 years ago
|
@click="open = !open"
|
||
2 years ago
|
>
|
||
2 years ago
|
<template #suffixIcon></template>
|
||
2 years ago
|
</a-date-picker>
|
||
2 years ago
|
</template>
|
||
|
|
||
2 years ago
|
<style scoped>
|
||
|
:deep(.ant-picker-input > input[disabled]) {
|
||
|
@apply !text-current;
|
||
|
}
|
||
|
</style>
|