forked from GeekyAnts/NativeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionsheet.js
More file actions
181 lines (175 loc) · 5.22 KB
/
Actionsheet.js
File metadata and controls
181 lines (175 loc) · 5.22 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { Component } from "react";
import PropTypes from "prop-types";
import {
View,
Modal,
Platform,
ActionSheetIOS,
TouchableOpacity,
ViewPropTypes,
FlatList,
Dimensions
} from "react-native";
import { connectStyle } from "native-base-shoutem-theme";
import { Text } from "./Text";
import { Button } from "./Button";
import { ViewNB } from "./View";
import { Icon } from "./Icon";
import { Left } from "./Left";
import { Right } from "./Right";
import { Body } from "./Body";
import { ListItem } from "./ListItem";
import mapPropsToStyleNames from "../utils/mapPropsToStyleNames";
class ActionSheetContainer extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
items: []
};
}
static actionsheetInstance;
static show(config, callback) {
this.actionsheetInstance._root.showActionSheet(config, callback);
}
static hide(){
this.actionsheetInstance._root.hideActionSheet();
}
showActionSheet(config, callback) {
if (Platform.OS === "ios") {
if (typeof config.options[0] == "object") {
let options = config.options;
let filtered = options.map(item => {
return item.text;
});
config.options = filtered;
ActionSheetIOS.showActionSheetWithOptions(config, callback);
} else {
ActionSheetIOS.showActionSheetWithOptions(config, callback);
}
} else {
this.setState({
items: config.options,
title: config.title,
message: config.message,
destructiveButtonIndex: config.destructiveButtonIndex,
cancelButtonIndex: config.cancelButtonIndex,
modalVisible: true,
callback: callback
});
}
}
hideActionSheet(){
this.setState({ modalVisible: false });
}
componentDidMount() {
if (!this.props.autoHide && this.props.duration) {
console.warn(`It's not recommended to set autoHide false with duration`);
}
}
render() {
return (
<Modal
animationType={"fade"}
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
this.state.callback(this.state.cancelButtonIndex);
this.setState({ modalVisible: false });
}}
>
<TouchableOpacity
activeOpacity={1}
onPress={() => {
this.state.callback(this.state.cancelButtonIndex);
this.setState({ modalVisible: false });
}}
style={{
backgroundColor: "rgba(0,0,0,0.4)",
flex: 1,
justifyContent: "flex-end"
}}
>
<TouchableOpacity
activeOpacity={1}
style={{
backgroundColor: "#fff",
minHeight: 56,
height: this.state.length * 80,
maxHeight: Dimensions.get("window").height / 2,
padding: 15,
elevation: 4
}}
>
{this.state.title ? (
<Text style={{ color: "#757575" }}>{this.state.title}</Text>
) : null}
<FlatList
style={{
marginHorizontal: -15,
marginTop: this.state.title ? 15 : 0
}}
data={this.state.items}
keyExtractor={(item, index) => String(index)}
renderItem={({ index, item }) => {
return typeof this.state.items[0] === "string" ? (
<ListItem
onPress={() => {
this.state.callback(parseInt(index));
this.setState({ modalVisible: false });
}}
style={{ borderColor: "transparent", marginLeft: 14 }}
>
<Text>{item}</Text>
</ListItem>
) : (
<ListItem
onPress={() => {
this.state.callback(parseInt(index));
this.setState({ modalVisible: false });
}}
style={{
borderColor: "transparent",
marginLeft: 14,
height: 50
}}
icon
>
<Left>
<Icon
name={item.icon}
style={{
color: item.iconColor ? item.iconColor : undefined
}}
/>
</Left>
<Body
style={{ borderColor: "transparent", paddingLeft: 7 }}
>
<Text>{item.text}</Text>
</Body>
<Right />
</ListItem>
);
}}
/>
</TouchableOpacity>
</TouchableOpacity>
</Modal>
);
}
}
ActionSheetContainer.propTypes = {
...ViewPropTypes,
style: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number,
PropTypes.array
])
};
const StyledActionSheetContainer = connectStyle(
"NativeBase.ActionSheetContainer",
{},
mapPropsToStyleNames
)(ActionSheetContainer);
export { StyledActionSheetContainer as ActionSheetContainer };