YH_ActionSheet.js
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
StyleSheet,
Text,
Modal,
TouchableOpacity,
Dimensions
} from 'react-native'
const {width,height} =Dimensions.get('window')
export default class YH_ActionSheet extends React.Component {
constructor(props){
super(props)
this.state = {
modalVisible:false,
}
}
static propTypes={
items:PropTypes.array,
itemStyle:PropTypes.object,
actionTitleStyle:PropTypes.object,
itemTitleStyle:PropTypes.object,
modalTitle:PropTypes.string,
}
static defaultProps={
items:[],
itemStyle:{},
actionTitleStyle:{},
itemTitleStyle:{},
modalTitle:''
}
showModal(){
this.setState({modalVisible:true})
}
cancelModal(){
this.setState({modalVisible:false})
}
clickItem(id){
this.props.actionsheetItemClick(id);
}
render(){
let actionSheets = this.props.items.map((item,i)=>{
return(
<TouchableOpacity
key={i}
style={[styles.actionItem,this.props.itemStyle]}
onPress={()=>this.clickItem(item.id)}>
<Text style={[styles.actionItemTitle,this.props.itemTitleStyle]}
>{item.title}</Text>
</TouchableOpacity>
)
})
return <Modal animationType="fade" //slide
visible={this.state.modalVisible}
transparent={true}
onRequestClose={()=>this.setState({modalVisible:false})}>
<View style={styles.modalStyle}>
<View style={styles.subView}>
<View style={styles.itemContainer}>
{this.props.modalTitle ? <View style={[styles.itemContainer,{height:55,marginTop:5}]}>
<Text style={[styles.actionTitle,this.props.actionTitleStyle]}
>{this.props.modalTitle}</Text>
</View> : null}
{actionSheets}
</View>
<View style={[styles.itemContainer]}>
<TouchableOpacity
style={[styles.actionItem,this.props.itemStyle,{height:70,marginBottom:-5,backgroundColor:'white'}]}
onPress={()=>this.setState({modalVisible:false})}>
<Text style={[styles.actionItemTitle,this.props.itemTitleStyle,{color:'#999999',}]}>取消</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
}
}
const styles = StyleSheet.create({
modalStyle:{
justifyContent:'flex-end',
alignItems:'center',
flex:1,
backgroundColor:'rgba(0,0,0,0.2)',
},
subView:{
justifyContent:'flex-end',
alignItems:'center',
alignSelf:'stretch',
width:width,
backgroundColor:'white',
},
itemContainer:{
marginLeft:15,
marginRight:15,
marginBottom:0.5,
backgroundColor:'#fff',
justifyContent:'center',
alignItems:'center',
},
actionItem:{
width:width,
height:70,
alignItems:'center',
justifyContent:'center',
borderTopColor:'#EEEEEE',
borderTopWidth:0.5,
},
actionTitle:{
fontSize:14,
color:'#9EA3AD',
textAlign:'center',
},
actionItemTitle:{
fontSize:16,
color:'#000000',
textAlign:'center',
},
})