Skip to content

feat: ScrollBar adds a showScrollBar field to determine if the scrollbar does not need to be hidden #300

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

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface ListProps<T> extends Omit<React.HTMLAttributes<any>, 'children'
verticalScrollBar?: React.CSSProperties;
verticalScrollBarThumb?: React.CSSProperties;
};

showScrollBar?: boolean | 'optional';
onScroll?: React.UIEventHandler<HTMLElement>;

/**
Expand Down Expand Up @@ -112,6 +112,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
innerProps,
extraRender,
styles,
showScrollBar = 'optional',
...restProps
} = props;

Expand Down Expand Up @@ -640,6 +641,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
containerSize={size.height}
style={styles?.verticalScrollBar}
thumbStyle={styles?.verticalScrollBarThumb}
showScrollBar={showScrollBar}
/>
)}

Expand All @@ -658,6 +660,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
horizontal
style={styles?.horizontalScrollBar}
thumbStyle={styles?.horizontalScrollBarThumb}
showScrollBar={showScrollBar}
/>
)}
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/ScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ScrollBarProps {
thumbStyle?: React.CSSProperties;
spinSize: number;
containerSize: number;
showScrollBar?: boolean | 'optional';
}

export interface ScrollBarRef {
Expand All @@ -38,6 +39,7 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
containerSize,
style,
thumbStyle: propsThumbStyle,
showScrollBar,
} = props;

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

// ======================= Visible ========================
const [visible, setVisible] = React.useState(false);
const [visible, setVisible] = React.useState(showScrollBar);
const visibleTimeoutRef = React.useRef<ReturnType<typeof setTimeout>>();

const delayHidden = () => {
if (showScrollBar === true || showScrollBar === false) return;
clearTimeout(visibleTimeoutRef.current);
setVisible(true);

visibleTimeoutRef.current = setTimeout(() => {
setVisible(false);
}, 3000);
Expand Down
19 changes: 19 additions & 0 deletions tests/scroll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ describe('List.Scroll', () => {
expect(wrapper.find('ul').instance().scrollTop > 10).toBeTruthy();
});

it('should show scrollbar when element has showScrollBar prop set to true', () => {
jest.useFakeTimers();
const listRef = React.createRef();
const { container } = genList(
{
itemHeight: 20,
height: 100,
data: genData(100),
ref: listRef,
showScrollBar: true,
},
render,
);
act(() => {
jest.runAllTimers();
});
const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
expect(scrollbarElement).not.toBeNull();
});
describe('not show scrollbar when disabled virtual', () => {
[
{ name: '!virtual', props: { virtual: false } },
Expand Down
Loading