11import React from 'react' ;
2- import { View , Dimensions , StyleSheet , Text , TouchableOpacity , Modal , ModalProps , Animated } from 'react-native' ;
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' ;
314export { default as ActionSheetItem } from './item' ;
15+ import ActionSheetItem from './item' ;
416
517let MainWidth = Dimensions . get ( 'window' ) . width ;
618let MainHeight = Dimensions . get ( 'window' ) . height ;
19+
20+ export interface DividerStyle {
21+ itemDivider ?: StyleProp < ViewStyle > ;
22+ actionDivider ?: StyleProp < ViewStyle > ;
23+ }
24+
725export interface ActionSheetProps extends ModalProps {
826 /** 点击蒙层是否关闭 */
927 onCancel ?: Boolean ;
28+ /** 分割线样式 */
29+ dividerStyle ?: DividerStyle ;
30+ /** 取消的容器样式 */
31+ containerStyle ?: StyleProp < ViewStyle > ;
32+ /** 取消的文本样式 */
33+ textStyle ?: StyleProp < TextStyle > ;
34+ /** 动作在被触摸操作激活时以多少不透明度显示 默认 1 */
35+ activeOpacity ?: number ;
36+ /** 动作有触摸操作时显示出来的底层的颜色 */
37+ underlayColor ?: string ;
1038 /** 取消的文本 */
1139 cancelText ?: React . ReactNode ;
1240}
@@ -23,7 +51,7 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
2351 super ( props ) ;
2452 this . state = {
2553 animatedHeight : 0 ,
26- stateVisible : false ,
54+ stateVisible : ! ! props . visible ,
2755 } ;
2856 }
2957
@@ -62,7 +90,18 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
6290 }
6391 }
6492 render ( ) {
65- const { children, visible, cancelText = '取消' , onCancel, ...other } = this . props ;
93+ const {
94+ children,
95+ visible,
96+ activeOpacity,
97+ underlayColor,
98+ cancelText = '取消' ,
99+ dividerStyle,
100+ onCancel,
101+ containerStyle,
102+ textStyle,
103+ ...other
104+ } = this . props ;
66105 const { stateVisible } = this . state ;
67106 return (
68107 < Modal
@@ -88,20 +127,22 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
88127 >
89128 { React . Children . toArray ( children ) . map ( ( item , index ) => (
90129 < View key = { index } >
91- { index !== 0 && < View style = { styles . actionSheetItemDivider } /> }
92- { item }
130+ { index !== 0 && < View style = { StyleSheet . flatten ( [ styles . itemDivider , dividerStyle ?. itemDivider ] ) } /> }
131+ { React . cloneElement ( item as React . DetailedReactHTMLElement < any , HTMLElement > , {
132+ activeOpacity : activeOpacity ,
133+ underlayColor : underlayColor ,
134+ } ) }
93135 </ View >
94136 ) ) }
95- < View style = { styles . divider } />
96- { typeof cancelText !== 'object' ? (
97- < TouchableOpacity activeOpacity = { 1 } onPress = { this . onClose } >
98- < View style = { styles . actionSheetItem } >
99- < Text style = { styles . actionSheetItemText } > { cancelText } </ Text >
100- </ View >
101- </ TouchableOpacity >
102- ) : (
103- < View > { cancelText } </ View >
104- ) }
137+ < View style = { StyleSheet . flatten ( [ styles . actionDivider , dividerStyle ?. actionDivider ] ) } />
138+ < ActionSheetItem
139+ activeOpacity = { activeOpacity }
140+ underlayColor = { underlayColor }
141+ onPress = { this . onClose }
142+ children = { cancelText }
143+ containerStyle = { containerStyle }
144+ textStyle = { textStyle }
145+ />
105146 </ Animated . View >
106147 </ Modal >
107148 ) ;
@@ -134,24 +175,14 @@ const styles = StyleSheet.create({
134175 backgroundColor : '#fff' ,
135176 zIndex : 9999 ,
136177 } ,
137- divider : {
178+ actionDivider : {
138179 backgroundColor : 'rgba(197,217,232,.3)' ,
139180 width : MainWidth ,
140181 height : 6 ,
141182 } ,
142- actionSheetItemDivider : {
143- borderBottomColor : 'rgba(197,217,232,.3)' ,
144- borderBottomWidth : 2 ,
145- width : MainWidth ,
146- } ,
147- actionSheetItem : {
183+ itemDivider : {
184+ backgroundColor : 'rgba(197,217,232,.3)' ,
185+ height : 2 ,
148186 width : MainWidth ,
149- height : 50 ,
150- justifyContent : 'center' ,
151- alignItems : 'center' ,
152- } ,
153- actionSheetItemText : {
154- fontSize : 20 ,
155- fontWeight : '400' ,
156187 } ,
157188} ) ;
0 commit comments