Skip to content

Commit

Permalink
fix: select cannot scroll #4971
Browse files Browse the repository at this point in the history
close #4971
  • Loading branch information
tangjinzhou committed Dec 4, 2021
1 parent 75be018 commit 698b684
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
21 changes: 17 additions & 4 deletions components/vc-virtual-list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ const List = defineComponent({
}

// ================================ Height ================================
const [setInstance, collectHeight, heights] = useHeights(getKey, null, null);
const [setInstance, collectHeight, heights, updatedMark] = useHeights(
mergedData,
getKey,
null,
null,
);

const calRes = ref<{
scrollHeight?: number;
Expand All @@ -157,9 +162,17 @@ const List = defineComponent({
offset?: number;
}>({});
watch(
[inVirtual, useVirtual, () => state.scrollTop, mergedData, heights, () => props.height],
[
inVirtual,
useVirtual,
() => state.scrollTop,
mergedData,
updatedMark,
heights,
() => props.height,
],
() => {
nextTick(() => {
setTimeout(() => {
if (!useVirtual.value) {
calRes.value = {
scrollHeight: undefined,
Expand Down Expand Up @@ -191,7 +204,7 @@ const List = defineComponent({
const item = data[i];
const key = getKey(item);

const cacheHeight = heights[key];
const cacheHeight = heights.value[key];
const currentItemBottom =
itemTop + (cacheHeight === undefined ? props.itemHeight! : cacheHeight);

Expand Down
23 changes: 15 additions & 8 deletions components/vc-virtual-list/hooks/useHeights.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { VNodeProps } from 'vue';
import { reactive } from 'vue';
import type { VNodeProps, ComputedRef, Ref } from 'vue';
import { shallowRef, watch, ref } from 'vue';
import type { GetKey } from '../interface';

type CacheMap = Record<string, number>;
type CacheMap = Ref<Record<string, number>>;

export default function useHeights<T>(
mergedData: ComputedRef<any[]>,
getKey: GetKey<T>,
onItemAdd?: ((item: T) => void) | null,
onItemRemove?: ((item: T) => void) | null,
): [(item: T, instance: HTMLElement) => void, () => void, CacheMap] {
): [(item: T, instance: HTMLElement) => void, () => void, CacheMap, Ref<Symbol>] {
const instance = new Map<VNodeProps['key'], HTMLElement>();
const heights = reactive({});
const heights = shallowRef({});
const updatedMark = ref(Symbol('update'));
watch(mergedData, () => {
heights.value = {};
updatedMark.value = Symbol('update');
});
let heightUpdateId = 0;
function collectHeight() {
heightUpdateId += 1;
Expand All @@ -22,9 +28,10 @@ export default function useHeights<T>(
instance.forEach((element, key) => {
if (element && element.offsetParent) {
const { offsetHeight } = element;
if (heights[key!] !== offsetHeight) {
if (heights.value[key!] !== offsetHeight) {
//changed = true;
heights[key!] = element.offsetHeight;
updatedMark.value = Symbol('update');
heights.value[key!] = element.offsetHeight;
}
}
});
Expand Down Expand Up @@ -52,5 +59,5 @@ export default function useHeights<T>(
}
}

return [setInstance, collectHeight, heights];
return [setInstance, collectHeight, heights, updatedMark];
}
4 changes: 2 additions & 2 deletions components/vc-virtual-list/hooks/useScrollTo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { GetKey } from '../interface';
export default function useScrollTo(
containerRef: Ref<Element | undefined>,
mergedData: ComputedRef<any[]>,
heights: Data,
heights: Ref<Data>,
props,
getKey: GetKey,
collectHeight: () => void,
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function useScrollTo(
for (let i = 0; i <= maxLen; i += 1) {
const key = getKey(data[i]);
itemTop = stackTop;
const cacheHeight = heights[key!];
const cacheHeight = heights.value[key!];
itemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight);

stackTop = itemBottom;
Expand Down

0 comments on commit 698b684

Please sign in to comment.