Skip to content

Commit 1c7c2f8

Browse files
committed
chore(Picker): 优化 android 激活先变色后置顶
1 parent d2a3515 commit 1c7c2f8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

packages/core/src/Picker/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const Picker = (props: PickerProps) => {
6363
const ItemHeights = useRef<Array<number>>([]).current;
6464
const saveY = useRef<number>(0);
6565
const timer = useRef<NodeJS.Timeout>();
66+
const onPressTimer = useRef<NodeJS.Timeout>();
6667
const onPressORonScroll = useRef<'onPress' | 'onScroll'>('onScroll');
6768
const currentY = useRef<number>(0);
6869
const [current, setCurrent] = useState(0);
@@ -103,7 +104,6 @@ const Picker = (props: PickerProps) => {
103104
let an = Platform.OS === 'android' && { duration: 0 };
104105
let os = Platform.OS === 'ios' && { animated: false };
105106
scrollView.current?.scrollTo({ x: 0, y: scrollY - (style.containerHeight as number), ...an, ...os });
106-
setCurrent(index);
107107
};
108108
const setScrollHandle = (val: number) => {
109109
const spot = val / ItemHeights[0];
@@ -123,7 +123,14 @@ const Picker = (props: PickerProps) => {
123123
timer.current = undefined;
124124
};
125125
const listener = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
126-
if (onPressORonScroll.current === 'onPress') return;
126+
if (onPressORonScroll.current === 'onPress') {
127+
clearTimeout(onPressTimer.current!);
128+
onPressTimer.current = setTimeout(() => {
129+
setCurrent(currentY.current);
130+
clearTimeout(onPressTimer.current!);
131+
}, 16);
132+
return;
133+
}
127134
saveY.current = event.nativeEvent.contentOffset.y;
128135
if (timer.current) {
129136
clearTimeout(timer.current!);
@@ -162,6 +169,7 @@ const Picker = (props: PickerProps) => {
162169
onPressOut={Platform.OS === 'android' ? onTouchEnd : undefined}
163170
onPress={() => {
164171
if (timer.current) return;
172+
clearTimeout(onPressTimer.current!);
165173
onPressORonScroll.current = 'onPress';
166174
location(ItemHeights![index], index);
167175
}}

0 commit comments

Comments
 (0)