diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index a42b38a6d..a7cdde344 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -131,9 +131,7 @@ async function fetch(data) { const rows = keyData ? data[keyData] : data; resetData(rows); emit('onFetch', rows); - if ($props.insertOnLoad) { - await insert(); - } + $props.insertOnLoad && await insert(); return rows; } @@ -231,7 +229,7 @@ async function insert(pushData = { ...$props.dataRequired, ...$props.dataDefault const lastRow = formData.value.at(-1); const isLastRowEmpty = lastRow ? isRowEmpty(lastRow) : false; - if (formData.value.length > 0 && isLastRowEmpty) return; + if (formData.value.length && isLastRowEmpty) return; const $index = formData.value.length ? formData.value.at(-1).$index + 1 : 0; const nRow = Object.assign({ $index }, pushData); @@ -335,34 +333,22 @@ function removeIndexField(data) { async function handleTab(event) { event.preventDefault(); - + const { shiftKey, target } = event; const focusableSelector = `tbody tr td:not(:first-child) :is(a, button, input, textarea, select, details):not([disabled])`; const focusableElements = rowsContainer.value?.querySelectorAll(focusableSelector); - const currentIndex = Array.prototype.indexOf.call(focusableElements, event.target); - const isLast = event.target === focusableElements[focusableElements.length - 1]; + const currentIndex = Array.prototype.indexOf.call(focusableElements, target); + const index = shiftKey ? currentIndex - 1 : currentIndex + 1; + const isLast = target === focusableElements[focusableElements.length - 1]; const isFirst = currentIndex === 0; - if (event.shiftKey) { - if (!isFirst) { - focusableElements[currentIndex - 1].focus(); - } else { - const prevRow = event.target.closest('tr')?.previousElementSibling; - const lastFocusable = prevRow?.querySelector( - 'td:not(:first-child) :is(input, select, textarea):not([disabled])' - ); - lastFocusable?.focus(); - } - } else { - if (isLast) { - await insert(); - await nextTick(); - } else { - focusableElements[currentIndex + 1]?.focus(); - } + if ((shiftKey && !isFirst) || (!shiftKey && !isLast)) + focusableElements[index]?.focus(); + else if (isLast) { + await insert(); + await nextTick(); } } - async function reload(params) { const data = await vnPaginateRef.value.fetch(params); fetch(data);