Browse Source

Nc feat/manually feed value for date time related fields (#8015)

* fix(nc-gui): allow manual feed value for date & dateTime field

* fix(nc-gui): add support to manually enter datetime field value

* fix(nc-gui): allow user to type date time field manually

* fix(nc-gui): on type in date time field should open date picker and fill value

* feat(nc-gui): allow user to manually enter date field value

* fix(nc-gui): allow user to manually enter year field value

* feat(nc-gui): allow user to manually feed time field value

* fix(nc-gui): pw test fail issue

* fix(nc-gui): pw test fail issue of time field

* chore(nc-gui): lint

* fix(nc-gui): on type in selected grid cell clear prev value and feed new value

* fix(nc-gui): on press enter date field should focus

* fix(nc-gui): small changes

* fix(nc-gui): year field cell value should be left aligned

* fix(nc-gui): cell padding issue

* fix(nc-gui): Use of escape any number of times should not make selected cell go away in grid view.

* fix(nc-gui): hide clear default value icon in edit column modal

* fix(nc-gui): on enter key press in date time related field it should select next cell field

* fix(nc-gui): date time related field keydown listener issue

* fix(nc-gui): disable manually enter value for date time related field in mobile device

* fix(nc-gui): revert use of escape any number of times should not make selected cell go away

---------

Co-authored-by: Raju Udava <86527202+dstala@users.noreply.github.com>
pull/8190/head
Ramesh Mane 8 months ago committed by GitHub
parent
commit
9b7e5a2911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 230
      packages/nc-gui/components/cell/DatePicker.vue
  2. 253
      packages/nc-gui/components/cell/DateTimePicker.vue
  3. 148
      packages/nc-gui/components/cell/TimePicker.vue
  4. 155
      packages/nc-gui/components/cell/YearPicker.vue
  5. 14
      packages/nc-gui/components/smartsheet/Cell.vue
  6. 2
      packages/nc-gui/components/smartsheet/grid/Table.vue
  7. 2
      tests/playwright/pages/Dashboard/common/Cell/TimeCell.ts

230
packages/nc-gui/components/cell/DatePicker.vue

@ -1,17 +1,15 @@
<script setup lang="ts"> <script setup lang="ts">
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { isDateMonthFormat } from 'nocodb-sdk' import { isDateMonthFormat, isSystemColumn } from 'nocodb-sdk'
import { import {
ActiveCellInj, ActiveCellInj,
CellClickHookInj, CellClickHookInj,
ColumnInj, ColumnInj,
EditColumnInj, EditColumnInj,
EditModeInj, EditModeInj,
IsSurveyFormInj,
ReadonlyInj, ReadonlyInj,
computed, computed,
inject, inject,
isDrawerOrModalExist,
onClickOutside, onClickOutside,
onMounted, onMounted,
onUnmounted, onUnmounted,
@ -19,7 +17,6 @@ import {
ref, ref,
useGlobal, useGlobal,
useI18n, useI18n,
useSelectedCellKeyupListener,
watch, watch,
} from '#imports' } from '#imports'
@ -34,7 +31,7 @@ const emit = defineEmits(['update:modelValue'])
const { t } = useI18n() const { t } = useI18n()
const { showNull } = useGlobal() const { showNull, isMobileMode } = useGlobal()
const columnMeta = inject(ColumnInj, null)! const columnMeta = inject(ColumnInj, null)!
@ -46,19 +43,27 @@ const active = inject(ActiveCellInj, ref(false))
const editable = inject(EditModeInj, ref(false)) const editable = inject(EditModeInj, ref(false))
const isGrid = inject(IsGridInj, ref(false))
const isForm = inject(IsFormInj, ref(false)) const isForm = inject(IsFormInj, ref(false))
const isSurveyForm = inject(IsSurveyFormInj, ref(false)) const isExpandedForm = inject(IsExpandedFormOpenInj, ref(false))
const isDateInvalid = ref(false) const isDateInvalid = ref(false)
const datePickerRef = ref<HTMLInputElement>()
const dateFormat = computed(() => parseProp(columnMeta?.value?.meta)?.date_format ?? 'YYYY-MM-DD') const dateFormat = computed(() => parseProp(columnMeta?.value?.meta)?.date_format ?? 'YYYY-MM-DD')
const picker = computed(() => (isDateMonthFormat(dateFormat.value) ? 'month' : '')) const picker = computed(() => (isDateMonthFormat(dateFormat.value) ? 'month' : ''))
const isClearedInputMode = ref<boolean>(false)
const open = ref<boolean>(false)
const localState = computed({ const localState = computed({
get() { get() {
if (!modelValue) { if (!modelValue || isClearedInputMode.value) {
return undefined return undefined
} }
@ -72,6 +77,8 @@ const localState = computed({
return dayjs(/^\d+$/.test(modelValue) ? +modelValue : modelValue, format) return dayjs(/^\d+$/.test(modelValue) ? +modelValue : modelValue, format)
}, },
set(val?: dayjs.Dayjs) { set(val?: dayjs.Dayjs) {
isClearedInputMode.value = false
if (!val) { if (!val) {
emit('update:modelValue', null) emit('update:modelValue', null)
return return
@ -85,25 +92,56 @@ const localState = computed({
if (val.isValid()) { if (val.isValid()) {
emit('update:modelValue', val?.format('YYYY-MM-DD')) emit('update:modelValue', val?.format('YYYY-MM-DD'))
} }
open.value = false
}, },
}) })
const open = ref<boolean>(false)
const randomClass = `picker_${Math.floor(Math.random() * 99999)}` const randomClass = `picker_${Math.floor(Math.random() * 99999)}`
onClickOutside(datePickerRef, (e) => {
if ((e.target as HTMLElement)?.closest(`.${randomClass}`)) return
datePickerRef.value?.blur?.()
open.value = false
})
const onBlur = (e) => {
if ((e?.relatedTarget as HTMLElement)?.closest(`.${randomClass}`)) return
open.value = false
}
watch( watch(
open, open,
(next) => { (next) => {
if (next) { if (next) {
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false)) editable.value = true
datePickerRef.value?.focus?.()
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, (e) => {
if ((e?.target as HTMLElement)?.closest(`.nc-${randomClass}`)) {
return
}
open.value = false
})
} else { } else {
editable.value = false isClearedInputMode.value = false
} }
}, },
{ flush: 'post' }, { flush: 'post' },
) )
watch(editable, (nextValue) => {
if (isGrid.value && nextValue && !open.value) {
open.value = true
}
})
const placeholder = computed(() => { const placeholder = computed(() => {
if (isForm.value && !isDateInvalid.value) { if (
((isForm.value || isExpandedForm.value) && !isDateInvalid.value) ||
(isGrid.value && !showNull.value && !isDateInvalid.value && !isSystemColumn(columnMeta.value) && active.value)
) {
return dateFormat.value return dateFormat.value
} else if (isEditColumn.value && (modelValue === '' || modelValue === null)) { } else if (isEditColumn.value && (modelValue === '' || modelValue === null)) {
return t('labels.optional') return t('labels.optional')
@ -116,113 +154,18 @@ const placeholder = computed(() => {
} }
}) })
useSelectedCellKeyupListener(active, (e: KeyboardEvent) => {
switch (e.key) {
case 'Enter':
e.stopPropagation()
// skip if drawer / modal is active
if (isDrawerOrModalExist()) {
return
}
if (!open.value) {
// open date picker
open.value = true
} else {
// select the current day
const el = document.querySelector('.nc-picker-date.active .ant-picker-cell-selected') as HTMLButtonElement
if (el) {
el.click()
open.value = false
}
}
break
case 'Escape':
// skip if drawer / modal is active
if (isDrawerOrModalExist()) {
return
}
if (open.value) {
e.stopPropagation()
open.value = false
}
break
case 'ArrowLeft':
if (!localState.value) {
;(document.querySelector('.nc-picker-date.active .ant-picker-header-prev-btn') as HTMLButtonElement)?.click()
} else {
const prevEl = document.querySelector('.nc-picker-date.active .ant-picker-cell-selected')
?.previousElementSibling as HTMLButtonElement
if (prevEl) {
prevEl.click()
} else {
// get the last td from previous tr
const prevRowLastEl = document
.querySelector('.nc-picker-date.active .ant-picker-cell-selected')
?.closest('tr')
?.previousElementSibling?.querySelector('td:last-child') as HTMLButtonElement
if (prevRowLastEl) {
prevRowLastEl.click()
} else {
// go to the previous month
;(document.querySelector('.nc-picker-date.active .ant-picker-header-prev-btn') as HTMLButtonElement)?.click()
}
}
}
break
case 'ArrowRight':
if (!localState.value) {
;(document.querySelector('.nc-picker-date.active .ant-picker-header-next-btn') as HTMLButtonElement)?.click()
} else {
const nextEl = document.querySelector('.nc-picker-date.active .ant-picker-cell-selected')
?.nextElementSibling as HTMLButtonElement
if (nextEl) {
nextEl.click()
} else {
// get the last td from previous tr
const nextRowFirstEl = document
.querySelector('.nc-picker-date.active .ant-picker-cell-selected')
?.closest('tr')
?.nextElementSibling?.querySelector('td:first-child') as HTMLButtonElement
if (nextRowFirstEl) {
nextRowFirstEl.click()
} else {
// go to the next month
;(document.querySelector('.nc-picker-date.active .ant-picker-header-next-btn') as HTMLButtonElement)?.click()
}
}
}
break
case 'ArrowUp':
if (!localState.value)
(document.querySelector('.nc-picker-date.active .ant-picker-header-super-prev-btn') as HTMLButtonElement)?.click()
break
case 'ArrowDown':
if (!localState.value)
(document.querySelector('.nc-picker-date.active .ant-picker-header-super-next-btn') as HTMLButtonElement)?.click()
break
case ';':
localState.value = dayjs(new Date())
break
}
})
const isOpen = computed(() => { const isOpen = computed(() => {
if (readOnly.value) return false if (readOnly.value) return false
return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value
}) })
// use the default date picker open sync only to close the picker
const updateOpen = (next: boolean) => {
if (open.value && !next) {
open.value = false
}
}
const cellClickHook = inject(CellClickHookInj, null) const cellClickHook = inject(CellClickHookInj, null)
const cellClickHandler = () => { const cellClickHandler = () => {
open.value = (active.value || editable.value) && !open.value if (readOnly.value || open.value) return
open.value = active.value || editable.value
} }
onMounted(() => { onMounted(() => {
@ -241,43 +184,88 @@ const clickHandler = () => {
} }
const handleKeydown = (e: KeyboardEvent) => { const handleKeydown = (e: KeyboardEvent) => {
if (e.key !== 'Enter') {
e.stopPropagation()
}
switch (e.key) { switch (e.key) {
case ' ': case 'Enter':
if (isSurveyForm.value) { open.value = !open.value
open.value = !open.value if (!open.value) {
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} }
break return
case 'Escape':
if (open.value) {
open.value = false
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} else {
editable.value = false
case 'Enter': datePickerRef.value?.blur?.()
if (!isSurveyForm.value) { }
open.value = !open.value
return
default:
if (!open.value && /^[0-9a-z]$/i.test(e.key)) {
open.value = true
} }
break
} }
} }
useEventListener(document, 'keydown', (e: KeyboardEvent) => {
// To prevent event listener on non active cell
if (!active.value) return
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey || !isGrid.value || isExpandedForm.value || isEditColumn.value) return
switch (e.key) {
case ';':
localState.value = dayjs(new Date())
e.preventDefault()
break
default:
if (!isOpen.value && datePickerRef.value && /^[0-9a-z]$/i.test(e.key)) {
isClearedInputMode.value = true
datePickerRef.value.focus()
editable.value = true
open.value = true
}
}
})
</script> </script>
<template> <template>
<a-date-picker <a-date-picker
ref="datePickerRef"
v-model:value="localState" v-model:value="localState"
:disabled="readOnly" :disabled="readOnly"
:picker="picker" :picker="picker"
:tabindex="0" :tabindex="0"
:bordered="false" :bordered="false"
class="nc-cell-field !w-full !py-1 !border-none !text-current" class="nc-cell-field !w-full !py-1 !border-none !text-current"
:class="{ 'nc-null': modelValue === null && showNull }" :class="[`nc-${randomClass}`, { 'nc-null': modelValue === null && showNull }]"
:format="dateFormat" :format="dateFormat"
:placeholder="placeholder" :placeholder="placeholder"
:allow-clear="!readOnly && !localState && !isPk" :allow-clear="!readOnly && !isEditColumn"
:input-read-only="true" :input-read-only="!!isMobileMode"
:dropdown-class-name="`${randomClass} nc-picker-date children:border-1 children:border-gray-200 ${open ? 'active' : ''} `" :dropdown-class-name="`${randomClass} nc-picker-date children:border-1 children:border-gray-200 ${open ? 'active' : ''} `"
:open="isOpen" :open="isOpen"
@blur="onBlur"
@click="clickHandler" @click="clickHandler"
@update:open="updateOpen"
@keydown="handleKeydown" @keydown="handleKeydown"
@mouseup.stop
@mousedown.stop
> >
<template #suffixIcon></template> <template #suffixIcon></template>
</a-date-picker> </a-date-picker>
<div v-if="!editable && isGrid" class="absolute inset-0 z-90 cursor-pointer"></div>
</template> </template>
<style scoped> <style scoped>

253
packages/nc-gui/components/cell/DateTimePicker.vue

@ -7,14 +7,11 @@ import {
ColumnInj, ColumnInj,
EditColumnInj, EditColumnInj,
IsFormInj, IsFormInj,
IsSurveyFormInj,
ReadonlyInj, ReadonlyInj,
inject, inject,
isDrawerOrModalExist,
parseProp, parseProp,
ref, ref,
useBase, useBase,
useSelectedCellKeyupListener,
watch, watch,
} from '#imports' } from '#imports'
@ -29,7 +26,7 @@ const emit = defineEmits(['update:modelValue'])
const { isMssql, isXcdbBase } = useBase() const { isMssql, isXcdbBase } = useBase()
const { showNull } = useGlobal() const { showNull, isMobileMode } = useGlobal()
const readOnly = inject(ReadonlyInj, ref(false)) const readOnly = inject(ReadonlyInj, ref(false))
@ -37,18 +34,22 @@ const active = inject(ActiveCellInj, ref(false))
const editable = inject(EditModeInj, ref(false)) const editable = inject(EditModeInj, ref(false))
const isForm = inject(IsFormInj, ref(false)) const isGrid = inject(IsGridInj, ref(false))
const isSurveyForm = inject(IsSurveyFormInj, ref(false)) const isForm = inject(IsFormInj, ref(false))
const { t } = useI18n() const { t } = useI18n()
const isEditColumn = inject(EditColumnInj, ref(false)) const isEditColumn = inject(EditColumnInj, ref(false))
const isExpandedForm = inject(IsExpandedFormOpenInj, ref(false))
const column = inject(ColumnInj)! const column = inject(ColumnInj)!
const isDateInvalid = ref(false) const isDateInvalid = ref(false)
const datePickerRef = ref<HTMLInputElement>()
const dateTimeFormat = computed(() => { const dateTimeFormat = computed(() => {
const dateFormat = parseProp(column?.value?.meta)?.date_format ?? dateFormats[0] const dateFormat = parseProp(column?.value?.meta)?.date_format ?? dateFormats[0]
const timeFormat = parseProp(column?.value?.meta)?.time_format ?? timeFormats[0] const timeFormat = parseProp(column?.value?.meta)?.time_format ?? timeFormats[0]
@ -57,14 +58,13 @@ const dateTimeFormat = computed(() => {
let localModelValue = modelValue ? dayjs(modelValue).utc().local() : undefined let localModelValue = modelValue ? dayjs(modelValue).utc().local() : undefined
const tempLocalValue = ref<dayjs.Dayjs>() const isClearedInputMode = ref<boolean>(false)
const open = ref(false)
const localState = computed({ const localState = computed({
get() { get() {
if (!modelValue && tempLocalValue.value) { if (!modelValue || isClearedInputMode.value) {
return tempLocalValue.value
}
if (!modelValue) {
return undefined return undefined
} }
@ -114,8 +114,10 @@ const localState = computed({
return dayjs(modelValue).utc().local() return dayjs(modelValue).utc().local()
}, },
set(val?: dayjs.Dayjs) { set(val?: dayjs.Dayjs) {
isClearedInputMode.value = false
if (!val) { if (!val) {
emit('update:modelValue', null) emit('update:modelValue', null)
return return
} }
@ -128,8 +130,6 @@ const localState = computed({
}, },
}) })
const open = ref(false)
const isOpen = computed(() => { const isOpen = computed(() => {
if (readOnly.value) return false if (readOnly.value) return false
@ -137,27 +137,44 @@ const isOpen = computed(() => {
}) })
const randomClass = `picker_${Math.floor(Math.random() * 99999)}` const randomClass = `picker_${Math.floor(Math.random() * 99999)}`
onClickOutside(datePickerRef, (e) => {
if ((e.target as HTMLElement)?.closest(`.${randomClass}`)) return
datePickerRef.value?.blur?.()
open.value = false
})
const onBlur = (e) => {
if ((e?.relatedTarget as HTMLElement)?.closest(`.${randomClass}`)) return
open.value = false
}
watch( watch(
open, open,
(next) => { (next) => {
if (next) { if (next) {
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false)) editable.value = true
datePickerRef.value?.focus?.()
if (!modelValue) { onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, (e) => {
tempLocalValue.value = dayjs(new Date()).utc().local() if ((e?.target as HTMLElement)?.closest(`.nc-${randomClass}`)) {
} else { return
tempLocalValue.value = undefined }
} open.value = false
})
} else { } else {
editable.value = false isClearedInputMode.value = false
tempLocalValue.value = undefined
} }
}, },
{ flush: 'post' }, { flush: 'post' },
) )
const placeholder = computed(() => { const placeholder = computed(() => {
if (isForm.value && !isDateInvalid.value) { if (
((isForm.value || isExpandedForm.value) && !isDateInvalid.value) ||
(isGrid.value && !showNull.value && !isDateInvalid.value && !isSystemColumn(column.value) && active.value)
) {
return dateTimeFormat.value return dateTimeFormat.value
} else if (isEditColumn.value && (modelValue === '' || modelValue === null)) { } else if (isEditColumn.value && (modelValue === '' || modelValue === null)) {
return t('labels.optional') return t('labels.optional')
@ -170,105 +187,18 @@ const placeholder = computed(() => {
} }
}) })
useSelectedCellKeyupListener(active, (e: KeyboardEvent) => {
switch (e.key) {
case 'Enter':
e.stopPropagation()
// skip if drawer / modal is active
if (isDrawerOrModalExist()) {
return
}
if (!open.value) {
// open date picker
open.value = true
} else {
// click Ok button to save the currently selected date
;(document.querySelector('.nc-picker-datetime.active .ant-picker-ok button') as HTMLButtonElement)?.click()
}
break
case 'Escape':
// skip if drawer / modal is active
if (isDrawerOrModalExist()) {
return
}
if (open.value) {
e.stopPropagation()
open.value = false
}
break
case 'ArrowLeft':
if (!localState.value) {
;(document.querySelector('.nc-picker-datetime.active .ant-picker-header-prev-btn') as HTMLButtonElement)?.click()
} else {
const prevEl = document.querySelector('.nc-picker-datetime.active .ant-picker-cell-selected')
?.previousElementSibling as HTMLButtonElement
if (prevEl) {
prevEl.click()
} else {
// get the last td from previous tr
const prevRowLastEl = document
.querySelector('.nc-picker-datetime.active .ant-picker-cell-selected')
?.closest('tr')
?.previousElementSibling?.querySelector('td:last-child') as HTMLButtonElement
if (prevRowLastEl) {
prevRowLastEl.click()
} else {
// go to the previous month
;(document.querySelector('.nc-picker-datetime.active .ant-picker-header-prev-btn') as HTMLButtonElement)?.click()
}
}
}
break
case 'ArrowRight':
if (!localState.value) {
;(document.querySelector('.nc-picker-datetime.active .ant-picker-header-next-btn') as HTMLButtonElement)?.click()
} else {
const nextEl = document.querySelector('.nc-picker-datetime.active .ant-picker-cell-selected')
?.nextElementSibling as HTMLButtonElement
if (nextEl) {
nextEl.click()
} else {
// get the last td from previous tr
const nextRowFirstEl = document
.querySelector('.nc-picker-datetime.active .ant-picker-cell-selected')
?.closest('tr')
?.nextElementSibling?.querySelector('td:first-child') as HTMLButtonElement
if (nextRowFirstEl) {
nextRowFirstEl.click()
} else {
// go to the next month
;(document.querySelector('.nc-picker-datetime.active .ant-picker-header-next-btn') as HTMLButtonElement)?.click()
}
}
}
break
case 'ArrowUp':
if (!localState.value)
(document.querySelector('.nc-picker-datetime.active .ant-picker-header-super-prev-btn') as HTMLButtonElement)?.click()
break
case 'ArrowDown':
if (!localState.value)
(document.querySelector('.nc-picker-datetime.active .ant-picker-header-super-next-btn') as HTMLButtonElement)?.click()
break
case ';':
localState.value = dayjs(new Date())
break
}
})
const cellClickHook = inject(CellClickHookInj, null) const cellClickHook = inject(CellClickHookInj, null)
const cellClickHandler = () => { const cellClickHandler = () => {
if (readOnly.value) return if (readOnly.value || open.value) return
open.value = (active.value || editable.value) && !open.value open.value = active.value || editable.value
} }
function okHandler(val: dayjs.Dayjs | string) { function okHandler(val: dayjs.Dayjs | string) {
isClearedInputMode.value = false
if (!val) { if (!val) {
emit('update:modelValue', null) emit('update:modelValue', null)
return } else if (dayjs(val).isValid()) {
}
if (dayjs(val).isValid()) {
// setting localModelValue to cater NOW function in date picker // setting localModelValue to cater NOW function in date picker
localModelValue = dayjs(val) localModelValue = dayjs(val)
// send the payload in UTC format // send the payload in UTC format
@ -276,7 +206,12 @@ function okHandler(val: dayjs.Dayjs | string) {
} }
open.value = !open.value open.value = !open.value
if (!open.value && isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
editable.value = false
}
} }
onMounted(() => { onMounted(() => {
cellClickHook?.on(cellClickHandler) cellClickHook?.on(cellClickHandler)
}) })
@ -284,7 +219,14 @@ onUnmounted(() => {
cellClickHook?.on(cellClickHandler) cellClickHook?.on(cellClickHandler)
}) })
const clickHandler = () => { const clickHandler = (e) => {
if ((e.target as HTMLElement).closest(`.nc-${randomClass} .ant-picker-clear`)) {
e.stopPropagation()
emit('update:modelValue', null)
open.value = false
return
}
if (cellClickHook) { if (cellClickHook) {
return return
} }
@ -296,42 +238,99 @@ const isColDisabled = computed(() => {
}) })
const handleKeydown = (e: KeyboardEvent) => { const handleKeydown = (e: KeyboardEvent) => {
if (e.key !== 'Enter') {
e.stopPropagation()
}
switch (e.key) { switch (e.key) {
case ' ': case 'Enter':
if (isSurveyForm.value) { if (isOpen.value) {
open.value = !open.value return okHandler((e.target as HTMLInputElement).value)
} else {
open.value = true
} }
break return
case 'Escape':
if (open.value) {
open.value = false
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} else {
editable.value = false
case 'Enter': datePickerRef.value?.blur?.()
if (!isSurveyForm.value) { }
open.value = !open.value
return
case 'Tab':
open.value = false
if (isGrid.value) {
editable.value = false
datePickerRef.value?.blur()
}
return
default:
if (!open.value && /^[0-9a-z]$/i.test(e.key)) {
open.value = true
} }
break
} }
} }
useEventListener(document, 'keydown', (e: KeyboardEvent) => {
// To prevent event listener on non active cell
if (!active.value) return
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey || !isGrid.value || isExpandedForm.value || isEditColumn.value) return
switch (e.key) {
case ';':
localState.value = dayjs(new Date())
e.preventDefault()
break
default:
if (!isOpen.value && datePickerRef.value && /^[0-9a-z]$/i.test(e.key)) {
isClearedInputMode.value = true
datePickerRef.value.focus()
editable.value = true
open.value = true
}
}
})
watch(editable, (nextValue) => {
if (isGrid.value && nextValue && !open.value) {
open.value = true
}
})
</script> </script>
<template> <template>
<a-date-picker <a-date-picker
ref="datePickerRef"
:value="localState" :value="localState"
:disabled="isColDisabled" :disabled="isColDisabled"
:show-time="true" :show-time="true"
:bordered="false" :bordered="false"
class="nc-cell-field !w-full !py-1 !border-none !text-current" class="nc-cell-field nc-cell-picker-datetime !w-full !py-1 !border-none !text-current"
:class="{ 'nc-null': modelValue === null && showNull }" :class="[`nc-${randomClass}`, { 'nc-null': modelValue === null && showNull }]"
:format="dateTimeFormat" :format="dateTimeFormat"
:placeholder="placeholder" :placeholder="placeholder"
:allow-clear="!readOnly && !localState && !isPk" :allow-clear="!isColDisabled && !isEditColumn"
:input-read-only="true" :input-read-only="!!isMobileMode"
:dropdown-class-name="`${randomClass} nc-picker-datetime children:border-1 children:border-gray-200 ${open ? 'active' : ''}`" :dropdown-class-name="`${randomClass} nc-picker-datetime children:border-1 children:border-gray-200 ${open ? 'active' : ''}`"
:open="isOpen" :open="isOpen"
@blur="onBlur"
@click="clickHandler" @click="clickHandler"
@ok="okHandler" @ok="okHandler"
@keydown="handleKeydown" @keydown="handleKeydown"
@mouseup.stop
@mousedown.stop
> >
<template #suffixIcon></template> <template #suffixIcon></template>
</a-date-picker> </a-date-picker>
<div v-if="!editable && isGrid" class="absolute inset-0 z-90 cursor-pointer"></div>
</template> </template>
<style scoped> <style scoped>

148
packages/nc-gui/components/cell/TimePicker.vue

@ -1,17 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { import { isSystemColumn } from 'nocodb-sdk'
ActiveCellInj, import { ActiveCellInj, EditColumnInj, IsFormInj, ReadonlyInj, inject, onClickOutside, useBase, watch } from '#imports'
EditColumnInj,
IsFormInj,
IsSurveyFormInj,
ReadonlyInj,
inject,
onClickOutside,
useBase,
useSelectedCellKeyupListener,
watch,
} from '#imports'
interface Props { interface Props {
modelValue?: string | null | undefined modelValue?: string | null | undefined
@ -24,7 +14,7 @@ const emit = defineEmits(['update:modelValue'])
const { isMysql } = useBase() const { isMysql } = useBase()
const { showNull } = useGlobal() const { showNull, isMobileMode } = useGlobal()
const readOnly = inject(ReadonlyInj, ref(false)) const readOnly = inject(ReadonlyInj, ref(false))
@ -34,21 +24,29 @@ const editable = inject(EditModeInj, ref(false))
const isEditColumn = inject(EditColumnInj, ref(false)) const isEditColumn = inject(EditColumnInj, ref(false))
const isGrid = inject(IsGridInj, ref(false))
const isForm = inject(IsFormInj, ref(false)) const isForm = inject(IsFormInj, ref(false))
const isSurveyForm = inject(IsSurveyFormInj, ref(false)) const isExpandedForm = inject(IsExpandedFormOpenInj, ref(false))
const column = inject(ColumnInj)! const column = inject(ColumnInj)!
const dateFormat = isMysql(column.value.source_id) ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'
const isTimeInvalid = ref(false) const isTimeInvalid = ref(false)
const dateFormat = isMysql(column.value.source_id) ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ' const datePickerRef = ref<HTMLInputElement>()
const isClearedInputMode = ref<boolean>(false)
const { t } = useI18n() const { t } = useI18n()
const open = ref(false)
const localState = computed({ const localState = computed({
get() { get() {
if (!modelValue) { if (!modelValue || isClearedInputMode.value) {
return undefined return undefined
} }
let dateTime = dayjs(modelValue) let dateTime = dayjs(modelValue)
@ -67,6 +65,7 @@ const localState = computed({
return dateTime return dateTime
}, },
set(val?: dayjs.Dayjs) { set(val?: dayjs.Dayjs) {
isClearedInputMode.value = false
if (!val) { if (!val) {
emit('update:modelValue', null) emit('update:modelValue', null)
return return
@ -80,23 +79,51 @@ const localState = computed({
}, },
}) })
const open = ref(false)
const randomClass = `picker_${Math.floor(Math.random() * 99999)}` const randomClass = `picker_${Math.floor(Math.random() * 99999)}`
onClickOutside(datePickerRef, (e) => {
if ((e.target as HTMLElement)?.closest(`.${randomClass}`)) return
datePickerRef.value?.blur?.()
open.value = false
})
const onBlur = (e) => {
if ((e?.relatedTarget as HTMLElement)?.closest(`.${randomClass}`)) return
open.value = false
}
watch( watch(
open, open,
(next) => { (next) => {
if (next) { if (next) {
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false)) editable.value = true
datePickerRef.value?.focus?.()
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, (e) => {
if ((e?.target as HTMLElement)?.closest(`.nc-${randomClass}`)) {
return
}
open.value = false
})
} else { } else {
editable.value = false isClearedInputMode.value = false
} }
}, },
{ flush: 'post' }, { flush: 'post' },
) )
watch(editable, (nextValue) => {
if (isGrid.value && nextValue && !open.value) {
open.value = true
}
})
const placeholder = computed(() => { const placeholder = computed(() => {
if (isForm.value && !isTimeInvalid.value) { if (
((isForm.value || isExpandedForm.value) && !isTimeInvalid.value) ||
(isGrid.value && !showNull.value && !isTimeInvalid.value && !isSystemColumn(column.value) && active.value)
) {
return 'HH:mm' return 'HH:mm'
} else if (isEditColumn.value && (modelValue === '' || modelValue === null)) { } else if (isEditColumn.value && (modelValue === '' || modelValue === null)) {
return t('labels.optional') return t('labels.optional')
@ -115,40 +142,71 @@ const isOpen = computed(() => {
return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value
}) })
const handleKeydown = (e: KeyboardEvent) => { const clickHandler = () => {
switch (e.key) { if (readOnly.value || open.value) return
case ' ': open.value = active.value || editable.value
if (isSurveyForm.value) { }
open.value = !open.value
}
break
case 'Enter': const handleKeydown = (e: KeyboardEvent) => {
if (!isSurveyForm.value) { if (e.key !== 'Enter') {
open.value = !open.value e.stopPropagation()
}
break
} }
}
useSelectedCellKeyupListener(active, (e: KeyboardEvent) => {
switch (e.key) { switch (e.key) {
case 'Enter': case 'Enter':
e.stopPropagation() open.value = !open.value
open.value = true if (!open.value) {
break editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
}
return
case 'Escape': case 'Escape':
if (open.value) { if (open.value) {
e.stopPropagation()
open.value = false open.value = false
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} else {
editable.value = false
datePickerRef.value?.blur?.()
}
return
default:
if (!open.value && /^[0-9a-z]$/i.test(e.key)) {
open.value = true
} }
}
}
useEventListener(document, 'keydown', (e: KeyboardEvent) => {
// To prevent event listener on non active cell
if (!active.value) return
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey || !isGrid.value || isExpandedForm.value || isEditColumn.value) return
switch (e.key) {
case ';':
localState.value = dayjs(new Date())
e.preventDefault()
break break
default:
if (!isOpen.value && datePickerRef.value && /^[0-9a-z]$/i.test(e.key)) {
isClearedInputMode.value = true
datePickerRef.value.focus()
editable.value = true
open.value = true
}
} }
}) })
</script> </script>
<template> <template>
<a-time-picker <a-time-picker
ref="datePickerRef"
v-model:value="localState" v-model:value="localState"
:tabindex="0" :tabindex="0"
:disabled="readOnly" :disabled="readOnly"
@ -157,18 +215,22 @@ useSelectedCellKeyupListener(active, (e: KeyboardEvent) => {
use12-hours use12-hours
format="HH:mm" format="HH:mm"
class="nc-cell-field !w-full !py-1 !border-none !text-current" class="nc-cell-field !w-full !py-1 !border-none !text-current"
:class="{ 'nc-null': modelValue === null && showNull }" :class="[`nc-${randomClass}`, { 'nc-null': modelValue === null && showNull }]"
:placeholder="placeholder" :placeholder="placeholder"
:allow-clear="!readOnly && !localState && !isPk" :allow-clear="!readOnly && !isPk && !isEditColumn"
:input-read-only="true" :input-read-only="!!isMobileMode"
:open="isOpen" :open="isOpen"
:popup-class-name="`${randomClass} nc-picker-time children:border-1 children:border-gray-200 ${open ? 'active' : ''}`" :popup-class-name="`${randomClass} nc-picker-time children:border-1 children:border-gray-200 ${open ? 'active' : ''}`"
@blur="onBlur"
@keydown="handleKeydown" @keydown="handleKeydown"
@click="open = (active || editable) && !open" @click="clickHandler"
@ok="open = !open" @ok="open = !open"
@mouseup.stop
@mousedown.stop
> >
<template #suffixIcon></template> <template #suffixIcon></template>
</a-time-picker> </a-time-picker>
<div v-if="!editable && isGrid" class="absolute inset-0 z-90 cursor-pointer"></div>
</template> </template>
<style scoped> <style scoped>

155
packages/nc-gui/components/cell/YearPicker.vue

@ -1,18 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { import { isSystemColumn } from 'nocodb-sdk'
ActiveCellInj, import { ActiveCellInj, EditColumnInj, IsFormInj, ReadonlyInj, computed, inject, onClickOutside, ref, watch } from '#imports'
EditColumnInj,
IsFormInj,
IsSurveyFormInj,
ReadonlyInj,
computed,
inject,
onClickOutside,
ref,
useSelectedCellKeyupListener,
watch,
} from '#imports'
interface Props { interface Props {
modelValue?: number | string | null modelValue?: number | string | null
@ -23,7 +12,9 @@ const { modelValue, isPk = false } = defineProps<Props>()
const emit = defineEmits(['update:modelValue']) const emit = defineEmits(['update:modelValue'])
const { showNull } = useGlobal() const { showNull, isMobileMode } = useGlobal()
const column = inject(ColumnInj, null)!
const readOnly = inject(ReadonlyInj, ref(false)) const readOnly = inject(ReadonlyInj, ref(false))
@ -33,17 +24,25 @@ const editable = inject(EditModeInj, ref(false))
const isEditColumn = inject(EditColumnInj, ref(false)) const isEditColumn = inject(EditColumnInj, ref(false))
const isGrid = inject(IsGridInj, ref(false))
const isForm = inject(IsFormInj, ref(false)) const isForm = inject(IsFormInj, ref(false))
const isSurveyForm = inject(IsSurveyFormInj, ref(false)) const isExpandedForm = inject(IsExpandedFormOpenInj, ref(false))
const isYearInvalid = ref(false) const isYearInvalid = ref(false)
const datePickerRef = ref<HTMLInputElement>()
const isClearedInputMode = ref<boolean>(false)
const { t } = useI18n() const { t } = useI18n()
const open = ref<boolean>(false)
const localState = computed({ const localState = computed({
get() { get() {
if (!modelValue) { if (!modelValue || isClearedInputMode.value) {
return undefined return undefined
} }
@ -56,6 +55,8 @@ const localState = computed({
return yearDate return yearDate
}, },
set(val?: dayjs.Dayjs) { set(val?: dayjs.Dayjs) {
isClearedInputMode.value = false
if (!val) { if (!val) {
emit('update:modelValue', null) emit('update:modelValue', null)
return return
@ -64,26 +65,56 @@ const localState = computed({
if (val?.isValid()) { if (val?.isValid()) {
emit('update:modelValue', val.format('YYYY')) emit('update:modelValue', val.format('YYYY'))
} }
open.value = false
}, },
}) })
const open = ref<boolean>(false)
const randomClass = `picker_${Math.floor(Math.random() * 99999)}` const randomClass = `picker_${Math.floor(Math.random() * 99999)}`
onClickOutside(datePickerRef, (e) => {
if ((e.target as HTMLElement)?.closest(`.${randomClass}`)) return
datePickerRef.value?.blur?.()
open.value = false
})
const onBlur = (e) => {
if ((e?.relatedTarget as HTMLElement)?.closest(`.${randomClass}`)) return
open.value = false
}
watch( watch(
open, open,
(next) => { (next) => {
if (next) { if (next) {
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, () => (open.value = false)) editable.value = true
datePickerRef.value?.focus?.()
onClickOutside(document.querySelector(`.${randomClass}`)! as HTMLDivElement, (e) => {
if ((e?.target as HTMLElement)?.closest(`.nc-${randomClass}`)) {
return
}
open.value = false
})
} else { } else {
editable.value = false isClearedInputMode.value = false
} }
}, },
{ flush: 'post' }, { flush: 'post' },
) )
watch(editable, (nextValue) => {
if (isGrid.value && nextValue && !open.value) {
open.value = true
}
})
const placeholder = computed(() => { const placeholder = computed(() => {
if (isForm.value && !isYearInvalid.value) { if (
((isForm.value || isExpandedForm.value) && !isYearInvalid.value) ||
(isGrid.value && !showNull.value && !isYearInvalid.value && !isSystemColumn(column.value) && active.value)
) {
return 'YYYY' return 'YYYY'
} else if (isEditColumn.value && (modelValue === '' || modelValue === null)) { } else if (isEditColumn.value && (modelValue === '' || modelValue === null)) {
return t('labels.optional') return t('labels.optional')
@ -98,62 +129,96 @@ const placeholder = computed(() => {
const isOpen = computed(() => { const isOpen = computed(() => {
if (readOnly.value) return false if (readOnly.value) return false
return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value return (readOnly.value || (localState.value && isPk)) && !active.value && !editable.value ? false : open.value
}) })
const clickHandler = () => {
if (readOnly.value || open.value) return
open.value = active.value || editable.value
}
const handleKeydown = (e: KeyboardEvent) => { const handleKeydown = (e: KeyboardEvent) => {
if (e.key !== 'Enter') {
e.stopPropagation()
}
switch (e.key) { switch (e.key) {
case ' ': case 'Enter':
if (isSurveyForm.value) { open.value = !open.value
open.value = !open.value if (!open.value) {
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} }
break
case 'Enter': return
if (!isSurveyForm.value) { case 'Escape':
open.value = !open.value if (open.value) {
open.value = false
editable.value = false
if (isGrid.value && !isExpandedForm.value && !isEditColumn.value) {
datePickerRef.value?.blur?.()
}
} else {
editable.value = false
datePickerRef.value?.blur?.()
}
return
default:
if (!open.value && /^[0-9a-z]$/i.test(e.key)) {
open.value = true
} }
break
} }
} }
useSelectedCellKeyupListener(active, (e: KeyboardEvent) => { useEventListener(document, 'keydown', (e: KeyboardEvent) => {
// To prevent event listener on non active cell
if (!active.value) return
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey || !isGrid.value || isExpandedForm.value || isEditColumn.value) return
switch (e.key) { switch (e.key) {
case 'Enter': case ';':
e.stopPropagation() localState.value = dayjs(new Date())
open.value = true e.preventDefault()
break break
case 'Escape': default:
if (open.value) { if (!isOpen.value && datePickerRef.value && /^[0-9a-z]$/i.test(e.key)) {
e.stopPropagation() isClearedInputMode.value = true
open.value = false datePickerRef.value.focus()
editable.value = true
open.value = true
} }
break
} }
}) })
</script> </script>
<template> <template>
<a-date-picker <a-date-picker
ref="datePickerRef"
v-model:value="localState" v-model:value="localState"
:disabled="readOnly"
:tabindex="0" :tabindex="0"
picker="year" picker="year"
:bordered="false" :bordered="false"
class="nc-cell-field !w-full !py-1 !border-none !text-current" class="nc-cell-field !w-full !py-1 !border-none !text-current"
:class="{ 'nc-null': modelValue === null && showNull }" :class="[`nc-${randomClass}`, { 'nc-null': modelValue === null && showNull }]"
:placeholder="placeholder" :placeholder="placeholder"
:allow-clear="(!readOnly && !localState && !isPk) || isEditColumn" :allow-clear="!readOnly && !isPk"
:input-read-only="true" :input-read-only="!!isMobileMode"
:open="isOpen" :open="isOpen"
:dropdown-class-name="`${randomClass} nc-picker-year children:border-1 children:border-gray-200 ${open ? 'active' : ''}`" :dropdown-class-name="`${randomClass} nc-picker-year children:border-1 children:border-gray-200 ${open ? 'active' : ''}`"
@blur="onBlur"
@keydown="handleKeydown" @keydown="handleKeydown"
@click="open = (active || editable) && !open" @click="clickHandler"
@change="open = (active || editable) && !open" @mouseup.stop
@ok="open = !open" @mousedown.stop
> >
<template #suffixIcon></template> <template #suffixIcon></template>
</a-date-picker> </a-date-picker>
<div v-if="!editable && isGrid" class="absolute inset-0 z-90 cursor-pointer"></div>
</template> </template>
<style scoped> <style scoped>

14
packages/nc-gui/components/smartsheet/Cell.vue

@ -196,7 +196,13 @@ onUnmounted(() => {
{ {
'text-brand-500': isPrimary(column) && !props.virtual && !isForm && !isCalendar, 'text-brand-500': isPrimary(column) && !props.virtual && !isForm && !isCalendar,
'nc-grid-numeric-cell-right': 'nc-grid-numeric-cell-right':
isGrid && isNumericField && !isEditColumnMenu && !isForm && !isExpandedFormOpen && !isRating(column), isGrid &&
isNumericField &&
!isEditColumnMenu &&
!isForm &&
!isExpandedFormOpen &&
!isRating(column) &&
!isYear(column, abstractType),
'h-10': !isEditColumnMenu && isForm && !isAttachment(column) && !isTextArea(column) && !isJSON(column) && !props.virtual, 'h-10': !isEditColumnMenu && isForm && !isAttachment(column) && !isTextArea(column) && !isJSON(column) && !props.virtual,
'nc-grid-numeric-cell-left': (isForm && isNumericField && isExpandedFormOpen) || isEditColumnMenu, 'nc-grid-numeric-cell-left': (isForm && isNumericField && isExpandedFormOpen) || isEditColumnMenu,
'!min-h-30': isTextArea(column) && (isForm || isSurveyForm), '!min-h-30': isTextArea(column) && (isForm || isSurveyForm),
@ -271,7 +277,9 @@ onUnmounted(() => {
} }
} }
.nc-cell .nc-cell-field { .nc-cell {
@apply px-0; :deep(.nc-cell-field) {
@apply px-0;
}
} }
</style> </style>

2
packages/nc-gui/components/smartsheet/grid/Table.vue

@ -585,7 +585,7 @@ const {
// ignore navigating if picker(Date, Time, DateTime, Year) // ignore navigating if picker(Date, Time, DateTime, Year)
// or single/multi select options is open // or single/multi select options is open
const activePickerOrDropdownEl = document.querySelector( const activePickerOrDropdownEl = document.querySelector(
'.nc-picker-datetime.active,.nc-dropdown-single-select-cell.active,.nc-dropdown-multi-select-cell.active,.nc-picker-date.active,.nc-picker-year.active,.nc-picker-time.active', '.nc-dropdown-single-select-cell.active,.nc-dropdown-multi-select-cell.active',
) )
if (activePickerOrDropdownEl) { if (activePickerOrDropdownEl) {
e.preventDefault() e.preventDefault()

2
tests/playwright/pages/Dashboard/common/Cell/TimeCell.ts

@ -48,7 +48,7 @@ export class TimeCellPageObject extends BasePage {
async set({ index, columnHeader, value }: { index: number; columnHeader: string; value: string }) { async set({ index, columnHeader, value }: { index: number; columnHeader: string; value: string }) {
const [hour, minute, _second] = value.split(':'); const [hour, minute, _second] = value.split(':');
await this.get({ index, columnHeader }).click(); await this.get({ index, columnHeader }).click();
await this.get({ index, columnHeader }).click(); await this.get({ index, columnHeader }).dblclick();
await this.selectTime({ hour: +hour, minute: +minute }); await this.selectTime({ hour: +hour, minute: +minute });
await this.save(); await this.save();
} }

Loading…
Cancel
Save