-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (84 loc) · 2.07 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* @flow */
import React from 'react'
import { Platform, ActionSheetIOS, NativeModules } from 'react-native'
const isIOS = Platform.OS === 'ios'
const ActionSheetNative = isIOS ? ActionSheetIOS : NativeModules.ActionSheet
const optionNames = [
'title',
'message',
'options',
'tintColor',
'cancelButtonIndex',
'destructiveButtonIndex',
]
type Props = {
title?: string,
message?: string,
options: Array<string>,
tintColor?: string,
cancelButtonIndex?: number,
destructiveButtonIndex?: number,
onPress: Function,
};
export default class ActionSheet extends React.Component<Props> {
componentDidMount() {
const options = this.props.options
if (!this.isArray(options) || options.length === 0) {
throw Error('Prop `options` must be an array and it must not be empty.')
}
}
shouldComponentUpdate() {
return false // handle on native thread only
}
isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]'
}
show() {
const {
title,
options,
tintColor,
cancelButtonIndex,
destructiveButtonIndex,
onPress,
message
} = this.props
if (isIOS) {
ActionSheetNative.showActionSheetWithOptions({ ...this.props }, onPress)
}
else {
const items = []
options.forEach((option, index) => {
if (index !== cancelButtonIndex) {
items.push({
title: option
})
}
});
ActionSheetNative.SheetView(
{
title: title,
items: items,
titleTextColor: '#000000',
itemTextColor: '#000000',
itemTintColor: tintColor || '#000000',
backgroundColor: '#FFFFFF',
delayDismissOnItemClick: false,
},
selectedIndex => {
if (typeof cancelButtonIndex !== 'undefined' && cancelButtonIndex !== null) {
onPress(selectedIndex + 1)
} else {
onPress(selectedIndex)
}
},
() => {
onPress(cancelButtonIndex)
}
);
}
}
render() {
return null
}
}