Skip to content

Commit 698b684

Browse files
committed
fix: select cannot scroll #4971
close #4971
1 parent 75be018 commit 698b684

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

components/vc-virtual-list/List.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ const List = defineComponent({
148148
}
149149

150150
// ================================ Height ================================
151-
const [setInstance, collectHeight, heights] = useHeights(getKey, null, null);
151+
const [setInstance, collectHeight, heights, updatedMark] = useHeights(
152+
mergedData,
153+
getKey,
154+
null,
155+
null,
156+
);
152157

153158
const calRes = ref<{
154159
scrollHeight?: number;
@@ -157,9 +162,17 @@ const List = defineComponent({
157162
offset?: number;
158163
}>({});
159164
watch(
160-
[inVirtual, useVirtual, () => state.scrollTop, mergedData, heights, () => props.height],
165+
[
166+
inVirtual,
167+
useVirtual,
168+
() => state.scrollTop,
169+
mergedData,
170+
updatedMark,
171+
heights,
172+
() => props.height,
173+
],
161174
() => {
162-
nextTick(() => {
175+
setTimeout(() => {
163176
if (!useVirtual.value) {
164177
calRes.value = {
165178
scrollHeight: undefined,
@@ -191,7 +204,7 @@ const List = defineComponent({
191204
const item = data[i];
192205
const key = getKey(item);
193206

194-
const cacheHeight = heights[key];
207+
const cacheHeight = heights.value[key];
195208
const currentItemBottom =
196209
itemTop + (cacheHeight === undefined ? props.itemHeight! : cacheHeight);
197210

components/vc-virtual-list/hooks/useHeights.tsx

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import type { VNodeProps } from 'vue';
2-
import { reactive } from 'vue';
1+
import type { VNodeProps, ComputedRef, Ref } from 'vue';
2+
import { shallowRef, watch, ref } from 'vue';
33
import type { GetKey } from '../interface';
44

5-
type CacheMap = Record<string, number>;
5+
type CacheMap = Ref<Record<string, number>>;
66

77
export default function useHeights<T>(
8+
mergedData: ComputedRef<any[]>,
89
getKey: GetKey<T>,
910
onItemAdd?: ((item: T) => void) | null,
1011
onItemRemove?: ((item: T) => void) | null,
11-
): [(item: T, instance: HTMLElement) => void, () => void, CacheMap] {
12+
): [(item: T, instance: HTMLElement) => void, () => void, CacheMap, Ref<Symbol>] {
1213
const instance = new Map<VNodeProps['key'], HTMLElement>();
13-
const heights = reactive({});
14+
const heights = shallowRef({});
15+
const updatedMark = ref(Symbol('update'));
16+
watch(mergedData, () => {
17+
heights.value = {};
18+
updatedMark.value = Symbol('update');
19+
});
1420
let heightUpdateId = 0;
1521
function collectHeight() {
1622
heightUpdateId += 1;
@@ -22,9 +28,10 @@ export default function useHeights<T>(
2228
instance.forEach((element, key) => {
2329
if (element && element.offsetParent) {
2430
const { offsetHeight } = element;
25-
if (heights[key!] !== offsetHeight) {
31+
if (heights.value[key!] !== offsetHeight) {
2632
//changed = true;
27-
heights[key!] = element.offsetHeight;
33+
updatedMark.value = Symbol('update');
34+
heights.value[key!] = element.offsetHeight;
2835
}
2936
}
3037
});
@@ -52,5 +59,5 @@ export default function useHeights<T>(
5259
}
5360
}
5461

55-
return [setInstance, collectHeight, heights];
62+
return [setInstance, collectHeight, heights, updatedMark];
5663
}

components/vc-virtual-list/hooks/useScrollTo.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { GetKey } from '../interface';
77
export default function useScrollTo(
88
containerRef: Ref<Element | undefined>,
99
mergedData: ComputedRef<any[]>,
10-
heights: Data,
10+
heights: Ref<Data>,
1111
props,
1212
getKey: GetKey,
1313
collectHeight: () => void,
@@ -63,7 +63,7 @@ export default function useScrollTo(
6363
for (let i = 0; i <= maxLen; i += 1) {
6464
const key = getKey(data[i]);
6565
itemTop = stackTop;
66-
const cacheHeight = heights[key!];
66+
const cacheHeight = heights.value[key!];
6767
itemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight);
6868

6969
stackTop = itemBottom;

0 commit comments

Comments
 (0)