-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseListItem.tsx
134 lines (123 loc) · 4.9 KB
/
BaseListItem.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
import React, {useRef} from 'react';
import {View} from 'react-native';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import useHover from '@hooks/useHover';
import {useMouseContext} from '@hooks/useMouseContext';
import useSyncFocus from '@hooks/useSyncFocus';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import type {BaseListItemProps, ListItem} from './types';
function BaseListItem<TItem extends ListItem>({
item,
pressableStyle,
wrapperStyle,
containerStyle,
isDisabled = false,
shouldPreventDefaultFocusOnSelectRow = false,
shouldPreventEnterKeySubmit = false,
canSelectMultiple = false,
onSelectRow,
onDismissError = () => {},
rightHandSideComponent,
keyForList,
errors,
pendingAction,
FooterComponent,
children,
isFocused,
shouldSyncFocus = true,
onFocus = () => {},
hoverStyle,
}: BaseListItemProps<TItem>) {
const theme = useTheme();
const styles = useThemeStyles();
const {hovered, bind} = useHover();
const {isMouseDownOnInput, setMouseUp} = useMouseContext();
const pressableRef = useRef<View>(null);
// Sync focus on an item
useSyncFocus(pressableRef, !!isFocused, shouldSyncFocus);
const handleMouseUp = (e: React.MouseEvent<Element, MouseEvent>) => {
e.stopPropagation();
setMouseUp();
};
const rightHandSideComponentRender = () => {
if (canSelectMultiple || !rightHandSideComponent) {
return null;
}
if (typeof rightHandSideComponent === 'function') {
return rightHandSideComponent(item);
}
return rightHandSideComponent;
};
return (
<OfflineWithFeedback
onClose={() => onDismissError(item)}
pendingAction={pendingAction}
errors={errors}
errorRowStyles={styles.ph5}
contentContainerStyle={containerStyle}
>
<PressableWithFeedback
// eslint-disable-next-line react/jsx-props-no-spreading
{...bind}
ref={pressableRef}
onPress={(e) => {
if (isMouseDownOnInput) {
e?.stopPropagation(); // Preventing the click action
return;
}
if (shouldPreventEnterKeySubmit && e && 'key' in e && e.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) {
return;
}
onSelectRow(item);
}}
disabled={isDisabled && !item.isSelected}
accessibilityLabel={item.text ?? ''}
role={CONST.ROLE.BUTTON}
hoverDimmingValue={1}
hoverStyle={[!item.isDisabled && styles.hoveredComponentBG, hoverStyle]}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
onMouseDown={shouldPreventDefaultFocusOnSelectRow ? (e) => e.preventDefault() : undefined}
id={keyForList ?? ''}
style={pressableStyle}
onFocus={onFocus}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
tabIndex={item.tabIndex}
>
<View style={wrapperStyle}>
{typeof children === 'function' ? children(hovered) : children}
{!canSelectMultiple && item.isSelected && !rightHandSideComponent && (
<View
style={[styles.flexRow, styles.alignItemsCenter, styles.ml3]}
accessible={false}
>
<View>
<Icon
src={Expensicons.Checkmark}
fill={theme.success}
/>
</View>
</View>
)}
{!item.isSelected && !!item.brickRoadIndicator && (
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<Icon
src={Expensicons.DotIndicator}
fill={item.brickRoadIndicator === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO ? theme.iconSuccessFill : theme.danger}
/>
</View>
)}
{rightHandSideComponentRender()}
</View>
{FooterComponent}
</PressableWithFeedback>
</OfflineWithFeedback>
);
}
BaseListItem.displayName = 'BaseListItem';
export default BaseListItem;