Skip to content

Commit

Permalink
fix: VirtualList scale scrollbar not work as expect (#278)
Browse files Browse the repository at this point in the history
* chore: fix scale

* test: fix test case
  • Loading branch information
zombieJ authored Jun 13, 2024
1 parent 645b5a0 commit 4c492cf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {

const onHolderResize: ResizeObserverProps['onResize'] = (sizeInfo) => {
setSize({
width: sizeInfo.width || sizeInfo.offsetWidth,
height: sizeInfo.height || sizeInfo.offsetHeight,
width: sizeInfo.offsetWidth,
height: sizeInfo.offsetHeight,
});
};

Expand Down
8 changes: 5 additions & 3 deletions src/ScrollBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import raf from 'rc-util/lib/raf';
import * as React from 'react';

export type ScrollBarDirectionType = 'ltr' | 'rtl';

Expand Down Expand Up @@ -84,7 +84,7 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
}, [scrollOffset, enableScrollRange, enableOffsetRange]);

// ====================== Container =======================
const onContainerMouseDown: React.MouseEventHandler = e => {
const onContainerMouseDown: React.MouseEventHandler = (e) => {
e.stopPropagation();
e.preventDefault();
};
Expand Down Expand Up @@ -142,8 +142,10 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
} = stateRef.current;
raf.cancel(moveRafId);

const scale = containerSize / scrollbarRef.current.getBoundingClientRect().height;

if (stateDragging) {
const offset = getPageXY(e, horizontal) - statePageY;
const offset = (getPageXY(e, horizontal) - statePageY) * scale;
let newTop = stateStartTop;

if (!isLTR && horizontal) {
Expand Down
27 changes: 21 additions & 6 deletions tests/scrollWidth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@ describe('List.scrollWidth', () => {
let mockMouseEvent;
let pageX: number;

const holderHeight = 100;
let holderWidth = 100;

beforeAll(() => {
mockElement = spyElementPrototypes(HTMLElement, {
offsetHeight: {
get: () => ITEM_HEIGHT,
get() {
if (this.classList.contains('rc-virtual-list-holder')) {
return holderHeight;
}
return ITEM_HEIGHT;
},
},
offsetWidth: {
get() {
return holderWidth;
},
},
clientHeight: {
get: () => holderWidth,
get() {
return holderWidth;
},
},
getBoundingClientRect() {
return {
width: holderWidth,
height: holderHeight,
};
},
getBoundingClientRect: () => ({
width: holderWidth,
height: 100,
}),
});

mockMouseEvent = spyElementPrototypes(MouseEvent, {
Expand Down

0 comments on commit 4c492cf

Please sign in to comment.