Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to keep typing date time in PreciseDateTimePicker #4814

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
density="compact"
hide-details="true"
maxlength="2"
@update:model-value="filterNumeric('month', $event)"
@update:model-value="filterNumeric('month', $event, 'day')"
@keydown="handleKeyPress('day', $event)"
@blur="validateMonth"
@focus="highlightText"
@click="highlightText"
Expand All @@ -27,7 +28,9 @@
density="compact"
hide-details="true"
maxlength="2"
@update:model-value="filterNumeric('day', $event)"
ref="day"
@update:model-value="filterNumeric('day', $event, 'year')"
@keydown="handleKeyPress('year', $event)"
@blur="validateDay"
@focus="highlightText"
@click="highlightText"
Expand All @@ -42,7 +45,9 @@
density="compact"
hide-details="true"
maxlength="4"
@update:model-value="filterNumeric('year', $event, true)"
ref="year"
@update:model-value="filterNumericYear('year', $event, 'hour')"
@keydown="handleKeyPress('hour', $event)"
@blur="validateYear"
@focus="highlightText"
@click="highlightText"
Expand All @@ -57,7 +62,9 @@
density="compact"
hide-details="true"
maxlength="2"
@update:model-value="filterNumeric('hour', $event)"
ref="hour"
@update:model-value="filterNumeric('hour', $event, 'minutes')"
@keydown="handleKeyPress('minutes', $event)"
@blur="validateHour"
@focus="highlightText"
@click="highlightText"
Expand All @@ -72,7 +79,9 @@
density="compact"
hide-details="true"
maxlength="2"
@update:model-value="filterNumeric('minutes', $event)"
ref="minutes"
@update:model-value="filterNumeric('minutes', $event, 'seconds')"
@keydown="handleKeyPress('seconds', $event)"
@blur="validateMinutes"
@focus="highlightText"
@click="highlightText"
Expand All @@ -87,7 +96,9 @@
density="compact"
hide-details="true"
maxlength="6"
ref="seconds"
@update:model-value="filterNumericSeconds('seconds', $event)"
@keydown="handleKeyPress(null, $event, true)"
@blur="validateSeconds"
@focus="highlightText"
@click="highlightText"
Expand Down Expand Up @@ -175,6 +186,40 @@ export default {
this.updateUnixTimestamp()
}
},
handleKeyPress(nextField, event, allowDecimal = false) {
const allowedKeys = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"Backspace",
"ArrowLeft",
"ArrowRight",
"Delete",
"Tab",
"Enter",
]
if (allowDecimal) {
allowedKeys.push(".")
}
if (!allowedKeys.includes(event.key)) {
// Prevent default action for non-numeric and edit keys
event.preventDefault()
}
if (nextField && event.key === "Enter") {
// if Enter, move to the next field
event.preventDefault()
if (nextField && this.$refs[nextField]) {
this.$refs[nextField].focus()
}
}
},
okHandler() {
let date = `${this.year}-${this.month}-${this.day}T${this.hour}:${this.minutes}:${this.seconds}`
if (this.timezone !== "UTC") {
Expand Down Expand Up @@ -297,7 +342,7 @@ export default {
}
this.updateUnixTimestamp()
},
filterNumeric(field, value, isYear = false) {
filterNumericInt(field, value, isYear = false) {
// Remove non-numeric characters
const numericValue = value.replace(/[^0-9]/g, "")
if (!numericValue || isNaN(numericValue)) {
Expand All @@ -306,7 +351,27 @@ export default {
this[field] = numericValue
}
},
filterNumericDecmial(field, value) {
filterNumeric(field, value, nextField) {
if (value.length >= 2) {
// Move focus to the next field if max length is reached
if (nextField && this.$refs[nextField]) {
this.$refs[nextField].focus()
}
} else {
this.filterNumericInt(field, value, false)
}
},
filterNumericYear(field, value, nextField) {
if (value.length >= 4) {
// Move focus to the next field if max length is reached
if (nextField && this.$refs[nextField]) {
this.$refs[nextField].focus()
}
} else {
this.filterNumericInt(field, value, true)
}
},
filterNumericSeconds(field, value) {
// Remove non-numeric characters but allow decimal point
const numericValue = value.replace(/[^0-9.]/g, "")
if (!numericValue || isNaN(numericValue)) {
Expand Down
Loading