0
0
Fork 0

feat: form focus

This commit is contained in:
Javier Segarra 2024-08-14 10:02:45 +02:00
parent c8fbef1754
commit 81f7b8aa70
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import { getCurrentInstance } from 'vue';
export default {
mounted: function () {
const vm = getCurrentInstance();
if (vm.type.name === 'QForm') {
const that = this;
this.$el.addEventListener('keyup', function (evt) {
if (evt.key === 'Enter') {
const input = evt.target;
if (input.type == 'textarea' && evt.shiftKey) {
evt.preventDefault();
let { selectionStart, selectionEnd } = input;
input.value =
input.value.substring(0, selectionStart) +
'\n' +
input.value.substring(selectionEnd);
selectionStart = selectionEnd = selectionStart + 1;
return;
}
evt.preventDefault();
that.onSubmit();
}
});
}
},
};

29
src/boot/qformFocus.js Normal file
View File

@ -0,0 +1,29 @@
import { getCurrentInstance } from 'vue';
export default {
mounted() {
const vm = getCurrentInstance();
if (vm.type.name === 'QForm') {
if (!['searchbarForm', 'filterPanelForm'].includes(this.$el?.id))
this.observeForInputs();
}
},
methods: {
observeForInputs() {
const observer = new MutationObserver((mutations, observerInstance) => {
const firstInput = this.$el.querySelector(
'input:not([disabled]):not([type="checkbox"], textarea:not([disabled]), [contenteditable]:not([disabled])'
);
if (firstInput) {
firstInput.focus();
observerInstance.disconnect();
}
});
observer.observe(this.$el, {
childList: true,
subtree: true,
});
},
},
};