-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathOverflowButton.js
115 lines (104 loc) · 2.74 KB
/
OverflowButton.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
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
/*
* @flow
*/
import * as React from 'react';
import {
View,
UIManager,
findNodeHandle,
StyleSheet,
ActionSheetIOS,
Platform,
} from 'react-native';
// TODO import based on global.Expo?
import Icon from 'react-native-vector-icons/MaterialIcons';
import { HeaderButton } from './HeaderButton';
export const textTransformer = (label: string) =>
Platform.OS === 'ios' ? label.charAt(0).toUpperCase() + label.substr(1) : label.toUpperCase();
type Props = {
hiddenButtons: Array<React.Element<*>>,
color: string,
OverflowIcon?: React.Node,
cancelButtonLabel: string,
buttonWrapperStyle?: Object,
};
export class OverflowButton extends React.Component<Props> {
overflowRef: ?View;
static defaultProps = {
color: 'grey',
cancelButtonLabel: 'Cancel',
};
setOverflowRef = (ref: ?View) => {
this.overflowRef = ref;
};
render() {
const { OverflowIcon, buttonWrapperStyle } = this.props;
const ButtonElement = OverflowIcon ? (
OverflowIcon
) : (
<Icon name="more-vert" size={23} color={this.props.color} />
);
return (
<View>
<View ref={this.setOverflowRef} style={styles.overflowMenuView} />
<HeaderButton
onPress={this.showOverflowPopup}
ButtonElement={ButtonElement}
buttonWrapperStyle={[styles.icon, buttonWrapperStyle]}
/>
</View>
);
}
showOverflowPopup = () => {
Platform.OS === 'android' ? this.showPopupAndroid() : this.showPopupIos();
};
showPopupAndroid() {
UIManager.showPopupMenu(
findNodeHandle(this.overflowRef),
this.props.hiddenButtons.map(btn => btn.props.title),
err => {
console.debug(`popup error ${err}`);
},
this.onHiddenItemPress
);
}
onHiddenItemPress = (eventName: string, index: number) => {
if (eventName !== 'itemSelected') return;
this.props.hiddenButtons[index].props.onPress();
};
showPopupIos() {
const actionTitles = this.props.hiddenButtons.map(btn => btn.props.title);
actionTitles.push(this.props.cancelButtonLabel);
ActionSheetIOS.showActionSheetWithOptions(
{
options: actionTitles,
cancelButtonIndex: actionTitles.length - 1,
},
(buttonIndex: number) => {
if (buttonIndex !== actionTitles.length - 1) {
this.onHiddenItemPress('itemSelected', buttonIndex);
}
}
);
}
}
const styles = StyleSheet.create({
overflowMenuView: {
position: 'absolute',
top: -10,
// TODO android actually has a little gap on the right of the menu
right: 0,
backgroundColor: 'transparent',
width: 1,
height: 1,
},
icon: {
marginTop: 2,
...Platform.select({
android: {
marginRight: 9,
marginLeft: 7,
},
}),
},
});