-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
OptionRow.tsx
342 lines (313 loc) · 16.7 KB
/
OptionRow.tsx
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import lodashIsEqual from 'lodash/isEqual';
import React, {useEffect, useRef, useState} from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {InteractionManager, StyleSheet, View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
import type {OptionData} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import Button from './Button';
import DisplayNames from './DisplayNames';
import Hoverable from './Hoverable';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import MultipleAvatars from './MultipleAvatars';
import OfflineWithFeedback from './OfflineWithFeedback';
import PressableWithFeedback from './Pressable/PressableWithFeedback';
import SelectCircle from './SelectCircle';
import SubscriptAvatar from './SubscriptAvatar';
import Text from './Text';
type OptionRowProps = {
/** Style for hovered state */
hoverStyle?: StyleProp<ViewStyle>;
/** Option to allow the user to choose from can be type 'report' or 'user' */
option: OptionData;
/** Whether this option is currently in focus so we can modify its style */
optionIsFocused?: boolean;
/** A function that is called when an option is selected. Selected option is passed as a param */
onSelectRow?: (option: OptionData, refElement: View | HTMLDivElement | null) => void | Promise<void>;
/** Whether we should show the selected state */
showSelectedState?: boolean;
/** Whether to show a button pill instead of a tickbox */
shouldShowSelectedStateAsButton?: boolean;
/** Text for button pill */
selectedStateButtonText?: string;
/** Callback to fire when the multiple selector (tickbox or button) is clicked */
onSelectedStatePressed?: (option: OptionData) => void;
/** Whether we highlight selected option */
highlightSelected?: boolean;
/** Whether this item is selected */
isSelected?: boolean;
/** Display the text of the option in bold font style */
boldStyle?: boolean;
/** Whether to show the title tooltip */
showTitleTooltip?: boolean;
/** Whether this option should be disabled */
isDisabled?: boolean;
/** Whether to show a line separating options in list */
shouldHaveOptionSeparator?: boolean;
/** Whether to remove the lateral padding and align the content with the margins */
shouldDisableRowInnerPadding?: boolean;
/** Whether to prevent default focusing on select */
shouldPreventDefaultFocusOnSelectRow?: boolean;
/** Whether to wrap large text up to 2 lines */
isMultilineSupported?: boolean;
/** Display name and alternate text style */
style?: StyleProp<TextStyle>;
/** Hovered background color */
backgroundColor?: string;
/** Key used internally by React */
keyForList?: string;
};
function OptionRow({
option,
onSelectRow,
style,
hoverStyle,
selectedStateButtonText,
keyForList,
isDisabled: isOptionDisabled = false,
isMultilineSupported = false,
shouldShowSelectedStateAsButton = false,
highlightSelected = false,
shouldHaveOptionSeparator = false,
showTitleTooltip = false,
optionIsFocused = false,
boldStyle = false,
onSelectedStatePressed = () => {},
backgroundColor,
isSelected = false,
showSelectedState = false,
shouldDisableRowInnerPadding = false,
shouldPreventDefaultFocusOnSelectRow = false,
}: OptionRowProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const pressableRef = useRef<View | HTMLDivElement>(null);
const [isDisabled, setIsDisabled] = useState(isOptionDisabled);
useEffect(() => {
setIsDisabled(isOptionDisabled);
}, [isOptionDisabled]);
const text = option.text ?? '';
const fullTitle = isMultilineSupported ? text.trimStart() : text;
const indentsLength = text.length - fullTitle.length;
const paddingLeft = Math.floor(indentsLength / CONST.INDENTS.length) * styles.ml3.marginLeft;
const textStyle = optionIsFocused ? styles.sidebarLinkActiveText : styles.sidebarLinkText;
const textUnreadStyle = boldStyle || option.boldStyle ? [textStyle, styles.sidebarLinkTextBold] : [textStyle];
const displayNameStyle: StyleProp<TextStyle> = [
styles.optionDisplayName,
textUnreadStyle,
style,
styles.pre,
isDisabled ? styles.optionRowDisabled : {},
isMultilineSupported ? {paddingLeft} : {},
];
const alternateTextStyle: StyleProp<TextStyle> = [
textStyle,
styles.optionAlternateText,
styles.textLabelSupporting,
style,
(option.alternateTextMaxLines ?? 1) === 1 ? styles.pre : styles.preWrap,
];
const contentContainerStyles = [styles.flex1];
const sidebarInnerRowStyle = StyleSheet.flatten([styles.chatLinkRowPressable, styles.flexGrow1, styles.optionItemAvatarNameWrapper, styles.optionRow, styles.justifyContentCenter]);
const flattenHoverStyle = StyleSheet.flatten(hoverStyle);
const hoveredStyle = hoverStyle ? flattenHoverStyle : styles.sidebarLinkHover;
const hoveredBackgroundColor = hoveredStyle?.backgroundColor ? (hoveredStyle.backgroundColor as string) : backgroundColor;
const focusedBackgroundColor = styles.sidebarLinkActive.backgroundColor;
const isMultipleParticipant = (option.participantsList?.length ?? 0) > 1;
// We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade.
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((option.participantsList ?? (option.accountID ? [option] : [])).slice(0, 10), isMultipleParticipant);
let subscriptColor = theme.appBG;
if (optionIsFocused) {
subscriptColor = focusedBackgroundColor;
}
return (
<OfflineWithFeedback
pendingAction={option.pendingAction}
errors={option.allReportErrors}
shouldShowErrorMessages={false}
needsOffscreenAlphaCompositing
>
<Hoverable>
{(hovered) => (
<PressableWithFeedback
nativeID={keyForList}
ref={pressableRef}
onPress={(e) => {
if (!onSelectRow) {
return;
}
setIsDisabled(true);
if (e) {
e.preventDefault();
}
let result = onSelectRow(option, pressableRef.current);
if (!(result instanceof Promise)) {
result = Promise.resolve();
}
InteractionManager.runAfterInteractions(() => {
result?.finally(() => setIsDisabled(isOptionDisabled));
});
}}
disabled={isDisabled}
style={[
styles.flexRow,
styles.alignItemsCenter,
styles.justifyContentBetween,
styles.sidebarLink,
!isOptionDisabled && styles.cursorPointer,
shouldDisableRowInnerPadding ? null : styles.sidebarLinkInner,
optionIsFocused ? styles.sidebarLinkActive : null,
shouldHaveOptionSeparator && styles.borderTop,
!onSelectRow && !isOptionDisabled ? styles.cursorDefault : null,
]}
accessibilityLabel={option.text ?? ''}
role={CONST.ROLE.BUTTON}
hoverDimmingValue={1}
hoverStyle={!optionIsFocused ? hoverStyle ?? styles.sidebarLinkHover : undefined}
needsOffscreenAlphaCompositing={(option.icons?.length ?? 0) >= 2}
onMouseDown={shouldPreventDefaultFocusOnSelectRow ? (event) => event.preventDefault() : undefined}
>
<View style={sidebarInnerRowStyle}>
<View style={[styles.flexRow, styles.alignItemsCenter]}>
{!!option.icons?.length &&
(option.shouldShowSubscript ? (
<SubscriptAvatar
mainAvatar={option.icons[0]}
secondaryAvatar={option.icons[1]}
backgroundColor={hovered && !optionIsFocused ? hoveredBackgroundColor : subscriptColor}
size={CONST.AVATAR_SIZE.DEFAULT}
/>
) : (
<MultipleAvatars
icons={option.icons}
size={CONST.AVATAR_SIZE.DEFAULT}
secondAvatarStyle={[StyleUtils.getBackgroundAndBorderStyle(hovered && !optionIsFocused ? hoveredBackgroundColor : subscriptColor)]}
shouldShowTooltip={showTitleTooltip && OptionsListUtils.shouldOptionShowTooltip(option)}
/>
))}
<View style={contentContainerStyles}>
<DisplayNames
accessibilityLabel={translate('accessibilityHints.chatUserDisplayNames')}
fullTitle={fullTitle}
displayNamesWithTooltips={displayNamesWithTooltips}
tooltipEnabled={showTitleTooltip}
numberOfLines={isMultilineSupported ? 2 : 1}
textStyles={displayNameStyle}
shouldUseFullTitle={
!!option.isChatRoom || !!option.isPolicyExpenseChat || !!option.isMoneyRequestReport || !!option.isThread || !!option.isTaskReport
}
/>
{option.alternateText ? (
<Text
style={alternateTextStyle}
numberOfLines={option.alternateTextMaxLines ?? 1}
>
{option.alternateText}
</Text>
) : null}
</View>
{option.descriptiveText ? (
<View style={[styles.flexWrap, styles.pl2]}>
<Text style={[styles.textLabel]}>{option.descriptiveText}</Text>
</View>
) : null}
{!isSelected && option.brickRoadIndicator === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR && (
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<Icon
src={Expensicons.DotIndicator}
fill={theme.danger}
/>
</View>
)}
{!isSelected && option.brickRoadIndicator === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO && (
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<Icon
src={Expensicons.DotIndicator}
fill={theme.iconSuccessFill}
/>
</View>
)}
{showSelectedState &&
(shouldShowSelectedStateAsButton && !isSelected ? (
<Button
style={[styles.pl2]}
text={selectedStateButtonText ?? translate('common.select')}
onPress={() => onSelectedStatePressed(option)}
small
shouldUseDefaultHover={false}
/>
) : (
<PressableWithFeedback
onPress={() => onSelectedStatePressed(option)}
disabled={isDisabled}
role={CONST.ROLE.BUTTON}
accessibilityLabel={CONST.ROLE.BUTTON}
style={[styles.ml2, styles.optionSelectCircle]}
>
<SelectCircle
isChecked={isSelected}
selectCircleStyles={styles.ml0}
/>
</PressableWithFeedback>
))}
{isSelected && highlightSelected && (
<View style={styles.defaultCheckmarkWrapper}>
<Icon
src={Expensicons.Checkmark}
fill={theme.iconSuccessFill}
/>
</View>
)}
</View>
</View>
{!!option.customIcon && (
<View
style={[styles.flexRow, styles.alignItemsCenter]}
accessible={false}
>
<View>
<Icon
src={option.customIcon.src}
fill={option.customIcon.color}
/>
</View>
</View>
)}
</PressableWithFeedback>
)}
</Hoverable>
</OfflineWithFeedback>
);
}
OptionRow.displayName = 'OptionRow';
export default React.memo(
OptionRow,
(prevProps, nextProps) =>
prevProps.isDisabled === nextProps.isDisabled &&
prevProps.isMultilineSupported === nextProps.isMultilineSupported &&
prevProps.isSelected === nextProps.isSelected &&
prevProps.shouldHaveOptionSeparator === nextProps.shouldHaveOptionSeparator &&
prevProps.selectedStateButtonText === nextProps.selectedStateButtonText &&
prevProps.showSelectedState === nextProps.showSelectedState &&
prevProps.highlightSelected === nextProps.highlightSelected &&
prevProps.showTitleTooltip === nextProps.showTitleTooltip &&
lodashIsEqual(prevProps.option.icons, nextProps.option.icons) &&
prevProps.optionIsFocused === nextProps.optionIsFocused &&
prevProps.option.text === nextProps.option.text &&
prevProps.option.alternateText === nextProps.option.alternateText &&
prevProps.option.descriptiveText === nextProps.option.descriptiveText &&
prevProps.option.brickRoadIndicator === nextProps.option.brickRoadIndicator &&
prevProps.option.shouldShowSubscript === nextProps.option.shouldShowSubscript &&
prevProps.option.ownerAccountID === nextProps.option.ownerAccountID &&
prevProps.option.subtitle === nextProps.option.subtitle &&
prevProps.option.pendingAction === nextProps.option.pendingAction &&
prevProps.option.customIcon === nextProps.option.customIcon,
);