-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·69 lines (57 loc) · 1.59 KB
/
index.js
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
'use strict';
import { Alert, NativeModules, Platform } from 'react-native';
const android = Platform.OS === 'android'
const Native = android ? NativeModules.RNCustomAlert : NativeModules.SweetAlertManager;
const DEFAULT_OPTIONS = {
title: '',
subTitle: '',
confirmButtonTitle: 'Ok',
confirmButtonColor: '#27ae60',
barColor: '',
otherButtonTitle: 'Cancel',
otherButtonColor: '#d63031',
style: 'success',
showCancel: false,
cancelable: false,
delay: 0
}
const CustomAlert = {
/**
* @param {DEFAULT_OPTIONS} options
* @param {*} callback
*/
showAlertWithOptions: (options, callback = () => {}) => {
Native.showAlertWithOptions(options ? options : DEFAULT_OPTIONS, (action) => {
if (action == 'error') {
defaultAlert(options, callback)
} else {
callback(action)
}
})
if (options.delay && !isNaN(options.delay) && parseInt(options.delay) > 0) {
setTimeout(Native.hideSweetAlert, parseInt(options.delay))
}
},
/**
* Dismisses the alert
*/
dismissAlert: () => { Native.hideSweetAlert() }
};
const defaultAlert = (options, callback = () => {}) => {
const buttons = []
buttons.push({
text: options.confirmButtonTitle || `Ok`,
onPress: () => callback('confirmed')
})
if (options.showCancel) {
buttons.push({
text: options.otherButtonTitle || `Cancel`,
onPress: () => callback('cancelled'),
style: `cancel`
})
}
const title = options.subTitle && options.title ? options.title : null
const subTitle = options.title && !options.subTitle ? options.title : options.subTitle || null
Alert.alert(title, subTitle, buttons)
}
export default CustomAlert