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.
85 lines
1.7 KiB
85 lines
1.7 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import dayjs from 'dayjs'
|
||
2 years ago
|
import { ReadonlyInj } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue: string | null | undefined
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
2 years ago
|
const emit = defineEmits(['update:modelValue'])
|
||
2 years ago
|
|
||
2 years ago
|
const { isMysql } = useProject()
|
||
|
|
||
2 years ago
|
const readOnly = inject(ReadonlyInj, false)
|
||
2 years ago
|
|
||
|
let isDateInvalid = $ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const dateFormat = isMysql ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'
|
||
|
|
||
|
const localState = $computed({
|
||
2 years ago
|
get() {
|
||
|
if (!modelValue) {
|
||
2 years ago
|
return undefined
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if (!dayjs(modelValue).isValid()) {
|
||
|
isDateInvalid = true
|
||
|
return undefined
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return /^\d+$/.test(modelValue) ? dayjs(+modelValue) : dayjs(modelValue)
|
||
2 years ago
|
},
|
||
2 years ago
|
set(val?: dayjs.Dayjs) {
|
||
|
if (!val) {
|
||
|
emit('update:modelValue', null)
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
if (val.isValid()) {
|
||
|
emit('update:modelValue', val?.format(dateFormat))
|
||
2 years ago
|
}
|
||
2 years ago
|
},
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
|
const open = ref(false)
|
||
|
|
||
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
|
</script>
|
||
|
|
||
2 years ago
|
<template>
|
||
2 years ago
|
<a-date-picker
|
||
|
v-model:value="localState"
|
||
|
:show-time="true"
|
||
|
:bordered="false"
|
||
|
class="!w-full px-1"
|
||
|
format="YYYY-MM-DD HH:mm"
|
||
2 years ago
|
:placeholder="isDateInvalid ? 'Invalid date' : ''"
|
||
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
|
:disabled="readOnly"
|
||
2 years ago
|
@click="open = !open"
|
||
2 years ago
|
@ok="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>
|