@@ -2,17 +2,18 @@ import React, {Component} from "react"
2
2
import {
3
3
View ,
4
4
Text ,
5
+ Modal ,
6
+ Animated ,
7
+ Dimensions ,
5
8
StyleSheet ,
6
9
TouchableWithoutFeedback ,
7
- Modal ,
8
- Dimensions
9
10
} from "react-native"
10
11
11
12
import OptionList from "./Optionlist"
12
13
import Indicator from "./Indicator"
13
14
14
- const window = Dimensions . get ( 'window' )
15
-
15
+ var deviceHeight = Dimensions . get ( 'window' ) . height
16
+ var deviceWidth = Dimensions . get ( 'window' ) . width
16
17
17
18
export default class Select extends Component {
18
19
static defaultProps = {
@@ -34,20 +35,23 @@ export default class Select extends Component {
34
35
indicator : React . PropTypes . string ,
35
36
indicatorColor : React . PropTypes . string ,
36
37
indicatorSize : React . PropTypes . number ,
37
- indicatorStyle : View . propTypes . style
38
+ indicatorStyle : View . propTypes . style ,
39
+ direction :
38
40
}
39
41
42
+ state = {
43
+ modalVisible : false ,
44
+ defaultText : this . props . defaultText ,
45
+ selected : this . props . selected ,
46
+ } ;
40
47
41
- constructor ( props ) {
42
- super ( props )
43
- this . selected = this . props . selected
44
- this . state = {
45
- modalVisible : false ,
46
- defaultText : this . props . defaultText ,
47
- selected : this . props . selected ,
48
- }
49
- }
48
+ selected = this . props . selected
49
+ left = ( this . props . direction == 'fromLeft' ) ? new Animated . Value ( - deviceWidth ) : null
50
+ right = ( this . props . direction == 'fromRight' ) ? new Animated . Value ( - deviceWidth ) : null
51
+ top = ( this . props . direction == 'fromTop' ) ? new Animated . Value ( - deviceHeight ) : null
52
+ bottom = ( this . props . direction == 'fromBottom' ) ? new Animated . Value ( - deviceHeight ) : null
50
53
54
+
51
55
onSelect ( label , value ) {
52
56
this . props . onSelect ( value )
53
57
this . setState ( {
@@ -56,10 +60,43 @@ export default class Select extends Component {
56
60
} )
57
61
}
58
62
59
- onClose ( ) {
60
- this . setState ( {
61
- modalVisible : false
62
- } )
63
+ openModal = e => {
64
+ this . setState ( { modalVisible : true } )
65
+ switch ( this . props . direction ) {
66
+ case 'fromLeft' :
67
+ Animated . timing ( this . left , { toValue : 0 , duration : 200 } ) . start ( )
68
+ break
69
+ case 'fromRight' :
70
+ Animated . timing ( this . right , { toValue : 0 } ) . start ( )
71
+ break
72
+ case 'fromTop' :
73
+ Animated . timing ( this . top , { toValue : 0 } ) . start ( )
74
+ break
75
+ case 'fromBottom' :
76
+ Animated . timing ( this . bottom , { toValue : 0 } ) . start ( )
77
+ break
78
+ }
79
+ }
80
+
81
+ closeModal = e => {
82
+ switch ( this . props . direction ) {
83
+ case 'fromLeft' :
84
+ Animated . timing ( this . left , { toValue : - deviceWidth } ) . start ( )
85
+ setTimeout ( ( ) => this . setState ( { modalVisible : false } ) , 500 )
86
+ break
87
+ case 'fromRight' :
88
+ Animated . timing ( this . right , { toValue : - deviceWidth } ) . start ( )
89
+ setTimeout ( ( ) => this . setState ( { modalVisible : false } ) , 500 )
90
+ break
91
+ case 'fromTop' :
92
+ Animated . timing ( this . top , { toValue : - deviceHeight } ) . start ( )
93
+ setTimeout ( ( ) => this . setState ( { modalVisible : false } ) , 500 )
94
+ break
95
+ case 'fromBottom' :
96
+ Animated . timing ( this . bottom , { toValue : - deviceHeight } ) . start ( )
97
+ setTimeout ( ( ) => this . setState ( { modalVisible : false } ) , 500 )
98
+ break
99
+ }
63
100
}
64
101
65
102
render ( ) {
@@ -70,56 +107,51 @@ export default class Select extends Component {
70
107
71
108
return (
72
109
< View >
73
- < TouchableWithoutFeedback onPress = { this . onPress . bind ( this ) } >
110
+ < TouchableWithoutFeedback onPress = { this . openModal } >
111
+ {
112
+ ( this . props . renderButton ) ?
113
+ < View >
114
+ { this . props . renderButton ( this . state . defaultText ) }
115
+ </ View > :
74
116
< View style = { [ styles . selectBox , style ] } >
75
117
< View style = { styles . selectBoxContent } >
76
118
< Text style = { textStyle } > { this . state . defaultText } </ Text >
77
119
< Indicator direction = { indicator } color = { indicatorColor } size = { indicatorSize } style = { indicatorStyle } />
78
120
</ View >
79
121
</ View >
122
+ }
80
123
</ TouchableWithoutFeedback >
81
124
82
125
< Modal
83
- transparent = { transparent }
84
- animationType = { animationType }
85
- visible = { this . state . modalVisible }
86
- onRequestClose = { this . onClose . bind ( this ) }
87
- >
88
- < TouchableWithoutFeedback onPress = { this . onModalPress . bind ( this ) } >
89
- < View style = { [ styles . modalOverlay , backdropStyle ] } >
90
- < OptionList
91
- onSelect = { this . onSelect . bind ( this ) }
92
- selectedStyle = { selectedStyle }
93
- selected = { selected }
94
- style = { [ optionListStyle ] } >
95
- { this . props . children }
96
- </ OptionList >
97
- </ View >
98
- </ TouchableWithoutFeedback >
99
-
126
+ animationType = { "none" }
127
+ transparent = { true }
128
+ visible = { this . state . modalVisible }
129
+ onRequestClose = { this . closeModal }
130
+ >
131
+ < Animated . View style = { [ styles . modal , {
132
+ left : this . left ,
133
+ right : this . right ,
134
+ top : this . top ,
135
+ bottom : this . bottom ,
136
+ } ] } >
137
+ < TouchableWithoutFeedback onPress = { this . closeModal } >
138
+ < View style = { [ styles . modalOverlay , backdropStyle ] } >
139
+ < OptionList
140
+ onSelect = { this . onSelect . bind ( this ) }
141
+ selectedStyle = { selectedStyle }
142
+ selected = { selected }
143
+ style = { [ optionListStyle ] }
144
+ >
145
+ { this . props . children }
146
+ </ OptionList >
147
+ </ View >
148
+ </ TouchableWithoutFeedback >
149
+ </ Animated . View >
100
150
</ Modal >
151
+
101
152
</ View >
102
153
)
103
154
}
104
- /*
105
- Fired when user clicks the button
106
- */
107
- onPress ( ) {
108
- this . setState ( {
109
- modalVisible : ! this . state . modalVisible
110
- } )
111
- }
112
-
113
- /*
114
- Fires when user clicks on modal. primarily used to close
115
- the select box
116
- */
117
-
118
- onModalPress ( ) {
119
- this . setState ( {
120
- modalVisible : false
121
- } )
122
- }
123
155
124
156
setSelectedText ( text ) {
125
157
this . setState ( {
@@ -145,6 +177,11 @@ var styles = StyleSheet.create({
145
177
justifyContent : "center" ,
146
178
alignItems : "center" ,
147
179
width : window . width ,
148
- height : window . height
180
+ height : window . height
181
+ } ,
182
+ modal :{
183
+ position : 'absolute' ,
184
+ height : deviceHeight ,
185
+ width : deviceWidth ,
149
186
}
150
187
} )
0 commit comments