-
Notifications
You must be signed in to change notification settings - Fork 3k
/
HeaderWithCloseButton.js
executable file
·227 lines (196 loc) · 8.67 KB
/
HeaderWithCloseButton.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
View, Keyboard, Pressable,
} from 'react-native';
import styles from '../styles/styles';
import Header from './Header';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import Tooltip from './Tooltip';
import ThreeDotsMenu, {ThreeDotsMenuItemPropTypes} from './ThreeDotsMenu';
import VirtualKeyboard from '../libs/VirtualKeyboard';
import getButtonState from '../libs/getButtonState';
import * as StyleUtils from '../styles/StyleUtils';
import withDelayToggleButtonState, {withDelayToggleButtonStatePropTypes} from './withDelayToggleButtonState';
import compose from '../libs/compose';
const propTypes = {
/** Title of the Header */
title: PropTypes.string,
/** Subtitle of the header */
subtitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
/** Method to trigger when pressing download button of the header */
onDownloadButtonPress: PropTypes.func,
/** Method to trigger when pressing close button of the header */
onCloseButtonPress: PropTypes.func,
/** Method to trigger when pressing back button of the header */
onBackButtonPress: PropTypes.func,
/** Method to trigger when pressing more options button of the header */
onThreeDotsButtonPress: PropTypes.func,
/** Whether we should show a back icon */
shouldShowBackButton: PropTypes.bool,
/** Whether we should show a border on the bottom of the Header */
shouldShowBorderBottom: PropTypes.bool,
/** Whether we should show a download button */
shouldShowDownloadButton: PropTypes.bool,
/** Whether we should show a get assistance (question mark) button */
shouldShowGetAssistanceButton: PropTypes.bool,
/** Whether we should show a more options (threedots) button */
shouldShowThreeDotsButton: PropTypes.bool,
/** List of menu items for more(three dots) menu */
threeDotsMenuItems: ThreeDotsMenuItemPropTypes,
/** The anchor position of the menu */
threeDotsAnchorPosition: PropTypes.shape({
top: PropTypes.number,
right: PropTypes.number,
bottom: PropTypes.number,
left: PropTypes.number,
}),
/** Whether we should show a close button */
shouldShowCloseButton: PropTypes.bool,
/** Whether we should show the step counter */
shouldShowStepCounter: PropTypes.bool,
/** The guides call taskID to associate with the get assistance button, if we show it */
guidesCallTaskID: PropTypes.string,
/** Data to display a step counter in the header */
stepCounter: PropTypes.shape({
step: PropTypes.number,
total: PropTypes.number,
}),
...withLocalizePropTypes,
...withDelayToggleButtonStatePropTypes,
};
const defaultProps = {
title: '',
subtitle: '',
onDownloadButtonPress: () => {},
onCloseButtonPress: () => {},
onBackButtonPress: () => {},
onThreeDotsButtonPress: () => {},
shouldShowBackButton: false,
shouldShowBorderBottom: false,
shouldShowDownloadButton: false,
shouldShowGetAssistanceButton: false,
shouldShowThreeDotsButton: false,
shouldShowCloseButton: true,
shouldShowStepCounter: true,
guidesCallTaskID: '',
stepCounter: null,
threeDotsMenuItems: [],
threeDotsAnchorPosition: {
top: 0,
left: 0,
},
};
class HeaderWithCloseButton extends Component {
constructor(props) {
super(props);
this.triggerButtonCompleteAndDownload = this.triggerButtonCompleteAndDownload.bind(this);
}
/**
* Method to trigger parent onDownloadButtonPress to download the file
* and toggleDelayButtonState to set button state and revert it after sometime.
*/
triggerButtonCompleteAndDownload() {
if (this.props.isDelayButtonStateComplete) {
return;
}
this.props.onDownloadButtonPress();
this.props.toggleDelayButtonState(true);
}
render() {
return (
<View style={[styles.headerBar, this.props.shouldShowBorderBottom && styles.borderBottom]}>
<View style={[
styles.dFlex,
styles.flexRow,
styles.alignItemsCenter,
styles.flexGrow1,
styles.justifyContentBetween,
styles.overflowHidden,
]}
>
{this.props.shouldShowBackButton && (
<Tooltip text={this.props.translate('common.back')}>
<Pressable
onPress={() => {
if (VirtualKeyboard.isOpen()) {
Keyboard.dismiss();
}
this.props.onBackButtonPress();
}}
style={[styles.touchableButtonImage]}
>
<Icon src={Expensicons.BackArrow} />
</Pressable>
</Tooltip>
)}
<Header
title={this.props.title}
subtitle={this.props.stepCounter && this.props.shouldShowStepCounter ? this.props.translate('stepCounter', this.props.stepCounter) : this.props.subtitle}
/>
<View style={[styles.reportOptions, styles.flexRow, styles.pr5]}>
{
this.props.shouldShowDownloadButton && (
<Tooltip text={this.props.translate('common.download')}>
<Pressable
onPress={this.triggerButtonCompleteAndDownload}
style={[styles.touchableButtonImage]}
>
<Icon
src={Expensicons.Download}
fill={StyleUtils.getIconFillColor(getButtonState(false, false, this.props.isDelayButtonStateComplete))}
/>
</Pressable>
</Tooltip>
)
}
{this.props.shouldShowGetAssistanceButton
&& (
<Tooltip text={this.props.translate('getAssistancePage.questionMarkButtonTooltip')}>
<Pressable
onPress={() => Navigation.navigate(ROUTES.getGetAssistanceRoute(this.props.guidesCallTaskID))}
style={[styles.touchableButtonImage, styles.mr0]}
accessibilityRole="button"
accessibilityLabel={this.props.translate('getAssistancePage.questionMarkButtonTooltip')}
>
<Icon src={Expensicons.QuestionMark} />
</Pressable>
</Tooltip>
)}
{this.props.shouldShowThreeDotsButton && (
<ThreeDotsMenu
menuItems={this.props.threeDotsMenuItems}
onIconPress={this.props.onThreeDotsButtonPress}
iconStyles={[styles.mr0]}
anchorPosition={this.props.threeDotsAnchorPosition}
/>
)}
{this.props.shouldShowCloseButton
&& (
<Tooltip text={this.props.translate('common.close')}>
<Pressable
onPress={this.props.onCloseButtonPress}
style={[styles.touchableButtonImage, styles.mr0]}
accessibilityRole="button"
accessibilityLabel={this.props.translate('common.close')}
>
<Icon src={Expensicons.Close} />
</Pressable>
</Tooltip>
)}
</View>
</View>
</View>
);
}
}
HeaderWithCloseButton.propTypes = propTypes;
HeaderWithCloseButton.defaultProps = defaultProps;
export default compose(
withLocalize,
withDelayToggleButtonState,
)(HeaderWithCloseButton);