fix(fe): resolve datepicker interaction in KsDateField by adding native picker fallback and click target
This commit is contained in:
@@ -35,10 +35,16 @@ const emit = defineEmits<{
|
|||||||
const isFocused = ref(false)
|
const isFocused = ref(false)
|
||||||
const id = computed(() => props.inputId || `date-${Math.random().toString(36).substr(2, 9)}`)
|
const id = computed(() => props.inputId || `date-${Math.random().toString(36).substr(2, 9)}`)
|
||||||
|
|
||||||
|
const inputRef = ref<HTMLInputElement | null>(null)
|
||||||
|
|
||||||
const dateString = computed(() => {
|
const dateString = computed(() => {
|
||||||
if (!props.modelValue) return ''
|
if (!props.modelValue) return ''
|
||||||
const date = typeof props.modelValue === 'string' ? new Date(props.modelValue) : props.modelValue
|
const date = typeof props.modelValue === 'string' ? new Date(props.modelValue) : props.modelValue
|
||||||
return date.toISOString().split('T')[0]
|
if (isNaN(date.getTime())) return ''
|
||||||
|
const year = date.getFullYear()
|
||||||
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
|
const day = String(date.getDate()).padStart(2, '0')
|
||||||
|
return `${year}-${month}-${day}`
|
||||||
})
|
})
|
||||||
|
|
||||||
const containerClass = computed(() => [
|
const containerClass = computed(() => [
|
||||||
@@ -55,9 +61,25 @@ const containerClass = computed(() => [
|
|||||||
|
|
||||||
const handleInput = (event: Event) => {
|
const handleInput = (event: Event) => {
|
||||||
const target = event.target as HTMLInputElement
|
const target = event.target as HTMLInputElement
|
||||||
|
if (!target.value) {
|
||||||
|
emit('update:modelValue', null)
|
||||||
|
emit('change', null)
|
||||||
|
return
|
||||||
|
}
|
||||||
const date = new Date(target.value)
|
const date = new Date(target.value)
|
||||||
emit('update:modelValue', date)
|
if (!isNaN(date.getTime())) {
|
||||||
emit('change', date)
|
emit('update:modelValue', date)
|
||||||
|
emit('change', date)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const openPicker = () => {
|
||||||
|
if (props.disabled || props.readonly) return
|
||||||
|
if (inputRef.value && typeof inputRef.value.showPicker === 'function') {
|
||||||
|
inputRef.value.showPicker()
|
||||||
|
} else if (inputRef.value) {
|
||||||
|
inputRef.value.focus()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFocus = (event: FocusEvent) => {
|
const handleFocus = (event: FocusEvent) => {
|
||||||
@@ -83,6 +105,7 @@ const handleBlur = (event: FocusEvent) => {
|
|||||||
<div class="ks-date-field__container">
|
<div class="ks-date-field__container">
|
||||||
<input
|
<input
|
||||||
:id="id"
|
:id="id"
|
||||||
|
ref="inputRef"
|
||||||
:type="type === 'range' ? 'text' : type"
|
:type="type === 'range' ? 'text' : type"
|
||||||
:value="dateString"
|
:value="dateString"
|
||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
@@ -98,7 +121,7 @@ const handleBlur = (event: FocusEvent) => {
|
|||||||
@focus="handleFocus"
|
@focus="handleFocus"
|
||||||
@blur="handleBlur"
|
@blur="handleBlur"
|
||||||
/>
|
/>
|
||||||
<span class="ks-date-field__icon">📅</span>
|
<span class="ks-date-field__icon" @click="openPicker">📅</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Error or help text -->
|
<!-- Error or help text -->
|
||||||
@@ -179,7 +202,7 @@ const handleBlur = (event: FocusEvent) => {
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
right: var(--spacing-3);
|
right: var(--spacing-3);
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
pointer-events: none;
|
cursor: pointer;
|
||||||
color: var(--color-text-tertiary);
|
color: var(--color-text-tertiary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user