1
1
import React from 'react' ;
2
- import {
3
- View ,
4
- Dimensions ,
5
- StyleSheet ,
6
- TextStyle ,
7
- StyleProp ,
8
- ViewStyle ,
9
- TouchableOpacity ,
10
- Modal ,
11
- ModalProps ,
12
- Animated ,
13
- } from 'react-native' ;
2
+ import { View , Dimensions , StyleSheet , TextStyle , StyleProp , ViewStyle } from 'react-native' ;
3
+ import Modal , { ModalProps } from '../Modal' ;
14
4
export { default as ActionSheetItem } from './item' ;
15
5
import ActionSheetItem from './item' ;
16
6
@@ -40,55 +30,42 @@ export interface ActionSheetProps extends ModalProps {
40
30
}
41
31
42
32
interface ActionSheetState {
43
- animatedHeight : number ;
44
33
stateVisible : boolean ;
34
+ control : 'props' | 'state' ;
45
35
}
46
36
47
37
export default class ActionSheet extends React . Component < ActionSheetProps , ActionSheetState > {
48
- private fadeAnim : Animated . Value = new Animated . Value ( 0 ) ;
49
- private animatedRef : React . RefObject < View > = React . createRef ( ) ;
50
38
constructor ( props : ActionSheetProps ) {
51
39
super ( props ) ;
52
40
this . state = {
53
- animatedHeight : 0 ,
54
41
stateVisible : ! ! props . visible ,
42
+ control : 'props' ,
55
43
} ;
56
44
}
57
-
58
- onClose = ( ) => {
59
- Animated . timing ( this . fadeAnim , {
60
- toValue : 0 ,
61
- duration : 150 ,
62
- useNativeDriver : true ,
63
- } ) . start ( ( { finished } ) => {
64
- this . setState ( { stateVisible : false } ) ;
65
- } ) ;
66
- } ;
67
- UNSAFE_componentWillReceiveProps ( nextProps : ActionSheetProps ) {
68
- if ( nextProps . visible ) {
69
- this . setState ( { stateVisible : true } ) ;
70
- Animated . timing ( this . fadeAnim , {
71
- toValue : 0 ,
72
- duration : 0 ,
73
- useNativeDriver : true ,
74
- } ) . start ( ( { finished } ) => {
75
- this . animatedRef . current &&
76
- this . animatedRef . current . measure (
77
- ( _frameOffsetX , _frameOffsetY , _width , _height , pageOffsetX , pageOffsetY ) => {
78
- this . setState ( { animatedHeight : _height } , ( ) => {
79
- Animated . timing ( this . fadeAnim , {
80
- toValue : - _height ,
81
- duration : 150 ,
82
- useNativeDriver : true ,
83
- } ) . start ( ) ;
84
- } ) ;
85
- } ,
86
- ) ;
87
- } ) ;
88
- } else {
89
- this . onClose ( ) ;
45
+ static getDerivedStateFromProps ( props : ActionSheetProps , state : ActionSheetState ) {
46
+ if ( props . visible === state . stateVisible && state . control === 'state' ) {
47
+ return {
48
+ control : 'props' ,
49
+ stateVisible : props . visible ,
50
+ } ;
51
+ }
52
+ if ( props . visible !== state . stateVisible ) {
53
+ if ( state . control === 'state' ) {
54
+ return {
55
+ control : 'props' ,
56
+ } ;
57
+ }
58
+ return {
59
+ control : 'props' ,
60
+ stateVisible : props . visible ,
61
+ } ;
90
62
}
63
+ return null ;
91
64
}
65
+ onClose = ( ) => {
66
+ this . setState ( { stateVisible : false , control : 'state' } ) ;
67
+ } ;
68
+
92
69
render ( ) {
93
70
const {
94
71
children,
@@ -105,26 +82,14 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
105
82
const { stateVisible } = this . state ;
106
83
return (
107
84
< Modal
85
+ placement = "bottom"
108
86
animationType = "fade" // slide none fade
109
87
transparent = { true }
110
- visible = { stateVisible }
111
- onRequestClose = { this . onClose }
112
88
{ ...other }
89
+ visible = { stateVisible }
90
+ onClosed = { this . onClose }
113
91
>
114
- < TouchableOpacity
115
- activeOpacity = { 1 }
116
- style = { [ styles . position , styles . spread ] }
117
- onPress = { ( ) => onCancel && this . onClose ( ) }
118
- >
119
- < Animated . View style = { [ styles . spread , styles . backdrop ] } > </ Animated . View >
120
- </ TouchableOpacity >
121
- < Animated . View
122
- style = { [
123
- styles . actionSheet ,
124
- { bottom : - this . state . animatedHeight , transform : [ { translateY : this . fadeAnim } ] } ,
125
- ] }
126
- ref = { this . animatedRef }
127
- >
92
+ < >
128
93
{ React . Children . toArray ( children ) . map ( ( item , index ) => (
129
94
< View key = { index } >
130
95
{ index !== 0 && < View style = { StyleSheet . flatten ( [ styles . itemDivider , dividerStyle ?. itemDivider ] ) } /> }
@@ -143,38 +108,13 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
143
108
containerStyle = { containerStyle }
144
109
textStyle = { textStyle }
145
110
/>
146
- </ Animated . View >
111
+ </ >
147
112
</ Modal >
148
113
) ;
149
114
}
150
115
}
151
116
152
117
const styles = StyleSheet . create ( {
153
- position : {
154
- position : 'absolute' ,
155
- backgroundColor : 'transparent' ,
156
- top : 0 ,
157
- bottom : 0 ,
158
- left : 0 ,
159
- right : 0 ,
160
- zIndex : 9998 ,
161
- } ,
162
- backdrop : {
163
- backgroundColor : '#000' ,
164
- opacity : 0.2 ,
165
- } ,
166
- spread : {
167
- width : MainWidth ,
168
- height : MainHeight ,
169
- } ,
170
- actionSheet : {
171
- width : MainWidth ,
172
- position : 'absolute' ,
173
- left : 0 ,
174
- right : 0 ,
175
- backgroundColor : '#fff' ,
176
- zIndex : 9999 ,
177
- } ,
178
118
actionDivider : {
179
119
backgroundColor : 'rgba(197,217,232,.3)' ,
180
120
width : MainWidth ,
0 commit comments