Skip to content

Commit 1ec3e16

Browse files
authored
fix: ScrollTo need wait for scroll item exist (#762)
* fix: scroll delay logic * test: fix test case
1 parent b0e8561 commit 1ec3e16

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/PickerPanel/TimePanel/TimePanelBody/useScrollTo.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEvent } from 'rc-util';
22
import raf from 'rc-util/lib/raf';
3+
import isVisible from 'rc-util/lib/Dom/isVisible';
34
import * as React from 'react';
45

56
const SPEED_PTG = 1 / 3;
@@ -20,9 +21,12 @@ export default function useScrollTo(
2021
scrollingRef.current = false;
2122
};
2223

24+
const scrollRafTimesRef = React.useRef<number>();
25+
2326
const startScroll = () => {
2427
const ul = ulRef.current;
2528
scrollDistRef.current = null;
29+
scrollRafTimesRef.current = 0;
2630

2731
if (ul) {
2832
const targetLi = ul.querySelector<HTMLLIElement>(`[data-value="${value}"]`);
@@ -31,16 +35,19 @@ export default function useScrollTo(
3135
const doScroll = () => {
3236
stopScroll();
3337
scrollingRef.current = true;
38+
scrollRafTimesRef.current += 1;
3439

3540
const { scrollTop: currentTop } = ul;
3641

3742
const firstLiTop = firstLi.offsetTop;
3843
const targetLiTop = targetLi.offsetTop;
3944
const targetTop = targetLiTop - firstLiTop;
4045

41-
// Wait for element exist
42-
if (targetLiTop === 0 && targetLi !== firstLi) {
43-
scrollRafRef.current = raf(doScroll);
46+
// Wait for element exist. 5 frames is enough
47+
if ((targetLiTop === 0 && targetLi !== firstLi) || !isVisible(ul)) {
48+
if (scrollRafTimesRef.current <= 5) {
49+
scrollRafRef.current = raf(doScroll);
50+
}
4451
return;
4552
}
4653

tests/new-range.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import {
1919
waitFakeTimer,
2020
} from './util/commonUtil';
2121

22+
jest.mock('rc-util/lib/Dom/isVisible', () => {
23+
return () => true;
24+
});
25+
2226
describe('NewPicker.Range', () => {
2327
beforeEach(() => {
2428
resetWarned();

tests/picker.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import {
2929

3030
const fakeTime = getDay('1990-09-03 00:00:00').valueOf();
3131

32+
jest.mock('rc-util/lib/Dom/isVisible', () => {
33+
return () => true;
34+
});
35+
3236
describe('Picker.Basic', () => {
3337
let errorSpy;
3438
beforeEach(() => {

0 commit comments

Comments
 (0)