Skip to content

Commit b4b13c4

Browse files
BoyYangzai洋
andauthored
feat: add List ScrollBar direction (#201)
* feat: add List ScrollBar direction * fix: code style * chore: add test case --------- Co-authored-by: 洋 <[email protected]>
1 parent d1c9231 commit b4b13c4

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/List.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useRef, useState } from 'react';
33
import classNames from 'classnames';
44
import Filler from './Filler';
55
import type { InnerProps } from './Filler';
6+
import type { ScrollBarDirectionType } from './ScrollBar';
67
import ScrollBar from './ScrollBar';
78
import type { RenderFunc, SharedConfig, GetKey } from './interface';
89
import useChildren from './hooks/useChildren';
@@ -50,6 +51,7 @@ export interface ListProps<T> extends Omit<React.HTMLAttributes<any>, 'children'
5051
component?: string | React.FC<any> | React.ComponentClass<any>;
5152
/** Set `false` will always use real scroll instead of virtual one */
5253
virtual?: boolean;
54+
direction?: ScrollBarDirectionType;
5355

5456
onScroll?: React.UIEventHandler<HTMLElement>;
5557
/** Trigger when render list item changed */
@@ -71,6 +73,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
7173
children,
7274
itemKey,
7375
virtual,
76+
direction,
7477
component: Component = 'div',
7578
onScroll,
7679
onVisibleChange,
@@ -373,6 +376,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
373376
height={height}
374377
scrollHeight={scrollHeight}
375378
count={mergedData.length}
379+
direction={direction}
376380
onScroll={onScrollBar}
377381
onStartMove={() => {
378382
setScrollMoving(true);

src/ScrollBar.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import raf from 'rc-util/lib/raf';
44

55
const MIN_SIZE = 20;
66

7+
export type ScrollBarDirectionType = 'ltr' | 'rtl';
8+
79
export interface ScrollBarProps {
810
prefixCls: string;
911
scrollTop: number;
1012
scrollHeight: number;
1113
height: number;
1214
count: number;
15+
direction?: ScrollBarDirectionType;
1316
onScroll: (scrollTop: number) => void;
1417
onStartMove: () => void;
1518
onStopMove: () => void;
@@ -90,7 +93,7 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
9093
window.removeEventListener('mouseup', this.onMouseUp);
9194

9295
this.scrollbarRef.current?.removeEventListener('touchstart', this.onScrollbarTouchStart);
93-
96+
9497
if (this.thumbRef.current) {
9598
this.thumbRef.current.removeEventListener('touchstart', this.onMouseDown);
9699
this.thumbRef.current.removeEventListener('touchmove', this.onMouseMove);
@@ -185,12 +188,20 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
185188
// ====================== Render =======================
186189
render() {
187190
const { dragging, visible } = this.state;
188-
const { prefixCls } = this.props;
191+
const { prefixCls, direction } = this.props;
189192
const spinHeight = this.getSpinHeight();
190193
const top = this.getTop();
191194

192195
const canScroll = this.showScroll();
193196
const mergedVisible = canScroll && visible;
197+
const scrollBarDirection =
198+
direction === 'rtl'
199+
? {
200+
left: 0,
201+
}
202+
: {
203+
right: 0,
204+
};
194205

195206
return (
196207
<div
@@ -202,7 +213,7 @@ export default class ScrollBar extends React.Component<ScrollBarProps, ScrollBar
202213
width: 8,
203214
top: 0,
204215
bottom: 0,
205-
right: 0,
216+
...scrollBarDirection,
206217
position: 'absolute',
207218
display: mergedVisible ? null : 'none',
208219
}}

tests/scroll.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,24 @@ describe('List.Scroll', () => {
286286
expect(wrapper.find('li').length).toBeLessThan(10);
287287
});
288288
});
289+
290+
it('scrollbar should be left position with rtl', () => {
291+
jest.useFakeTimers();
292+
const listRef = React.createRef();
293+
const wrapper = genList({
294+
itemHeight: 20,
295+
height: 100,
296+
data: genData(100),
297+
ref: listRef,
298+
direction: 'rtl',
299+
});
300+
jest.runAllTimers();
301+
302+
listRef.current.scrollTo(null);
303+
expect(wrapper.find('.rc-virtual-list-scrollbar-thumb').props().style.display).not.toEqual(
304+
'none',
305+
);
306+
expect(wrapper.find('.rc-virtual-list-scrollbar').props().style.left).toEqual(0);
307+
jest.useRealTimers();
308+
});
289309
});

0 commit comments

Comments
 (0)