1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import { StyleSheet , ScrollView , TextInput } from 'react-native' ;
3
+ import { StyleSheet , FlatList , TextInput } from 'react-native' ;
4
4
import _ from 'lodash' ;
5
5
import { Constants } from '../../helpers' ;
6
6
import { BaseComponent } from '../../commons' ;
@@ -25,12 +25,13 @@ class PickerModal extends BaseComponent {
25
25
} ) ,
26
26
searchPlaceholder : PropTypes . string ,
27
27
onSearchChange : PropTypes . func ,
28
+ listProps : PropTypes . object ,
28
29
} ;
29
30
30
31
static defaultProps = {
31
32
searchPlaceholder : 'Search...' ,
32
33
searchStyle : { } ,
33
- }
34
+ } ;
34
35
35
36
state = {
36
37
scrollHeight : undefined ,
@@ -41,51 +42,14 @@ class PickerModal extends BaseComponent {
41
42
this . styles = createStyles ( this . props ) ;
42
43
}
43
44
44
- componentWillReceiveProps ( nextProps ) {
45
- this . scrollToSelected ( nextProps . scrollPosition ) ;
46
- }
47
-
48
- onScrollViewLayout = ( {
49
- nativeEvent : {
50
- layout : { height} ,
51
- } ,
52
- } ) => {
53
- this . setState ( { scrollHeight : height } , ( ) => {
54
- this . scrollToSelected ( ) ;
55
- } ) ;
56
- } ;
57
-
58
- onScrollViewContentSizeChange = ( contentWidth , contentHeight ) => {
59
- this . setState ( { scrollContentHeight : contentHeight } , ( ) => {
60
- this . scrollToSelected ( ) ;
61
- } ) ;
62
- } ;
63
-
64
- scrollToSelected ( scrollPosition = this . props . scrollPosition ) {
65
- const isSearchFocused = _ . invoke ( this . search , 'isFocused' ) ;
66
- if ( ! scrollPosition || isSearchFocused ) return ;
67
-
68
- const { scrollHeight, scrollContentHeight} = this . state ;
69
- if ( this . scrollView && scrollHeight && scrollContentHeight ) {
70
- const pageNumber = Math . floor ( scrollPosition / scrollHeight ) ;
71
- const numberOfPages = Math . ceil ( scrollContentHeight / scrollHeight ) ;
72
-
73
- if ( pageNumber === numberOfPages - 1 ) {
74
- this . scrollView . scrollToEnd ( { animated : false } ) ;
75
- } else {
76
- this . scrollView . scrollTo ( { x : 0 , y : pageNumber * scrollHeight , animated : false } ) ;
77
- }
78
- }
79
- }
80
-
81
45
renderSearchInput ( ) {
82
46
const { showSearch, searchStyle, searchPlaceholder, onSearchChange} = this . props ;
83
47
if ( showSearch ) {
84
48
return (
85
49
< View style = { this . styles . searchInputContainer } >
86
- < Image style = { this . styles . searchIcon } source = { Assets . icons . search } />
50
+ < Image style = { this . styles . searchIcon } source = { Assets . icons . search } />
87
51
< TextInput
88
- ref = { r => this . search = r }
52
+ ref = { r => ( this . search = r ) }
89
53
style = { [ this . styles . searchInput , { color : searchStyle . color } ] }
90
54
placeholderTextColor = { searchStyle . placeholderTextColor }
91
55
selectionColor = { searchStyle . selectionColor }
@@ -100,7 +64,7 @@ class PickerModal extends BaseComponent {
100
64
}
101
65
102
66
render ( ) {
103
- const { visible, enableModalBlur, topBarProps, children} = this . props ;
67
+ const { visible, enableModalBlur, topBarProps, listProps , children} = this . props ;
104
68
return (
105
69
< Modal
106
70
animationType = { 'slide' }
@@ -111,14 +75,15 @@ class PickerModal extends BaseComponent {
111
75
>
112
76
< Modal . TopBar { ...topBarProps } />
113
77
{ this . renderSearchInput ( ) }
114
- < ScrollView
115
- ref = { r => ( this . scrollView = r ) }
116
- onLayout = { this . onScrollViewLayout }
117
- onContentSizeChange = { this . onScrollViewContentSizeChange }
118
- keyboardShouldPersistTaps = "always"
119
- >
120
- < View style = { this . styles . modalBody } > { children } </ View >
121
- </ ScrollView >
78
+
79
+ < FlatList
80
+ data = { _ . times ( React . Children . count ( children ) ) }
81
+ renderItem = { ( { index} ) => {
82
+ return React . Children . toArray ( children ) [ index ] ;
83
+ } }
84
+ keyExtractor = { ( item , index ) => index }
85
+ { ...listProps }
86
+ />
122
87
</ Modal >
123
88
) ;
124
89
}
0 commit comments