Skip to content

Commit 76ad9b3

Browse files
authored
fix: optimize the shadow effect of Scrollable (#443)
* fix: fix the shadow of scrollable * test: improve test for scrollable * test: remove useless code in test
1 parent 50e0de2 commit 76ad9b3

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/components/scrollable/__tests__/scrollable.test.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,35 @@ describe('Test Scrollable Component', () => {
9999
fireEvent.scroll(rootEle);
100100
expect(trackElement(container).style.opacity).toBe('1');
101101
});
102+
103+
test('Should NOT render active shadow when scorllTop is 0', () => {
104+
const original = React.useState;
105+
React.useState = jest
106+
.fn()
107+
.mockImplementation((args) => [args, () => {}]);
108+
109+
const { container } = render(<TestScrollable isShowShadow />);
110+
111+
const shadowDom = container.querySelector('.shadow');
112+
expect(shadowDom?.classList).not.toContain('active');
113+
114+
React.useState = original;
115+
});
116+
117+
test("Should render active shadow when scrollTop isn't 0", () => {
118+
const original = React.useState;
119+
React.useState = jest
120+
.fn()
121+
.mockImplementation((args) => [
122+
typeof args === 'number' ? 100 : args,
123+
() => {},
124+
]);
125+
126+
const { container } = render(<TestScrollable isShowShadow />);
127+
128+
const shadowDom = container.querySelector('.shadow');
129+
expect(shadowDom?.classList).toContain('active');
130+
131+
React.useState = original;
132+
});
102133
});

src/components/scrollable/index.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ const Scrollable = forwardRef<Scrollbar, IScrollbarProps>(function (
3333

3434
const [isScrolling, setIsScrolling] = useState(false);
3535
const [isMouseOver, setIsMouseOver] = useState(false);
36-
const [scrollTop, setScrollTop] = useState(0);
36+
const [scrollTop, setScrollTop] = React.useState(0);
3737
const isShow = isScrolling || isMouseOver;
3838

3939
const claNames = classNames(defaultScrollableClassName, className);
4040

41-
const onScrollStart = useCallback((values) => {
41+
const onScroll = useCallback(({ scrollTop }) => {
42+
/* istanbul ignore next */
43+
setScrollTop(scrollTop);
44+
}, []);
45+
46+
const onScrollStart = useCallback(() => {
4247
setIsScrolling(true);
43-
setScrollTop(values.scrollTop);
4448
}, []);
4549
const onScrollStop = useCallback(() => {
4650
/* istanbul ignore next */
@@ -132,6 +136,7 @@ const Scrollable = forwardRef<Scrollbar, IScrollbarProps>(function (
132136
thumbYProps={thumbProps}
133137
onScrollStart={onScrollStart}
134138
onScrollStop={onScrollStop}
139+
onScroll={onScroll}
135140
scrollDetectionThreshold={500} // ms
136141
>
137142
{children}

0 commit comments

Comments
 (0)