fix(front): accidentally and surprisingly, the bug where entering a string in pagination led to page=Nan is crushed.

This commit is contained in:
upsiflu 2025-02-19 11:49:39 +01:00
parent a3e94d677a
commit 6bdae01dcd
1 changed files with 21 additions and 9 deletions

View File

@ -20,10 +20,11 @@ const page = defineModel<number>('page', {
validator: (value: number) => value > 0 validator: (value: number) => value > 0
}) })
const goTo = ref<number>(1) const goTo = ref<number | string>('' as const)
const range = (start: number, end: number) => Array.from({ length: end - start + 1 }, (_, i) => i + start) const range = (start: number, end: number) => Array.from({ length: end - start + 1 }, (_, i) => i + start)
/* Why? What? */
const renderPages = computed(() => { const renderPages = computed(() => {
const start = range(2, 5) const start = range(2, 5)
const end = range(pages - 4, pages - 1) const end = range(pages - 4, pages - 1)
@ -47,12 +48,25 @@ const { width } = useElementSize(pagination)
const isSmall = isMobileView(width) const isSmall = isMobileView(width)
const setPage = () => { const setPage = () => {
if (goTo.value == null) return if (goTo.value === '') return
page.value = Math.min(pages, Math.max(1, goTo.value)) page.value = pageFromInput(goTo.value)
} }
watch(page, (newPage) => { watch(goTo, (potentiallyWrongValue) =>
goTo.value = newPage goTo.value =
typeof potentiallyWrongValue === 'string' ? ''
: pageFromInput(potentiallyWrongValue)
)
const pageFromInput = (input: string | number): number =>
input === 'NaN' ? pageFromInput('')
: typeof input === 'string' ? pageFromInput(parseInt(input))
: Number.isNaN(input) ? 1
: Math.min(Math.max(1, input), pages)
/* When user changes page, the "GoTo" input should be emptied */
watch(page, (_) => {
goTo.value = ''
}) })
</script> </script>
@ -108,18 +122,16 @@ watch(page, (newPage) => {
</Button> </Button>
</li> </li>
</ul> </ul>
<!-- \d{1,100} -->
<!-- TODO: Move number input functionality into `Input` component -->
<div class="goto"> <div class="goto">
{{ t('vui.go-to') }} {{ t('vui.go-to') }}
<Input <Input
:placeholder="page.toString()" :placeholder="page.toString()"
@click.stop @click.stop
@keyup.enter="setPage" @keyup.enter="setPage"
@keydown="preventNonNumeric"
inputmode="numeric" inputmode="numeric"
pattern="[0-9]*" pattern="[0-9]*"
v-model="goTo" v-model.number="goTo"
/> />
</div> </div>
</nav> </nav>