Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3cc7ec

Browse files
fix(useVirtualList): allow readonly arrays as input (#4504)
1 parent 69cedd2 commit f3cc7ec

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

packages/core/useVirtualList/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,19 @@ describe('useVirtualList, horizontal', () => {
112112
scrollTo(6)
113113
expect(list.value.map(i => i.data)).toEqual(['f', 'g'])
114114
})
115+
116+
it('allows both readonly and mutable arrays as input', () => {
117+
const mutableInput: string[] = ['a', 'b', 'c', 'd', 'e', 'f']
118+
const readonlyInput: readonly string[] = ['a', 'b', 'c', 'd', 'e', 'f']
119+
120+
const {
121+
list: readonlyList,
122+
} = useVirtualList(ref(readonlyInput), { itemHeight: () => 50, overscan: 1 })
123+
const {
124+
list: mutableList,
125+
} = useVirtualList(ref(mutableInput), { itemHeight: () => 50, overscan: 1 })
126+
127+
expect(readonlyList.value).toBeDefined()
128+
expect(mutableList.value).toBeDefined()
129+
})
115130
})

packages/core/useVirtualList/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface UseVirtualListReturn<T> {
6767
/**
6868
* Please consider using [`vue-virtual-scroller`](https://github.com/Akryum/vue-virtual-scroller) if you are looking for more features.
6969
*/
70-
export function useVirtualList<T = any>(list: MaybeRef<T[]>, options: UseVirtualListOptions): UseVirtualListReturn<T> {
70+
export function useVirtualList<T = any>(list: MaybeRef<readonly T[]>, options: UseVirtualListOptions): UseVirtualListReturn<T> {
7171
const { containerStyle, wrapperProps, scrollTo, calculateRange, currentList, containerRef } = 'itemHeight' in options
7272
? useVerticalVirtualList(options, list)
7373
: useHorizontalVirtualList(options, list)
@@ -96,7 +96,7 @@ interface UseVirtualElementSizes {
9696
type UseVirtualListArray<T> = UseVirtualListItem<T>[]
9797
type UseVirtualListRefArray<T> = Ref<UseVirtualListArray<T>>
9898

99-
type UseVirtualListSource<T> = Ref<T[]> | ShallowRef<T[]>
99+
type UseVirtualListSource<T> = Ref<readonly T[]> | ShallowRef<readonly T[]>
100100

101101
interface UseVirtualListState { start: number, end: number }
102102

@@ -110,7 +110,7 @@ interface UseVirtualListResources<T> {
110110
containerRef: UseVirtualListContainerRef
111111
}
112112

113-
function useVirtualListResources<T>(list: MaybeRef<T[]>): UseVirtualListResources<T> {
113+
function useVirtualListResources<T>(list: MaybeRef<readonly T[]>): UseVirtualListResources<T> {
114114
const containerRef = ref<HTMLElement | null>(null)
115115
const size = useElementSize(containerRef)
116116

@@ -201,7 +201,7 @@ function createGetDistance<T>(itemSize: UseVirtualListItemSize, source: UseVirtu
201201
}
202202
}
203203

204-
function useWatchForSizes<T>(size: UseVirtualElementSizes, list: MaybeRef<T[]>, containerRef: Ref<HTMLElement | null>, calculateRange: () => void) {
204+
function useWatchForSizes<T>(size: UseVirtualElementSizes, list: MaybeRef<readonly T[]>, containerRef: Ref<HTMLElement | null>, calculateRange: () => void) {
205205
watch([size.width, size.height, list, containerRef], () => {
206206
calculateRange()
207207
})
@@ -230,7 +230,7 @@ function createScrollTo<T>(type: 'horizontal' | 'vertical', calculateRange: () =
230230
}
231231
}
232232

233-
function useHorizontalVirtualList<T>(options: UseHorizontalVirtualListOptions, list: MaybeRef<T[]>) {
233+
function useHorizontalVirtualList<T>(options: UseHorizontalVirtualListOptions, list: MaybeRef<readonly T[]>) {
234234
const resources = useVirtualListResources(list)
235235
const { state, source, currentList, size, containerRef } = resources
236236
const containerStyle: StyleValue = { overflowX: 'auto' }
@@ -274,7 +274,7 @@ function useHorizontalVirtualList<T>(options: UseHorizontalVirtualListOptions, l
274274
}
275275
}
276276

277-
function useVerticalVirtualList<T>(options: UseVerticalVirtualListOptions, list: MaybeRef<T[]>) {
277+
function useVerticalVirtualList<T>(options: UseVerticalVirtualListOptions, list: MaybeRef<readonly T[]>) {
278278
const resources = useVirtualListResources(list)
279279

280280
const { state, source, currentList, size, containerRef } = resources

0 commit comments

Comments
 (0)