-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathApp.tsx
More file actions
312 lines (296 loc) · 8.93 KB
/
App.tsx
File metadata and controls
312 lines (296 loc) · 8.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {
ActionSheetProvider,
connectActionSheet,
ActionSheetProps,
} from '@expo/react-native-action-sheet';
import { Entypo } from '@expo/vector-icons';
import * as React from 'react';
import { useState } from 'react';
import {
Modal,
StyleSheet,
Text,
View,
ScrollView,
SafeAreaView,
TouchableOpacity,
Share,
Platform,
} from 'react-native';
import ShowActionSheetButton from './ShowActionSheetButton';
type Props = ActionSheetProps & {
useCustomActionSheet: boolean;
setUseCustomActionSheet: (next: boolean) => void;
};
interface State {
selectedIndex?: number | null;
isModalOpen: boolean;
}
class App extends React.Component<Props, State> {
state: State = {
selectedIndex: null,
isModalOpen: false,
};
_updateSelectionText = (selectedIndex?: number) => {
this.setState({
selectedIndex,
});
};
_renderSelectionText = () => {
const { selectedIndex } = this.state;
const text =
selectedIndex === null || selectedIndex === undefined
? 'No Option Selected'
: `Option #${selectedIndex + 1} Selected`;
return <Text style={styles.selectionText}>{text}</Text>;
};
_renderSectionHeader = (text: string) => {
return <Text style={styles.sectionHeaderText}>{text}</Text>;
};
_toggleModal = () => {
this.setState((prevState) => ({ isModalOpen: !prevState.isModalOpen }));
};
async _onShare() {
try {
const result = await Share.share({
message: 'React Native | A framework for building native apps using React',
});
if (result.action === Share.sharedAction) {
if (result.activityType) {
// shared with activity type of result.activityType
} else {
// shared
}
} else if (result.action === Share.dismissedAction) {
// dismissed
}
} catch (error) {}
}
_renderButtons() {
const { showActionSheetWithOptions } = this.props;
return (
<View
style={{
alignItems: 'center',
}}>
<View
style={{
alignItems: 'center',
display: Platform.OS === 'ios' ? 'flex' : 'none',
}}>
{this._renderSectionHeader('Use Custom Action Sheet')}
<Text style={{ marginBottom: 10 }}>
On iOS the default action sheet will be the native UI. However, you can optionally
enable the custom JS action sheet by setting the useCustomActionSheet prop on the
provider.
</Text>
<Entypo.Button
backgroundColor={this.props.useCustomActionSheet ? '#3e3e3e' : 'white'}
name={this.props.useCustomActionSheet ? 'check' : 'circle'}
color={this.props.useCustomActionSheet ? '#fff' : '#3e3e3e'}
style={{
borderColor: '#3e3e3e',
borderWidth: 2,
}}
onPress={() => this.props.setUseCustomActionSheet(!this.props.useCustomActionSheet)}>
<Text
style={{
fontSize: 15,
color: this.props.useCustomActionSheet ? '#fff' : '#3e3e3e',
}}>
Use Custom Action Sheet
</Text>
</Entypo.Button>
</View>
{this._renderSectionHeader('Universal Options')}
<ShowActionSheetButton
title="Options Only"
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Title"
withTitle
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Title & Message"
withTitle
withMessage
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Cancel Button Tint Color"
withCancelButtonTintColor
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="iPad Anchor"
withAnchor
withTitle
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Nested Action Sheets"
onSelection={(index) => {
if (!index || index < 3) {
showActionSheetWithOptions(
{
title: 'Sub Action Sheet',
options: ['One', 'Two', 'Three', 'Done'],
cancelButtonIndex: 3,
},
this._updateSelectionText
);
}
}}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Share Menu"
showActionSheetWithOptions={() =>
showActionSheetWithOptions(
{
title: 'Share Menu',
options: ['Share', 'Cancel'],
cancelButtonIndex: 1,
},
(i) => {
i === 0 && this._onShare();
}
)
}
/>
{this._renderSectionHeader('Android-Only Options')}
<ShowActionSheetButton
title="Icons"
withIcons
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Title, Message, & Icons"
withTitle
withMessage
withIcons
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Use Separators"
withTitle
withIcons
withSeparators
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="Custom Styles"
withTitle
withMessage
withIcons
withCustomStyles
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<ShowActionSheetButton
title="More Options"
withExtendedOptions
stickyCancel
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
withSeparators
/>
{this._renderSectionHeader('Special Cases')}
<TouchableOpacity onPress={this._toggleModal}>
<Text style={styles.link}>Open Modal</Text>
</TouchableOpacity>
{this.state.isModalOpen && (
<Modal>
<View style={{ flex: 1, padding: 30 }}>
<ShowActionSheetButton
useModal
title="Options Only"
onSelection={this._updateSelectionText}
showActionSheetWithOptions={showActionSheetWithOptions}
/>
<TouchableOpacity onPress={this._toggleModal}>
<Text style={styles.link}>Close Modal</Text>
</TouchableOpacity>
</View>
</Modal>
)}
</View>
);
}
render() {
return (
<SafeAreaView style={styles.flex}>
<ScrollView style={styles.flex} contentContainerStyle={styles.contentContainer}>
<Text style={styles.headerText}>
{
'Hello!\n\nThis is a simple example app to demonstrate @expo/react-native-action-sheet.'
}
</Text>
{this._renderButtons()}
{this._renderSelectionText()}
<Text style={styles.notes}>
Note: Icons and custom text styles are only available on Android. Separators can only be
toggled on Android; they always show on iOS.
</Text>
</ScrollView>
</SafeAreaView>
);
}
}
const ConnectedApp = connectActionSheet<any>(App);
export default function WrappedApp() {
const [useCustomActionSheet, setUseCustomActionSheet] = useState(false);
return (
<ActionSheetProvider useCustomActionSheet={useCustomActionSheet}>
<ConnectedApp
useCustomActionSheet={useCustomActionSheet}
setUseCustomActionSheet={setUseCustomActionSheet}
/>
</ActionSheetProvider>
);
}
const styles = StyleSheet.create({
flex: {
flex: 1,
},
contentContainer: {
padding: 16,
paddingVertical: 20,
},
headerText: {
textAlign: 'center',
fontSize: 16,
marginBottom: 10,
},
notes: {
marginTop: 32,
},
sectionHeaderText: {
color: 'orange',
textAlign: 'center',
fontWeight: 'bold',
fontSize: 20,
marginTop: 20,
marginBottom: 10,
},
selectionText: {
textAlign: 'center',
color: 'blue',
fontSize: 16,
marginTop: 20,
},
link: {
fontSize: 15,
textDecorationLine: 'underline',
},
});