Skip to content

Commit 34ec0e7

Browse files
NameWjpwangjinping
andauthored
fix: adjust inVirtual logic(#227) (#262)
* fix: adjust inVirtual logic(#227) * feat: support detection size(#256) * fix: check whether scrollWidth exist(#261) * fix: fix maxScrollHeight negative * test: add scrollbar thumb test * fix: add annotation --------- Co-authored-by: wangjinping <[email protected]>
1 parent e8e9aeb commit 34ec0e7

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

src/List.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,28 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
113113
...restProps
114114
} = props;
115115

116+
// =============================== Item Key ===============================
117+
const getKey = React.useCallback<GetKey<T>>(
118+
(item: T) => {
119+
if (typeof itemKey === 'function') {
120+
return itemKey(item);
121+
}
122+
return item?.[itemKey as string];
123+
},
124+
[itemKey],
125+
);
126+
127+
// ================================ Height ================================
128+
const [setInstanceRef, collectHeight, heights, heightUpdatedMark] = useHeights(
129+
getKey,
130+
null,
131+
null,
132+
);
133+
116134
// ================================= MISC =================================
117135
const useVirtual = !!(virtual !== false && height && itemHeight);
118-
const inVirtual = useVirtual && data && (itemHeight * data.length > height || !!scrollWidth);
136+
const containerHeight = React.useMemo(() => Object.values(heights.maps).reduce((total, curr) => total + curr, 0), [heights.id, heights.maps]);
137+
const inVirtual = useVirtual && data && (Math.max(itemHeight * data.length, containerHeight) > height || !!scrollWidth);
119138
const isRTL = direction === 'rtl';
120139

121140
const mergedClassName = classNames(prefixCls, { [`${prefixCls}-rtl`]: isRTL }, className);
@@ -136,17 +155,6 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
136155
setScrollMoving(false);
137156
};
138157

139-
// =============================== Item Key ===============================
140-
const getKey = React.useCallback<GetKey<T>>(
141-
(item: T) => {
142-
if (typeof itemKey === 'function') {
143-
return itemKey(item);
144-
}
145-
return item?.[itemKey as string];
146-
},
147-
[itemKey],
148-
);
149-
150158
const sharedConfig: SharedConfig<T> = {
151159
getKey,
152160
};
@@ -176,13 +184,6 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
176184
const [diffItem] = useDiffItem(mergedData, getKey);
177185
diffItemRef.current = diffItem;
178186

179-
// ================================ Height ================================
180-
const [setInstanceRef, collectHeight, heights, heightUpdatedMark] = useHeights(
181-
getKey,
182-
null,
183-
null,
184-
);
185-
186187
// ========================== Visible Calculation =========================
187188
const {
188189
scrollHeight,
@@ -307,6 +308,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
307308
const getVirtualScrollInfo = () => ({
308309
x: isRTL ? -offsetLeft : offsetLeft,
309310
y: offsetTop,
311+
maxScrollWidth: !!scrollWidth ? scrollWidth - size.width : 0,
312+
maxScrollHeight: scrollHeight > height ? maxScrollHeight : 0,
310313
});
311314

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

355358
const keepInHorizontalRange = (nextOffsetLeft: number) => {
356359
let tmpOffsetLeft = nextOffsetLeft;
357-
const max = scrollWidth - size.width;
360+
const max = !!scrollWidth ? scrollWidth - size.width : 0;
358361
tmpOffsetLeft = Math.max(tmpOffsetLeft, 0);
359362
tmpOffsetLeft = Math.min(tmpOffsetLeft, max);
360363

tests/scroll.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,25 @@ describe('List.Scroll', () => {
473473
height: `20px`,
474474
});
475475
});
476+
477+
it('show scrollbar when actual height is larger than container height', async () => {
478+
jest.useRealTimers();
479+
const { container } = genList(
480+
// set itemHeight * data.length < height, but sum of actual height > height
481+
{
482+
itemHeight: 8,
483+
height: 100,
484+
data: genData(10)
485+
},
486+
render,
487+
);
488+
489+
await act(async () => {
490+
await new Promise((resolve) => {
491+
setTimeout(resolve, 10);
492+
});
493+
});
494+
495+
expect(container.querySelector('.rc-virtual-list-scrollbar-thumb')).toBeVisible()
496+
});
476497
});

tests/scrollWidth.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ describe('List.scrollWidth', () => {
132132
width: '20px',
133133
});
134134

135-
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 900, y: 0 });
136-
expect(listRef.current.getScrollInfo()).toEqual({ x: 900, y: 0 });
135+
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 900, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
136+
expect(listRef.current.getScrollInfo()).toEqual({ x: 900, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
137137
});
138138

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

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

175175
it('shift wheel', async () => {
@@ -188,7 +188,7 @@ describe('List.scrollWidth', () => {
188188
deltaY: 123,
189189
shiftKey: true,
190190
});
191-
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
191+
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0, maxScrollWidth: 900, maxScrollHeight: 1900 });
192192
});
193193
});
194194

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

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

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

213213
it('support extraRender', async () => {

0 commit comments

Comments
 (0)