-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (59 loc) · 1.87 KB
/
index.js
File metadata and controls
79 lines (59 loc) · 1.87 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
import { NativeModules, ActionSheetIOS, Platform } from 'react-native';
class CCSActionSheet {
defCancel = 'Cancel';
setDefaultCancel(cancel:String) {
this.defCancel = cancel;
}
show(options, index = -1, etc = { title: '', message: '', androidCancelHide: false, cancelLabel : '', tintColor: '' }) {
this.isOptionObject = (options?.length && options[0] instanceof Object);
let buttons = [];
if (this.isOptionObject) {
this.options = options;
buttons = buttons.concat(this.options.map((v) => (v.label)));
}
else {
buttons = buttons.concat(options);
}
if (Platform.OS == 'ios' || !etc?.androidCancelHide) {
this.usedCancel = true;
buttons.push((etc?.cancelLabel ? etc?.cancelLabel : this.defCancel));
}
else {
this.usedCancel = false;
}
const native = (Platform.OS == 'ios') ? ActionSheetIOS : (Platform.OS == 'android') ? NativeModules.RNCActionsheet : null;
return new Promise((resolve, reject) => {
if (native == null) reject('Not Support Device');
try {
let params = {
options: buttons,
cancelButtonIndex: buttons.length - 1,
destructiveButtonIndex: index
};
if (etc?.title) params.title = etc.title;
if (etc?.message) params.message = etc.message;
if (etc?.tintColor) params.tintColor = etc.tintColor;
native.showActionSheetWithOptions(params, (buttonIndex) => {
if (buttonIndex == undefined || (this.usedCancel && buttonIndex == buttons.length - 1)) {
reject("User Cancelled");
return;
}
let o = {};
if (this.isOptionObject) {
o.item = this.options[buttonIndex];
}
o.index = buttonIndex;
o.label = buttons[buttonIndex];
o.isDestructive = (buttonIndex == index);
resolve(o);
});
}
catch(e) {
console.log(e);
reject(e);
}
});
}
}
const ActionSheet = new CCSActionSheet();
export default ActionSheet;