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
Show file tree
Hide file tree
Changes from 5 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 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
...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, heights.maps]);
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 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
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 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
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 @@ -307,6 +308,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
const getVirtualScrollInfo = () => ({
x: isRTL ? -offsetLeft : offsetLeft,
y: offsetTop,
maxScrollWidth: !!scrollWidth ? scrollWidth - size.width : 0,
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.

maxScrollHeight: scrollHeight > height ? maxScrollHeight : 0,
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
Contributor Author

@NameWjp NameWjp Apr 5, 2024

Choose a reason for hiding this comment

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

maxScrollWidth:scrollWidth 可能不传,所以这里加了个判断。
maxScrollHeight:这里 scrollHeight 是取的 fillerInnerRef.current?.offsetHeight(感觉这里应该取容器的 scrollHeight),会出现 scrollHeight 小于容器 height 的情况,所以要做个判断

Copy link
Member

Choose a reason for hiding this comment

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

这俩属性和bugfix无关,我发PR去掉先。

Copy link
Member

Choose a reason for hiding this comment

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

});

const lastVirtualScrollInfoRef = useRef(getVirtualScrollInfo());
Expand Down Expand Up @@ -354,7 +357,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {

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
20 changes: 20 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,24 @@ describe('List.Scroll', () => {
height: `20px`,
});
});

it('show scrollbar when actual height is larger than container height', async () => {
jest.useRealTimers();
const { container } = genList(
{
itemHeight: 8,
height: 100,
data: genData(10)
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
},
render,
);

await act(async () => {
await new Promise((resolve) => {
setTimeout(resolve, 10);
});
});

expect(container.querySelector('.rc-virtual-list-scrollbar-thumb')).toBeVisible()
});
});
14 changes: 7 additions & 7 deletions tests/scrollWidth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ describe('List.scrollWidth', () => {
width: '20px',
});

expect(onVirtualScroll).toHaveBeenCalledWith({ x: 900, y: 0 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 900, y: 0 });
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 900, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 900, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
});

it('wheel', async () => {
Expand All @@ -151,7 +151,7 @@ describe('List.scrollWidth', () => {
fireEvent.wheel(container.querySelector('.rc-virtual-list-holder')!, {
deltaX: 123,
});
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
});

it('trigger event when less count', async () => {
Expand All @@ -169,7 +169,7 @@ describe('List.scrollWidth', () => {
fireEvent.wheel(container.querySelector('.rc-virtual-list-holder')!, {
deltaX: 123,
});
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0, maxScrollWidth: 900, maxScrollHeight: 0 });
});

it('shift wheel', async () => {
Expand All @@ -188,7 +188,7 @@ describe('List.scrollWidth', () => {
deltaY: 123,
shiftKey: true,
});
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
});
});

Expand All @@ -204,10 +204,10 @@ describe('List.scrollWidth', () => {
});

listRef.current.scrollTo({ left: 135 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 135, y: 0 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 135, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });

listRef.current.scrollTo({ left: -99 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 0, y: 0 });
expect(listRef.current.getScrollInfo()).toEqual({ x: 0, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
});

it('support extraRender', async () => {
Expand Down
Loading