Skip to content

Commit

Permalink
fix(table): keep position after list updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jpntex committed Oct 22, 2024
1 parent 52928a4 commit 0fa8fb6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-pillows-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@indielayer/ui": patch
---

fix(table): keep position after list updates
25 changes: 19 additions & 6 deletions packages/ui/docs/pages/component/table/virtual.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { type TableHeader } from '@indielayer/ui'
import { computed, ref } from 'vue'
const headers: TableHeader[] = [
{ text: '#', value: 'id', sortable: true, align: 'center' },
{ text: 'Title', value: 'title' },
{ text: 'Description', value: 'description' },
{ text: 'Published', value: 'published' },
{ text: '#', value: 'id', sortable: true, align: 'center', width: 50 },
{ text: 'Title', value: 'title', width: 120, truncate: true },
{ text: 'Description', value: 'description', width: 240, truncate: true },
{ text: 'Published', value: 'published', width: 140, truncate: true },
{ text: 'Status', value: 'status' },
{ value: 'action', align: 'right' },
]
const sort = ref([])
Expand Down Expand Up @@ -43,11 +44,23 @@ const items = ref(generateItems(1000))
<x-card>
<x-table
v-model:sort="sort"
class="!h-80"
:headers="headers"
:items="itemsSorted"
class="!h-80"
fixed
virtual-list
:virtual-list-item-height="54"
/>
>
<template #item-action="{ item }">
<x-button
icon="x"
light
outlined
color="error"
size="xs"
@click="items = items.filter((i) => i.id !== item.id)"
/>
</template>
</x-table>
</x-card>
</template>
13 changes: 8 additions & 5 deletions packages/ui/src/composables/useVirtualList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function createCalculateRange<T>(type: 'horizontal' | 'vertical', overscan: numb

function createGetDistance<T>(itemSize: UseVirtualListItemSize, source: UseVirtualListResources<T>['source']) {
return (index: number) => {

if (typeof itemSize === 'number') {
const size = index * itemSize

Expand All @@ -210,10 +211,9 @@ function createGetDistance<T>(itemSize: UseVirtualListItemSize, source: UseVirtu
}
}

function useWatchForSizes<T>(size: UseVirtualElementSizes, list: MaybeRef<T[]>, containerRef: Ref<HTMLElement | null>, calculateRange: () => void, scrollTo: (index: number) => void) {
function useWatchForSizes<T>(size: UseVirtualElementSizes, list: MaybeRef<T[]>, containerRef: Ref<HTMLElement | null>, calculateRange: () => void) {
watch([size.width, size.height, list, containerRef], () => {
calculateRange()
scrollTo(0)
})
}

Expand Down Expand Up @@ -263,14 +263,17 @@ function useVerticalVirtualList<T>(options: UseVirtualListOptions, list: MaybeRe

const scrollTo = createScrollTo('vertical', calculateRange, getDistanceTop, containerRef)

useWatchForSizes(size, list, containerRef, calculateRange, scrollTo)
useWatchForSizes(size, source, containerRef, calculateRange)

const wrapperProps = computed(() => {
const total = totalHeight.value + topOffset + bottomOffset
const offTop = offsetTop.value > total ? total : offsetTop.value

return {
style: {
width: '100%',
height: `${totalHeight.value - offsetTop.value + topOffset + bottomOffset}px`,
marginTop: `${offsetTop.value}px`,
height: `${totalHeight.value - offTop + topOffset + bottomOffset}px`,
marginTop: `${offTop}px`,
},
}
})
Expand Down

0 comments on commit 0fa8fb6

Please sign in to comment.