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.
163 lines
3.5 KiB
163 lines
3.5 KiB
<script setup lang="ts"> |
|
import dayjs from 'dayjs' |
|
import { |
|
ActiveCellInj, |
|
EditColumnInj, |
|
IsFormInj, |
|
IsSurveyFormInj, |
|
ReadonlyInj, |
|
computed, |
|
inject, |
|
onClickOutside, |
|
ref, |
|
useSelectedCellKeyupListener, |
|
watch, |
|
} from '#imports' |
|
|
|
interface Props { |
|
modelValue?: number | string | null |
|
isPk?: boolean |
|
} |
|
|
|
const { modelValue, isPk = false } = defineProps<Props>() |
|
|
|
const emit = defineEmits(['update:modelValue']) |
|
|
|
const { showNull } = useGlobal() |
|
|
|
const readOnly = inject(ReadonlyInj, ref(false)) |
|
|
|
const active = inject(ActiveCellInj, ref(false)) |
|
|
|
const editable = inject(EditModeInj, ref(false)) |
|
|
|
const isEditColumn = inject(EditColumnInj, ref(false)) |
|
|
|
const isForm = inject(IsFormInj, ref(false)) |
|
|
|
const isSurveyForm = inject(IsSurveyFormInj, ref(false)) |
|
|
|
const isYearInvalid = ref(false) |
|
|
|
const { t } = useI18n() |
|
|
|
const localState = computed({ |
|
get() { |
|
if (!modelValue) { |
|
return undefined |
|
} |
|
|
|
const yearDate = dayjs(modelValue.toString(), 'YYYY') |
|
if (!yearDate.isValid()) { |
|
isYearInvalid.value = true |
|
return undefined |
|
} |
|
|
|
return yearDate |
|
}, |
|
set(val?: dayjs.Dayjs) { |
|
if (!val) { |
|
emit('update:modelValue', null) |
|
return |
|
} |
|
|
|
if (val?.isValid()) { |
|
emit('update:modelValue', val.format('YYYY')) |
|
} |
|
}, |
|
}) |
|
|
|
const open = ref<boolean>(false) |
|
|
|
const randomClass = `picker_${Math.floor(Math.random() * 99999)}` |
|
watch( |
|
open, |
|
(next) => { |
|
if (next) { |
|
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false)) |
|
} else { |
|
editable.value = false |
|
} |
|
}, |
|
{ flush: 'post' }, |
|
) |
|
|
|
const placeholder = computed(() => { |
|
if (isForm.value && !isYearInvalid.value) { |
|
return 'YYYY' |
|
} else if (isEditColumn.value && (modelValue === '' || modelValue === null)) { |
|
return t('labels.optional') |
|
} else if (modelValue === null && showNull.value) { |
|
return t('general.null').toUpperCase() |
|
} else if (isYearInvalid.value) { |
|
return t('msg.invalidTime') |
|
} else { |
|
return '' |
|
} |
|
}) |
|
|
|
const isOpen = computed(() => { |
|
if (readOnly.value) return false |
|
|
|
return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value |
|
}) |
|
|
|
const handleKeydown = (e: KeyboardEvent) => { |
|
switch (e.key) { |
|
case ' ': |
|
if (isSurveyForm.value) { |
|
open.value = !open.value |
|
} |
|
break |
|
|
|
case 'Enter': |
|
if (!isSurveyForm.value) { |
|
open.value = !open.value |
|
} |
|
break |
|
} |
|
} |
|
|
|
useSelectedCellKeyupListener(active, (e: KeyboardEvent) => { |
|
switch (e.key) { |
|
case 'Enter': |
|
e.stopPropagation() |
|
open.value = true |
|
break |
|
case 'Escape': |
|
if (open.value) { |
|
e.stopPropagation() |
|
open.value = false |
|
} |
|
break |
|
} |
|
}) |
|
</script> |
|
|
|
<template> |
|
<a-date-picker |
|
v-model:value="localState" |
|
:tabindex="0" |
|
picker="year" |
|
:bordered="false" |
|
class="nc-cell-field !w-full !py-1 !border-none !text-current" |
|
:class="{ 'nc-null': modelValue === null && showNull }" |
|
:placeholder="placeholder" |
|
:allow-clear="(!readOnly && !localState && !isPk) || isEditColumn" |
|
:input-read-only="true" |
|
:open="isOpen" |
|
:dropdown-class-name="`${randomClass} nc-picker-year children:border-1 children:border-gray-200 ${open ? 'active' : ''}`" |
|
@keydown="handleKeydown" |
|
@click="open = (active || editable) && !open" |
|
@change="open = (active || editable) && !open" |
|
@ok="open = !open" |
|
> |
|
<template #suffixIcon></template> |
|
</a-date-picker> |
|
</template> |
|
|
|
<style scoped> |
|
:deep(.ant-picker-input > input) { |
|
@apply !text-current; |
|
} |
|
</style>
|
|
|