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.
95 lines
2.0 KiB
95 lines
2.0 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import { onClickOutside } from '@vueuse/core'
|
||
2 years ago
|
import dayjs from 'dayjs'
|
||
|
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue?: string | null | undefined
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
|
const { isMysql } = useProject()
|
||
|
|
||
2 years ago
|
const readOnly = inject(ReadonlyInj, false)
|
||
2 years ago
|
|
||
|
let isTimeInvalid = $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({
|
||
|
get() {
|
||
|
if (!modelValue) {
|
||
|
return undefined
|
||
|
}
|
||
2 years ago
|
let dateTime = dayjs(modelValue)
|
||
2 years ago
|
|
||
2 years ago
|
if (!dateTime.isValid()) {
|
||
|
dateTime = dayjs(modelValue, 'HH:mm:ss')
|
||
|
}
|
||
|
if (!dateTime.isValid()) {
|
||
|
dateTime = dayjs(`1999-01-01 ${modelValue}`)
|
||
|
}
|
||
|
if (!dateTime.isValid()) {
|
||
2 years ago
|
isTimeInvalid = true
|
||
|
return undefined
|
||
|
}
|
||
|
|
||
2 years ago
|
return dateTime
|
||
2 years ago
|
},
|
||
|
set(val?: dayjs.Dayjs) {
|
||
|
if (!val) {
|
||
|
emit('update:modelValue', null)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if (val.isValid()) {
|
||
|
const time = val.format('HH:mm')
|
||
|
const date = dayjs(`1999-01-01 ${time}:00`)
|
||
|
emit('update:modelValue', date.format(dateFormat))
|
||
|
}
|
||
|
},
|
||
|
})
|
||
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>
|
||
|
|
||
|
<template>
|
||
|
<a-time-picker
|
||
|
v-model:value="localState"
|
||
|
autofocus
|
||
|
:show-time="true"
|
||
|
:bordered="false"
|
||
|
use12-hours
|
||
|
format="HH:mm"
|
||
|
class="!w-full px-1"
|
||
2 years ago
|
:placeholder="isTimeInvalid ? 'Invalid time' : ''"
|
||
2 years ago
|
:allow-clear="!readOnly"
|
||
2 years ago
|
:input-read-only="true"
|
||
2 years ago
|
:open="readOnly ? false : open"
|
||
2 years ago
|
:popup-class-name="randomClass"
|
||
2 years ago
|
@click="open = !open"
|
||
2 years ago
|
@ok="open = !open"
|
||
2 years ago
|
>
|
||
|
<template #suffixIcon></template>
|
||
|
</a-time-picker>
|
||
|
</template>
|
||
|
|
||
2 years ago
|
<style scoped>
|
||
|
:deep(.ant-picker-input > input[disabled]) {
|
||
|
@apply !text-current;
|
||
|
}
|
||
|
</style>
|