select next field when the tab key is pressed

This commit is contained in:
Bram Wiepjes committed 2020-05-03 21:07:29 +02:00
1 parent 03bdb3d449
commit 2ff6ed9aa4
4 files changed
+119 -2

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# Changelog
## Unreleased
* The next field is now selected when the tab character is pressed when a field is
selected.
@@ -50,9 +50,12 @@
</div>
<GridViewField
v-if="primary !== null"
:ref="'row-' + row.id + '-field-' + primary.id"
:field="primary"
:row="row"
:table="table"
@selected="selectedField(primary, $event.component)"
@selectNext="selectNextField(row, primary, fields, primary)"
></GridViewField>
</div>
</div>
@@ -150,12 +153,18 @@
>
<GridViewField
v-for="field in fields"
:ref="'row-' + row.id + '-field-' + field.id"
:key="
'right-row-field-' + view.id + '-' + row.id + '-' + field.id
"
:field="field"
:row="row"
:table="table"
@selected="selectedField(field, $event.component)"
@selectPrevious="
selectNextField(row, field, fields, primary, true)
"
@selectNext="selectNextField(row, field, fields, primary)"
></GridViewField>
</div>
</div>
@@ -325,6 +334,82 @@ export default {
notifyIf(error, 'row')
}
},
/**
* When a field is selected we want to make sure it is visible so we might need to
* scroll a little bit. Because the primary fields are always visible we don't have
* to do anything when those are selected.
*/
selectedField(field, component) {
if (field.primary) {
return
}
const container = this.$refs.right
const containerLeft = container.scrollLeft
const containerWidth = container.clientWidth
const containerRight = containerLeft + container.clientWidth
const element = component.$el
const elementLeft = element.offsetLeft
const elementWidth = element.offsetWidth
const elementRight = element.offsetLeft + element.offsetWidth
if (elementWidth >= containerWidth || elementLeft < containerLeft) {
// If the element doesn't fit in the viewport we just want to make sure the
// beginning is visible.
container.scrollLeft = elementLeft - 20
this.$refs.scrollbars.updateHorizontal()
} else if (elementRight > containerRight) {
// If the right side if the element isn't visible within the viewport.
// The +10 is a small padding.
container.scrollLeft = elementRight - containerWidth + 20
this.$refs.scrollbars.updateHorizontal()
}
},
/**
* This method is called when the next field must be selected. This can for example
* happen when the tab key is pressed. It tries to find the next field and will
* select that one.
*/
selectNextField(row, field, fields, primary, previous = false) {
let nextFieldId = -1
if (field.primary && fields.length > 0 && !previous) {
// If the currently selected field is the primary we can just select the first
// field of the fields if there are any.
nextFieldId = fields[0].id
} else if (!field.primary) {
// First we need to know which index the currently selected field has in the
// fields list.
const index = fields.findIndex((f) => f.id === field.id)
if (!previous && fields.length > index + 1) {
// If we want to select the next field we can just check if the next index
// exists and read the id from there.
nextFieldId = fields[index + 1].id
} else if (previous && index > 0) {
// If we want to select the previous field we can just check if aren't already
// the first and read the id from the previous.
nextFieldId = fields[index - 1].id
} else if (previous && index === 0) {
// If we want to select the previous field and we already are the first index
// we just select the primary.
nextFieldId = primary.id
}
}
if (nextFieldId === -1) {
return
}
const current = this.$refs['row-' + row.id + '-field-' + field.id]
const next = this.$refs['row-' + row.id + '-field-' + nextFieldId]
if (next.length === 0 || current.length === 0) {
return
}
current[0].unselect()
next[0].select()
},
},
}
</script>
@@ -1,5 +1,5 @@
<template>
<div class="grid-view-column" style="width: 200px;" @click="select($event)">
<div class="grid-view-column" style="width: 200px;" @click="select()">
<component
:is="getFieldComponent(field.type)"
ref="column"
@@ -86,7 +86,7 @@ export default {
* Method that is called when a user clicks on the grid field. It wil
* @TODO improve speed somehow, maybe with the fastclick library.
*/
select(event) {
select() {
const timestamp = new Date().getTime()
if (this.selected) {
@@ -122,6 +122,25 @@ export default {
}
}
document.body.addEventListener('click', this.$el.clickOutsideEvent)
// If the tab key is pressed want to select the next field. This is however out
// of the scope of the component so we emit the selectNext event that the
// GridView can handle.
this.$el.tabPressedEvent = (event) => {
if (event.keyCode !== 9) {
return
}
event.preventDefault()
this.$emit(event.shiftKey ? 'selectPrevious' : 'selectNext')
}
document.body.addEventListener('keydown', this.$el.tabPressedEvent)
// Emit the selected event so that the parent component can take an action like
// making sure that the element fits in the viewport.
this.$emit('selected', {
component: this,
})
}
this.clickTimestamp = timestamp
@@ -132,6 +151,7 @@ export default {
this.selected = false
})
document.body.removeEventListener('click', this.$el.clickOutsideEvent)
document.body.removeEventListener('keydown', this.$el.tabPressedEvent)
},
},
}
@@ -26,6 +26,12 @@ export default {
*/
select() {
this.$el.keydownEvent = (event) => {
// If the tab key is pressed we don't want to do anything because the
// GridViewField component will select the next field.
if (event.keyCode === 9) {
return
}
// If the enter key is pressed.
if (event.keyCode === 13) {
if (this.editing) {