Skip to content

Commit ddf436f

Browse files
EmilyyyLiu刘欢
andauthored
feat: ScrollBar adds a showScrollBar field to determine if the scrollbar does not need to be hidden (#300)
* feat:ScrollBar增加showScrollBar字段,用于判断是否隐藏滚动条 * feat:调整新增字段默认值和判断位置 * feat:修改判断内容 * feat: showScrollBar: boolean | 'optional' --------- Co-authored-by: 刘欢 <[email protected]>
1 parent c2ce7ce commit ddf436f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/List.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface ListProps<T> extends Omit<React.HTMLAttributes<any>, 'children'
7272
verticalScrollBar?: React.CSSProperties;
7373
verticalScrollBarThumb?: React.CSSProperties;
7474
};
75-
75+
showScrollBar?: boolean | 'optional';
7676
onScroll?: React.UIEventHandler<HTMLElement>;
7777

7878
/**
@@ -112,6 +112,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
112112
innerProps,
113113
extraRender,
114114
styles,
115+
showScrollBar = 'optional',
115116
...restProps
116117
} = props;
117118

@@ -640,6 +641,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
640641
containerSize={size.height}
641642
style={styles?.verticalScrollBar}
642643
thumbStyle={styles?.verticalScrollBarThumb}
644+
showScrollBar={showScrollBar}
643645
/>
644646
)}
645647

@@ -658,6 +660,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
658660
horizontal
659661
style={styles?.horizontalScrollBar}
660662
thumbStyle={styles?.horizontalScrollBarThumb}
663+
showScrollBar={showScrollBar}
661664
/>
662665
)}
663666
</div>

src/ScrollBar.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ScrollBarProps {
1818
thumbStyle?: React.CSSProperties;
1919
spinSize: number;
2020
containerSize: number;
21+
showScrollBar?: boolean | 'optional';
2122
}
2223

2324
export interface ScrollBarRef {
@@ -38,6 +39,7 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
3839
containerSize,
3940
style,
4041
thumbStyle: propsThumbStyle,
42+
showScrollBar,
4143
} = props;
4244

4345
const [dragging, setDragging] = React.useState(false);
@@ -51,13 +53,13 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
5153
const thumbRef = React.useRef<HTMLDivElement>();
5254

5355
// ======================= Visible ========================
54-
const [visible, setVisible] = React.useState(false);
56+
const [visible, setVisible] = React.useState(showScrollBar);
5557
const visibleTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
5658

5759
const delayHidden = () => {
60+
if (showScrollBar === true || showScrollBar === false) return;
5861
clearTimeout(visibleTimeoutRef.current);
5962
setVisible(true);
60-
6163
visibleTimeoutRef.current = setTimeout(() => {
6264
setVisible(false);
6365
}, 3000);

tests/scroll.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,25 @@ describe('List.Scroll', () => {
233233
expect(wrapper.find('ul').instance().scrollTop > 10).toBeTruthy();
234234
});
235235

236+
it('should show scrollbar when element has showScrollBar prop set to true', () => {
237+
jest.useFakeTimers();
238+
const listRef = React.createRef();
239+
const { container } = genList(
240+
{
241+
itemHeight: 20,
242+
height: 100,
243+
data: genData(100),
244+
ref: listRef,
245+
showScrollBar: true,
246+
},
247+
render,
248+
);
249+
act(() => {
250+
jest.runAllTimers();
251+
});
252+
const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
253+
expect(scrollbarElement).not.toBeNull();
254+
});
236255
describe('not show scrollbar when disabled virtual', () => {
237256
[
238257
{ name: '!virtual', props: { virtual: false } },

0 commit comments

Comments
 (0)