-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathHeaderButtons.js
168 lines (151 loc) · 3.87 KB
/
HeaderButtons.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
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
/*
* @flow
*/
import * as React from 'react';
import { HeaderButton } from './HeaderButton';
import { StyleSheet, Platform, View, Text } from 'react-native';
import { OverflowButton, textTransformer } from './OverflowButton';
const OS_IOS = Platform.OS === 'ios';
type ItemProps = {
onPress: () => void,
title: string,
show: string,
IconElement?: React.Node,
iconName?: string,
color?: string,
iconSize?: number,
buttonStyle?: Object,
};
// TODO check RTL
class Item extends React.Component<ItemProps> {
static SHOW_ALWAYS = 'always';
static SHOW_NEVER = 'never';
static defaultProps = {
show: 'always',
};
render() {
return null;
}
}
type HeaderButtonsProps = {
children: React.Node,
left: boolean,
IconComponent?: React.ComponentType<*>,
iconSize?: number,
color?: string,
OverflowIcon?: React.Node,
overflowButtonWrapperStyle?: Object,
cancelButtonLabel?: string,
};
export class HeaderButtons extends React.Component<HeaderButtonsProps> {
static Item = Item;
static defaultProps = {
left: false,
};
render() {
const { visibleButtons, hiddenButtons } = getVisibleAndHiddenButtons(this.props);
const { color, OverflowIcon, cancelButtonLabel, overflowButtonWrapperStyle } = this.props;
return (
<View style={[styles.row, this.getEdgeMargin()]}>
{visibleButtons.length > 0 && this.renderVisibleButtons(visibleButtons)}
{hiddenButtons.length > 0 && (
<OverflowButton
color={color}
hiddenButtons={hiddenButtons}
OverflowIcon={OverflowIcon}
cancelButtonLabel={cancelButtonLabel}
buttonWrapperStyle={overflowButtonWrapperStyle}
/>
)}
</View>
);
}
getEdgeMargin() {
return this.props.left ? styles.extraEdgeMarginOnLeft : styles.extraEdgeMarginOnRight;
}
renderVisibleButtons(visibleButtons: Array<React.Element<*>>) {
return visibleButtons.map(btn => {
const { props: { title, IconElement } } = btn;
const ButtonElement = IconElement ? IconElement : this.renderVisibleButton(btn.props);
return <HeaderButton key={title} ButtonElement={ButtonElement} {...btn.props} />;
});
}
renderVisibleButton(itemProps: ItemProps) {
const { IconComponent, iconSize, color } = this.props;
const { iconName, title, buttonStyle } = itemProps;
return IconComponent && iconName ? (
<IconComponent
name={iconName}
color={color}
size={iconSize}
style={[styles.button, buttonStyle]}
/>
) : (
<Text style={[styles.text, { color }, buttonStyle]}>{textTransformer(title)}</Text>
);
}
}
function getVisibleAndHiddenButtons(props) {
let visibleButtons = [];
let hiddenButtons = [];
React.Children.forEach(props.children, child => {
if (child && typeof child === 'object') {
// TODO implement ifRoom, which will be tricky
if (!child.props.show || child.props.show === Item.SHOW_ALWAYS) {
visibleButtons.push(child);
} else {
hiddenButtons.push(child);
}
}
});
return {
visibleButtons,
hiddenButtons,
};
}
const styles = StyleSheet.create({
row: { flexDirection: 'row' },
extraEdgeMarginOnLeft: {
...Platform.select({
android: {
marginLeft: 5,
},
ios: {
marginLeft: 4,
},
}),
},
extraEdgeMarginOnRight: {
...Platform.select({
android: {
marginRight: 4,
},
ios: {
marginRight: 5,
},
}),
},
text: {
...Platform.select({
android: {
fontFamily: 'sans-serif-medium',
fontSize: 14,
marginHorizontal: 11,
},
ios: {
fontSize: 17,
marginHorizontal: 10,
},
}),
},
button: {
...Platform.select({
android: {
marginHorizontal: 11,
},
ios: {
marginHorizontal: 11,
},
}),
},
});