-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BaseListItem.tsx
125 lines (115 loc) · 4.45 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
import React 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 useStyleUtils from '@hooks/useStyleUtils';
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,
selectMultipleStyle,
isDisabled = false,
shouldPreventDefaultFocusOnSelectRow = false,
canSelectMultiple = false,
onSelectRow,
onCheckboxPress,
onDismissError = () => {},
rightHandSideComponent,
keyForList,
errors,
pendingAction,
FooterComponent,
children,
}: BaseListItemProps<TItem>) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {hovered, bind} = useHover();
const rightHandSideComponentRender = () => {
if (canSelectMultiple || !rightHandSideComponent) {
return null;
}
if (typeof rightHandSideComponent === 'function') {
return rightHandSideComponent(item);
}
return rightHandSideComponent;
};
const handleCheckboxPress = () => {
if (onCheckboxPress) {
onCheckboxPress(item);
} else {
onSelectRow(item);
}
};
return (
<OfflineWithFeedback
onClose={() => onDismissError(item)}
pendingAction={pendingAction}
errors={errors}
errorRowStyles={styles.ph5}
>
<PressableWithFeedback
// eslint-disable-next-line react/jsx-props-no-spreading
{...bind}
onPress={() => onSelectRow(item)}
disabled={isDisabled}
accessibilityLabel={item.text}
role={CONST.ROLE.BUTTON}
hoverDimmingValue={1}
hoverStyle={!item.isSelected && styles.hoveredComponentBG}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
onMouseDown={shouldPreventDefaultFocusOnSelectRow ? (e) => e.preventDefault() : undefined}
nativeID={keyForList}
style={pressableStyle}
>
<View style={wrapperStyle}>
{canSelectMultiple && (
<PressableWithFeedback
accessibilityLabel={item.text}
role={CONST.ROLE.BUTTON}
disabled={isDisabled}
onPress={handleCheckboxPress}
style={[styles.cursorUnset, StyleUtils.getCheckboxPressableStyle()]}
>
<View style={selectMultipleStyle}>
{item.isSelected && (
<Icon
src={Expensicons.Checkmark}
fill={theme.textLight}
height={14}
width={14}
/>
)}
</View>
</PressableWithFeedback>
)}
{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>
)}
{rightHandSideComponentRender()}
</View>
{FooterComponent}
</PressableWithFeedback>
</OfflineWithFeedback>
);
}
BaseListItem.displayName = 'BaseListItem';
export default BaseListItem;