-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathOptionRow.js
247 lines (222 loc) · 9.29 KB
/
OptionRow.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import _ from 'underscore';
import React, {memo} from 'react';
import PropTypes from 'prop-types';
import {
Text,
TouchableOpacity,
View,
StyleSheet,
} from 'react-native';
import styles, {getBackgroundAndBorderStyle, getBackgroundColorStyle} from '../../../styles/styles';
import {optionPropTypes} from './optionPropTypes';
import Icon from '../../../components/Icon';
import {Pencil, PinCircle, Checkmark} from '../../../components/Icon/Expensicons';
import MultipleAvatars from '../../../components/MultipleAvatars';
import themeColors from '../../../styles/themes/default';
import Hoverable from '../../../components/Hoverable';
import OptionRowTitle from './OptionRowTitle';
import IOUBadge from '../../../components/IOUBadge';
import colors from '../../../styles/colors';
const propTypes = {
// Background Color of the Option Row
backgroundColor: PropTypes.string,
// Style for hovered state
// eslint-disable-next-line react/forbid-prop-types
hoverStyle: PropTypes.object,
// Option to allow the user to choose from can be type 'report' or 'user'
option: optionPropTypes.isRequired,
// Whether this option is currently in focus so we can modify its style
optionIsFocused: PropTypes.bool.isRequired,
// A function that is called when an option is selected. Selected option is passed as a param
onSelectRow: PropTypes.func.isRequired,
// A flag to indicate whether to show additional optional states, such as pin and draft icons
hideAdditionalOptionStates: PropTypes.bool,
// Whether we should show the selected state
showSelectedState: PropTypes.bool,
// Whether this item is selected
isSelected: PropTypes.bool,
// Force the text style to be the unread style
forceTextUnreadStyle: PropTypes.bool,
// Whether to show the title tooltip
showTitleTooltip: PropTypes.bool,
// Toggle between compact and default view
mode: PropTypes.oneOf(['compact', 'default']),
};
const defaultProps = {
backgroundColor: colors.white,
hoverStyle: styles.sidebarLinkHover,
hideAdditionalOptionStates: false,
showSelectedState: false,
isSelected: false,
forceTextUnreadStyle: false,
showTitleTooltip: false,
mode: 'default',
};
const OptionRow = ({
backgroundColor,
hoverStyle,
option,
optionIsFocused,
onSelectRow,
hideAdditionalOptionStates,
showSelectedState,
isSelected,
forceTextUnreadStyle,
showTitleTooltip,
mode,
}) => {
const textStyle = optionIsFocused
? styles.sidebarLinkActiveText
: styles.sidebarLinkText;
const textUnreadStyle = (option.isUnread || forceTextUnreadStyle)
? [textStyle, styles.sidebarLinkTextUnread] : [textStyle];
const displayNameStyle = mode === 'compact'
? [styles.optionDisplayName, textUnreadStyle, styles.optionDisplayNameCompact, styles.mr2]
: [styles.optionDisplayName, textUnreadStyle];
const alternateTextStyle = mode === 'compact'
? [textStyle, styles.optionAlternateText, styles.optionAlternateTextCompact]
: [textStyle, styles.optionAlternateText, styles.mt1];
const contentContainerStyles = mode === 'compact'
? [styles.flex1, styles.flexRow, styles.overflowHidden, styles.alignItemsCenter]
: [styles.flex1];
const sidebarInnerRowStyle = StyleSheet.flatten(mode === 'compact' ? [
styles.chatLinkRowPressable,
styles.flexGrow1,
styles.optionItemAvatarNameWrapper,
styles.sidebarInnerRowSmall,
styles.justifyContentCenter,
] : [
styles.chatLinkRowPressable,
styles.flexGrow1,
styles.optionItemAvatarNameWrapper,
styles.sidebarInnerRow,
styles.justifyContentCenter,
]);
const hoveredBackgroundColor = hoverStyle && hoverStyle.backgroundColor
? hoverStyle.backgroundColor
: backgroundColor;
const focusedBackgroundColor = styles.sidebarLinkActive.backgroundColor;
return (
<Hoverable>
{hovered => (
<TouchableOpacity
onPress={() => onSelectRow(option)}
activeOpacity={0.8}
style={[
styles.flexRow,
styles.alignItemsCenter,
styles.justifyContentBetween,
styles.sidebarLink,
styles.sidebarLinkInner,
getBackgroundColorStyle(backgroundColor),
optionIsFocused ? styles.sidebarLinkActive : null,
hovered && !optionIsFocused ? hoverStyle : null,
]}
>
<View style={sidebarInnerRowStyle}>
<View
style={[
styles.flexRow,
styles.alignItemsCenter,
]}
>
{
!_.isEmpty(option.icons)
&& (
<MultipleAvatars
avatarImageURLs={option.icons}
size={mode === 'compact' ? 'small' : 'default'}
secondAvatarStyle={[
getBackgroundAndBorderStyle(backgroundColor),
optionIsFocused
? getBackgroundAndBorderStyle(focusedBackgroundColor)
: undefined,
hovered && !optionIsFocused
? getBackgroundAndBorderStyle(hoveredBackgroundColor)
: undefined,
]}
/>
)
}
<View style={contentContainerStyles}>
<OptionRowTitle
option={option}
tooltipEnabled={showTitleTooltip}
numberOfLines={1}
style={displayNameStyle}
reportID={option.reportID}
/>
{option.alternateText ? (
<Text
style={alternateTextStyle}
numberOfLines={1}
>
{option.alternateText}
</Text>
) : null}
</View>
{showSelectedState && (
<View style={[styles.selectCircle]}>
{isSelected && (
<Icon src={Checkmark} fill={themeColors.iconSuccessFill} />
)}
</View>
)}
</View>
</View>
{!hideAdditionalOptionStates && (
<View style={[styles.flexRow, styles.alignItemsCenter]}>
{option.hasDraftComment && (
<View style={styles.ml2}>
<Icon src={Pencil} />
</View>
)}
{option.hasOutstandingIOU && (
<IOUBadge iouReportID={option.iouReportID} />
)}
{option.isPinned && (
<View style={styles.ml2}>
<Icon src={PinCircle} />
</View>
)}
</View>
)}
</TouchableOpacity>
)}
</Hoverable>
);
};
OptionRow.propTypes = propTypes;
OptionRow.defaultProps = defaultProps;
OptionRow.displayName = 'OptionRow';
// It it very important to use React.memo here so SectionList items will not unnecessarily re-render
export default memo(OptionRow, (prevProps, nextProps) => {
if (prevProps.optionIsFocused !== nextProps.optionIsFocused) {
return false;
}
if (prevProps.isSelected !== nextProps.isSelected) {
return false;
}
if (prevProps.mode !== nextProps.mode) {
return false;
}
if (prevProps.option.isUnread !== nextProps.option.isUnread) {
return false;
}
if (prevProps.option.alternateText !== nextProps.option.alternateText) {
return false;
}
if (prevProps.option.hasDraftComment !== nextProps.option.hasDraftComment) {
return false;
}
if (prevProps.option.isPinned !== nextProps.option.isPinned) {
return false;
}
if (prevProps.option.hasOutstandingIOU !== nextProps.option.hasOutstandingIOU) {
return false;
}
if (!_.isEqual(prevProps.option.icons, nextProps.option.icons)) {
return false;
}
return true;
});