Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust inVirtual logic(#227) #262

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,28 @@
...restProps
} = props;

// =============================== Item Key ===============================
const getKey = React.useCallback<GetKey<T>>(
(item: T) => {
if (typeof itemKey === 'function') {
return itemKey(item);
}
return item?.[itemKey as string];
},
[itemKey],
);

// ================================ Height ================================
const [setInstanceRef, collectHeight, heights, heightUpdatedMark] = useHeights(
getKey,
null,
null,
);

// ================================= MISC =================================
const useVirtual = !!(virtual !== false && height && itemHeight);
const inVirtual = useVirtual && data && (itemHeight * data.length > height || !!scrollWidth);
const containerHeight = React.useMemo(() => Object.values(heights.maps).reduce((total, curr) => total + curr, 0), [heights.id])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heights.maps 不需要 memo deps吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heights.maps 引用压根不会变,不过确实加上更好点

Fixed Show fixed Hide fixed
const inVirtual = useVirtual && data && (Math.max(itemHeight * data.length, containerHeight) > height || !!scrollWidth);
const isRTL = direction === 'rtl';

const mergedClassName = classNames(prefixCls, { [`${prefixCls}-rtl`]: isRTL }, className);
Expand All @@ -136,17 +155,6 @@
setScrollMoving(false);
};

// =============================== Item Key ===============================
const getKey = React.useCallback<GetKey<T>>(
(item: T) => {
if (typeof itemKey === 'function') {
return itemKey(item);
}
return item?.[itemKey as string];
},
[itemKey],
);

const sharedConfig: SharedConfig<T> = {
getKey,
};
Expand Down Expand Up @@ -176,13 +184,6 @@
const [diffItem] = useDiffItem(mergedData, getKey);
diffItemRef.current = diffItem;

// ================================ Height ================================
const [setInstanceRef, collectHeight, heights, heightUpdatedMark] = useHeights(
getKey,
null,
null,
);

// ========================== Visible Calculation =========================
const {
scrollHeight,
Expand Down Expand Up @@ -306,7 +307,9 @@
// ================================ Scroll ================================
const getVirtualScrollInfo = () => ({
x: isRTL ? -offsetLeft : offsetLeft,
maxScrollWidth: !!scrollWidth ? scrollWidth - size.width : 0,
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尽量避免给测试用的打孔,这个去掉吧。测试换种方式测

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y: offsetTop,
maxScrollHeight,
});

const lastVirtualScrollInfoRef = useRef(getVirtualScrollInfo());
Expand Down Expand Up @@ -354,7 +357,7 @@

const keepInHorizontalRange = (nextOffsetLeft: number) => {
let tmpOffsetLeft = nextOffsetLeft;
const max = scrollWidth - size.width;
const max = !!scrollWidth ? scrollWidth - size.width : 0;
tmpOffsetLeft = Math.max(tmpOffsetLeft, 0);
tmpOffsetLeft = Math.min(tmpOffsetLeft, max);

Expand Down
Loading