@@ -12,28 +12,28 @@ import {
12
12
} from 'react-native' ;
13
13
import Animated , { useSharedValue , useAnimatedScrollHandler } from 'react-native-reanimated' ;
14
14
import { FlatList } from 'react-native-gesture-handler' ;
15
- import { Colors , Spacings } from 'style' ;
16
- import { Constants , asBaseComponent } from '../../commons/new' ;
17
- import View from '../../components/view' ;
18
- import Fader , { FaderPosition , FaderProps } from '../../components/fader' ;
19
- import Item , { ItemProps } from './Item' ;
20
- import Text , { TextProps } from '../../components/text' ;
15
+ import { Colors , Spacings } from '../../style' ;
16
+ import { Constants } from '../../commons/new' ;
17
+ import { useThemeProps } from '../../hooks' ;
18
+ import View from '../view' ;
19
+ import Text , { TextProps } from '../text' ;
20
+ import Fader , { FaderPosition , FaderProps } from '../fader' ;
21
+ import Item , { WheelPickerItemProps } from './Item' ;
21
22
import usePresenter from './usePresenter' ;
22
- import { WheelPickerAlign } from './types' ;
23
- export { WheelPickerAlign } ;
23
+ import { WheelPickerAlign , WheelPickerItemValue } from './types' ;
24
+ export type { WheelPickerAlign , WheelPickerItemValue } ;
24
25
25
- const AnimatedFlatList = Animated . createAnimatedComponent < FlatListProps < ItemProps > > ( FlatList ) ;
26
26
export const ITEM_HEIGHT = 44 ;
27
27
28
- export interface WheelPickerProps {
28
+ export type WheelPickerProps < T = WheelPickerItemValue > = {
29
29
/**
30
30
* Initial value
31
31
*/
32
- initialValue ?: ItemProps | number | string ;
32
+ initialValue ?: T ;
33
33
/**
34
34
* Data source for WheelPicker
35
35
*/
36
- items ?: ItemProps [ ] ;
36
+ items ?: WheelPickerItemProps < T > [ ] ;
37
37
/**
38
38
* Describe the height of each item in the WheelPicker
39
39
* default value: 44
@@ -71,7 +71,7 @@ export interface WheelPickerProps {
71
71
/**
72
72
* Event, on active row change
73
73
*/
74
- onChange ?: ( item : string | number , index : number ) => void ;
74
+ onChange ?: ( item : T , index : number ) => void ;
75
75
/**
76
76
* Container's ViewStyle, height is computed according to itemHeight * numberOfVisibleRows
77
77
*/
@@ -97,30 +97,35 @@ export interface WheelPickerProps {
97
97
/**
98
98
* Props to be sent to the FlatList
99
99
*/
100
- flatListProps ?: Partial < FlatListProps < ItemProps > > ;
100
+ flatListProps ?: Partial < FlatListProps < WheelPickerItemProps < T > > > ;
101
101
}
102
102
103
- const WheelPicker = ( {
104
- items : propItems ,
105
- itemHeight = ITEM_HEIGHT ,
106
- numberOfVisibleRows = 5 ,
107
- activeTextColor = Colors . $textPrimary ,
108
- inactiveTextColor,
109
- textStyle,
110
- label,
111
- labelStyle,
112
- labelProps,
113
- onChange,
114
- align = WheelPickerAlign . CENTER ,
115
- disableRTL,
116
- style,
117
- children,
118
- initialValue = 0 ,
119
- separatorsStyle,
120
- testID,
121
- faderProps,
122
- flatListProps
123
- } : WheelPickerProps ) => {
103
+ const WheelPicker = < T extends WheelPickerItemValue > ( props : WheelPickerProps < T > ) => {
104
+ const AnimatedFlatList =
105
+ useMemo ( ( ) => Animated . createAnimatedComponent < FlatListProps < WheelPickerItemProps < T > > > ( FlatList ) , [ ] ) ;
106
+ const themeProps = useThemeProps ( props , 'WheelPicker' ) ;
107
+
108
+ const {
109
+ items : propItems ,
110
+ itemHeight = ITEM_HEIGHT ,
111
+ numberOfVisibleRows = 5 ,
112
+ activeTextColor = Colors . $textPrimary ,
113
+ inactiveTextColor,
114
+ textStyle,
115
+ label,
116
+ labelStyle,
117
+ labelProps,
118
+ onChange,
119
+ align = WheelPickerAlign . CENTER ,
120
+ disableRTL,
121
+ style,
122
+ children,
123
+ initialValue,
124
+ separatorsStyle,
125
+ testID,
126
+ faderProps,
127
+ flatListProps
128
+ } = themeProps ;
124
129
const scrollView = useRef < Animated . ScrollView > ( ) ;
125
130
const offset = useSharedValue ( 0 ) ;
126
131
const scrollHandler = useAnimatedScrollHandler ( e => {
@@ -133,7 +138,7 @@ const WheelPicker = ({
133
138
const {
134
139
height,
135
140
items,
136
- index : currentIndex ,
141
+ index : currentIndex = 0 ,
137
142
getRowItemAtOffset
138
143
} = usePresenter ( {
139
144
initialValue,
@@ -146,7 +151,7 @@ const WheelPicker = ({
146
151
const prevInitialValue = useRef ( initialValue ) ;
147
152
const prevIndex = useRef ( currentIndex ) ;
148
153
const [ flatListWidth , setFlatListWidth ] = useState ( 0 ) ;
149
- const keyExtractor = useCallback ( ( item : ItemProps , index : number ) => `${ item } .${ index } ` , [ ] ) ;
154
+ const keyExtractor = useCallback ( ( item : WheelPickerItemProps < T > , index : number ) => `${ item } .${ index } ` , [ ] ) ;
150
155
const androidFlatListProps = useMemo ( ( ) => {
151
156
if ( Constants . isAndroid ) {
152
157
return {
@@ -156,7 +161,7 @@ const WheelPicker = ({
156
161
} , [ items ] ) ;
157
162
158
163
useEffect ( ( ) => {
159
- //This effect should replace the onLyout function in the flatlist , should happen only once
164
+ // This effect should replace the onLayout function in the FlatList , should happen only once
160
165
scrollToIndex ( currentIndex , true ) ;
161
166
} , [ ] ) ;
162
167
@@ -165,7 +170,7 @@ const WheelPicker = ({
165
170
! isUndefined ( initialValue ) && scrollToIndex ( currentIndex , true ) ;
166
171
} , [ currentIndex ] ) ;
167
172
168
- const _onChange = useCallback ( ( value : string | number , index : number ) => {
173
+ const _onChange = useCallback ( ( value : T , index : number ) => {
169
174
if ( prevInitialValue . current !== initialValue ) {
170
175
// don't invoke 'onChange' if 'initialValue' changed
171
176
prevInitialValue . current = initialValue ;
@@ -176,7 +181,7 @@ const WheelPicker = ({
176
181
[ initialValue , onChange ] ) ;
177
182
178
183
const onValueChange = useCallback ( ( event : NativeSyntheticEvent < NativeScrollEvent > ) => {
179
- const { index , value } = getRowItemAtOffset ( event . nativeEvent . contentOffset . y ) ;
184
+ const { value , index } = getRowItemAtOffset ( event . nativeEvent . contentOffset . y ) ;
180
185
_onChange ( value , index ) ;
181
186
} ,
182
187
[ _onChange , getRowItemAtOffset ] ) ;
@@ -224,7 +229,7 @@ const WheelPicker = ({
224
229
return { ...labelMargins , ...labelProps } ;
225
230
} , [ labelMargins , labelProps ] ) ;
226
231
227
- const renderItem = useCallback ( ( { item, index} : ListRenderItemInfo < ItemProps > ) => {
232
+ const renderItem = useCallback ( ( { item, index} : ListRenderItemInfo < WheelPickerItemProps < T > > ) => {
228
233
return (
229
234
< Item
230
235
index = { index }
@@ -378,8 +383,8 @@ const WheelPicker = ({
378
383
} ;
379
384
380
385
WheelPicker . alignments = WheelPickerAlign ;
381
- export default asBaseComponent < WheelPickerProps , typeof WheelPicker > ( WheelPicker ) ;
382
- export { ItemProps as WheelPickerItemProps } ;
386
+ export default WheelPicker ;
387
+ export { WheelPickerItemProps } ;
383
388
384
389
const styles = StyleSheet . create ( {
385
390
separators : {
0 commit comments