From 81f7b8aa7064f01be4068e9ec2d245cb4f35ec91 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 14 Aug 2024 10:02:45 +0200 Subject: [PATCH] feat: form focus --- src/boot/qformEnterEvent.js | 27 +++++++++++++++++++++++++++ src/boot/qformFocus.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/boot/qformEnterEvent.js create mode 100644 src/boot/qformFocus.js diff --git a/src/boot/qformEnterEvent.js b/src/boot/qformEnterEvent.js new file mode 100644 index 000000000..c6ce6a7b8 --- /dev/null +++ b/src/boot/qformEnterEvent.js @@ -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(); + } + }); + } + }, +}; diff --git a/src/boot/qformFocus.js b/src/boot/qformFocus.js new file mode 100644 index 000000000..9a6a4b8fe --- /dev/null +++ b/src/boot/qformFocus.js @@ -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, + }); + }, + }, +};