11import 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' ;
144export { default as ActionSheetItem } from './item' ;
155import ActionSheetItem from './item' ;
166
@@ -40,55 +30,42 @@ export interface ActionSheetProps extends ModalProps {
4030}
4131
4232interface ActionSheetState {
43- animatedHeight : number ;
4433 stateVisible : boolean ;
34+ control : 'props' | 'state' ;
4535}
4636
4737export 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 ( ) ;
5038 constructor ( props : ActionSheetProps ) {
5139 super ( props ) ;
5240 this . state = {
53- animatedHeight : 0 ,
5441 stateVisible : ! ! props . visible ,
42+ control : 'props' ,
5543 } ;
5644 }
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+ } ;
9062 }
63+ return null ;
9164 }
65+ onClose = ( ) => {
66+ this . setState ( { stateVisible : false , control : 'state' } ) ;
67+ } ;
68+
9269 render ( ) {
9370 const {
9471 children,
@@ -105,26 +82,14 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
10582 const { stateVisible } = this . state ;
10683 return (
10784 < Modal
85+ placement = "bottom"
10886 animationType = "fade" // slide none fade
10987 transparent = { true }
110- visible = { stateVisible }
111- onRequestClose = { this . onClose }
11288 { ...other }
89+ visible = { stateVisible }
90+ onClosed = { this . onClose }
11391 >
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+ < >
12893 { React . Children . toArray ( children ) . map ( ( item , index ) => (
12994 < View key = { index } >
13095 { index !== 0 && < View style = { StyleSheet . flatten ( [ styles . itemDivider , dividerStyle ?. itemDivider ] ) } /> }
@@ -143,38 +108,13 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
143108 containerStyle = { containerStyle }
144109 textStyle = { textStyle }
145110 />
146- </ Animated . View >
111+ </ >
147112 </ Modal >
148113 ) ;
149114 }
150115}
151116
152117const 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- } ,
178118 actionDivider : {
179119 backgroundColor : 'rgba(197,217,232,.3)' ,
180120 width : MainWidth ,
0 commit comments