Skip to content

Commit 436b828

Browse files
authored
Merge pull request #128 from devforth/fix-page-input-validation
fix: add page input validation
2 parents 6c7a43f + 6a68fbf commit 436b828

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

adminforth/spa/src/components/ResourceListTable.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
<div
209209
contenteditable="true"
210210
class="min-w-10 outline-none inline-block w-auto min-w-10 py-1.5 px-3 text-sm text-center text-gray-700 border border-gray-300 dark:border-gray-700 dark:text-gray-400 dark:bg-gray-800 z-10"
211+
@keydown="onPageKeydown($event)"
211212
@input="page = parseInt($event.target.innerText) || ''"
212213
>
213214
{{ pageInput }}
@@ -271,7 +272,7 @@
271272
<script setup lang="ts">
272273
273274
274-
import { computed, onMounted, ref, watch, useTemplateRef, type Ref } from 'vue';
275+
import { computed, onMounted, ref, watch, useTemplateRef, nextTick, type Ref } from 'vue';
275276
import { callAdminForthApi } from '@/utils';
276277
import { useI18n } from 'vue-i18n';
277278
import ValueRenderer from '@/components/ValueRenderer.vue';
@@ -329,6 +330,14 @@ const to = computed(() => Math.min((page.value || 1) * props.pageSize, props.tot
329330
watch(() => page.value, (newPage) => {
330331
emits('update:page', newPage);
331332
});
333+
async function onPageKeydown(event) {
334+
// page input should accept only numbers, arrow keys and backspace
335+
if (['Enter', 'Space'].includes(event.code) ||
336+
(!['Backspace', 'ArrowRight', 'ArrowLeft'].includes(event.code)
337+
&& isNaN(String.fromCharCode(event.keyCode)))) {
338+
event.preventDefault();
339+
}
340+
}
332341
333342
watch(() => sort.value, (newSort) => {
334343
emits('update:sort', newSort);

0 commit comments

Comments
 (0)