Skip to content

Commit 03ccb09

Browse files
committed
Use Flatlist for WheelPicker for better performance when having big lists
1 parent 16d8001 commit 03ccb09

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

demo/src/screens/incubatorScreens/WheelPickerScreen.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import {View, Text, Incubator} from 'react-native-ui-lib';
33
import _ from 'lodash';
44

5+
// Months
56
const items = [
67
'January',
78
'February',
@@ -17,6 +18,9 @@ const items = [
1718
'December'
1819
];
1920

21+
// Years
22+
// const items = _.times(2000, i => i);
23+
2024
export default () => {
2125
return (
2226
<View flex padding-page>

generatedTypes/incubator/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as TabController } from './TabController';
22
export { default as TextField } from './TextField';
33
export { default as TouchableOpacity } from './TouchableOpacity';
4+
export { default as WheelPicker } from './WheelPicker';

src/incubator/WheelPicker/Item.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export default ({
5858
itemOffset,
5959
itemOffset + 2 * itemHeight
6060
],
61-
outputRange: [0.8, 1, 0.8]
61+
// outputRange: [0.8, 1, 0.8], // with scale change
62+
outputRange: [1, 1, 1],
6263
});
6364
}, [itemHeight]);
6465

src/incubator/WheelPicker/index.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// TODO: Support control of visible items
44
import _ from 'lodash';
55
import React, {useCallback, useRef} from 'react';
6-
import {TextStyle} from 'react-native';
6+
import {TextStyle, FlatList} from 'react-native';
77
import Animated from 'react-native-reanimated';
88
import {onScrollEvent, useValues} from 'react-native-redash';
99

@@ -12,29 +12,25 @@ import {Constants} from '../../helpers';
1212

1313
import Item, {ItemProps} from './Item';
1414

15+
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
16+
1517
export interface WheelPickerProps {
1618
items?: ItemProps[];
1719
itemHeight?: number;
1820
itemTextStyle?: TextStyle;
1921
onChange: (item: ItemProps, index: number) => void;
2022
}
2123

22-
const WheelPicker = ({
23-
items,
24-
itemHeight = 48,
25-
itemTextStyle
26-
}: WheelPickerProps) => {
24+
const WheelPicker = ({items, itemHeight = 48, itemTextStyle}: WheelPickerProps) => {
2725
const height = itemHeight * 4;
2826
const scrollview = useRef<Animated.ScrollView>();
2927
const [offset] = useValues([0], []);
3028
const onScroll = onScrollEvent({y: offset});
3129

3230
const selectItem = useCallback(
33-
(index) => {
31+
index => {
3432
if (scrollview.current) {
35-
scrollview.current
36-
.getNode()
37-
.scrollTo({y: index * itemHeight, animated: true});
33+
scrollview.current.getNode().scrollTo({y: index * itemHeight, animated: true});
3834
}
3935
},
4036
[itemHeight]
@@ -44,10 +40,25 @@ const WheelPicker = ({
4440
// TODO: need to implement on change event that calc the current selected index
4541
}, [itemHeight]);
4642

43+
const renderItem = useCallback(({item, index}) => {
44+
return (
45+
<Item
46+
index={index}
47+
itemHeight={itemHeight}
48+
offset={offset}
49+
textStyle={itemTextStyle}
50+
{...item}
51+
onSelect={selectItem}
52+
/>
53+
);
54+
}, []);
55+
4756
return (
4857
<View>
4958
<View width={250} height={height} br20>
50-
<Animated.ScrollView
59+
<AnimatedFlatList
60+
data={items}
61+
keyExtractor={keyExtractor}
5162
scrollEventThrottle={16}
5263
onScroll={onScroll}
5364
onMomentumScrollEnd={onChange}
@@ -59,21 +70,8 @@ const WheelPicker = ({
5970
}}
6071
snapToInterval={itemHeight}
6172
decelerationRate={Constants.isAndroid ? 0.98 : 'normal'}
62-
>
63-
{_.map(items, (item, index) => {
64-
return (
65-
<Item
66-
key={item.value}
67-
index={index}
68-
itemHeight={itemHeight}
69-
offset={offset}
70-
textStyle={itemTextStyle}
71-
{...item}
72-
onSelect={selectItem}
73-
/>
74-
);
75-
})}
76-
</Animated.ScrollView>
73+
renderItem={renderItem}
74+
/>
7775
<View absF centerV pointerEvents="none">
7876
<View
7977
style={{
@@ -88,4 +86,6 @@ const WheelPicker = ({
8886
);
8987
};
9088

89+
const keyExtractor = (item: ItemProps) => item.value;
90+
9191
export default WheelPicker;

0 commit comments

Comments
 (0)