Skip to content

Commit

Permalink
feat(market): scroll to top when page update, fix #170
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 23, 2023
1 parent 2887039 commit 4563aa1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/market/client/components/list.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<template>
<div class="market-list">
<div class="market-list" ref="root">
<slot name="header" v-bind="{ all, packages, hasFilter: hasFilter(modelValue) }"></slot>
<el-pagination
class="pagination"
background
v-model:current-page="page"
:pager-count="5"
:page-size="limit"
:total="packages.length"
layout="prev, pager, next"
/>
<div class="market-container">
<market-package
v-for="data in pages[page - 1]"
Expand Down Expand Up @@ -28,7 +37,7 @@

<script lang="ts" setup>
import { computed, inject, ref } from 'vue'
import { computed, inject, ref, watch } from 'vue'
import { SearchObject } from '@koishijs/registry'
import { getSorted, getFiltered, hasFilter, kConfig } from '../utils'
import MarketPackage from './package.vue'
Expand All @@ -40,7 +49,7 @@ const props = defineProps<{
gravatar?: string,
}>()
const emit = defineEmits(['update:modelValue'])
const emit = defineEmits(['update:modelValue', 'update:page'])
const config = inject(kConfig, {})
Expand All @@ -60,6 +69,8 @@ const limit = computed(() => {
const page = ref(1)
watch(page, (page) => emit('update:page', page))
const pages = computed(() => {
const result: SearchObject[][] = []
for (let i = 0; i < packages.value.length; i += limit.value) {
Expand Down
11 changes: 9 additions & 2 deletions plugins/market/client/components/market.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
</div>
</div>

<el-scrollbar v-else-if="store.market.total">
<el-scrollbar ref="root" v-else-if="store.market.total">
<market-list
v-model="words"
:data="data"
:gravatar="store.market.gravatar">
:gravatar="store.market.gravatar"
@update:page="scrollToTop">
<template #header="{ hasFilter, all, packages }">
<market-search v-model="words"></market-search>
<div class="market-hint">
Expand Down Expand Up @@ -64,6 +65,8 @@ provide(kConfig, {
installed: global.static ? undefined : installed,
})
const root = ref()
const words = ref<string[]>([''])
const prompt = computed(() => words.value.filter(w => w).join(' '))
Expand Down Expand Up @@ -92,6 +95,10 @@ function handleClick(data: SearchObject) {
active.value = data.package.name
}
function scrollToTop() {
root.value?.scrollTo(0, 0)
}
</script>

<style lang="scss">
Expand Down

0 comments on commit 4563aa1

Please sign in to comment.