/
RenderListItem.js
201 lines (181 loc) · 5.82 KB
/
RenderListItem.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
import React, {
useMemo,
useCallback,
memo
} from 'react';
import {
Text,
TouchableOpacity
} from 'react-native';
import { LIST_MODE } from '../constants';
function RenderListItem({
rtl,
item,
label,
value,
parent,
selectable,
disabled,
props,
labelProps,
custom,
isSelected,
IconComponent,
TickIconComponent,
listItemContainerStyle,
listItemLabelStyle,
listChildContainerStyle,
listParentContainerStyle,
listChildLabelStyle,
listParentLabelStyle,
customItemContainerStyle,
customItemLabelStyle,
selectedItemContainerStyle,
selectedItemLabelStyle,
disabledItemContainerStyle,
disabledItemLabelStyle,
containerStyle,
labelStyle,
categorySelectable,
onPress,
setPosition,
THEME
}) {
/**
* The tick icon component.
* @returns {JSX|null}
*/
const _TickIconComponent = useMemo(() => isSelected && (
<TickIconComponent />
), [isSelected, TickIconComponent]);
/**
* The list category container style.
* @returns {object}
*/
const _listParentChildContainerStyle = useMemo(() => (parent !== null ? [
THEME.listChildContainer,
...[listChildContainerStyle].flat()
] : [
THEME.listParentContainer,
...[listParentContainerStyle].flat()
]), [THEME, rtl, listChildContainerStyle, listParentContainerStyle, parent])
/**
* The selected item container style.
* @returns {object}
*/
const _selectedItemContainerStyle = useMemo(() => isSelected && selectedItemContainerStyle, [isSelected, selectedItemContainerStyle]);
/**
* The disabled item container style.
* @returns {object}
*/
const _disabledItemContainerStyle = useMemo(() => disabled && disabledItemContainerStyle, [disabled, disabledItemContainerStyle]);
/**
* The custom container item style.
* @returns {JSX}
*/
const _customItemContainerStyle = useMemo(() => custom && ([
THEME.customItemContainer,
...[customItemContainerStyle].flat()
]), [THEME, custom, customItemContainerStyle]);
/**
* The list item container style.
* @returns {object}
*/
const _listItemContainerStyle = useMemo(() => ([
...[listItemContainerStyle].flat(),
...[_listParentChildContainerStyle].flat(),
...[containerStyle].flat(),
...[_selectedItemContainerStyle].flat(),
...[_customItemContainerStyle].flat(),
...[_disabledItemContainerStyle].flat(),
]), [listItemContainerStyle, _listParentChildContainerStyle, _selectedItemContainerStyle, _customItemContainerStyle, _disabledItemContainerStyle, containerStyle]);
/**
* The list category label style.
* @returns {object}
*/
const _listParentChildLabelStyle = useMemo(() => (parent !== null ? [
THEME.listChildLabel,
...[listChildLabelStyle].flat(),
] : [
THEME.listParentLabel,
...[listParentLabelStyle].flat(),
]), [THEME, listChildLabelStyle, listParentLabelStyle, parent]);
/**
* The selected item label style.
* @returns {object}
*/
const _selectedItemLabelStyle = useMemo(() => isSelected && selectedItemLabelStyle, [isSelected, selectedItemLabelStyle]);
/**
* The disabled item label style.
* @returns {object}
*/
const _disabledItemLabelStyle = useMemo(() => disabled && disabledItemLabelStyle, [disabled, disabledItemLabelStyle]);
/**
* The custom label item style.
* @returns {JSX}
*/
const _customItemLabelStyle = useMemo(() => custom && ([
THEME.customItemLabel,
...[customItemLabelStyle].flat()
]), [THEME, custom, customItemLabelStyle]);
/**
* The list item label style.
* @returns {object}
*/
const _listItemLabelStyle = useMemo(() => ([
...[listItemLabelStyle].flat(),
...[_listParentChildLabelStyle].flat(),
...[labelStyle].flat(),
...[_selectedItemLabelStyle].flat(),
...[_customItemLabelStyle].flat(),
...[_disabledItemLabelStyle].flat(),
]), [listItemLabelStyle, _listParentChildLabelStyle, _selectedItemLabelStyle, _customItemLabelStyle, _disabledItemLabelStyle, labelStyle]);
/**
* onPress.
*/
const __onPress = useCallback(() => {
if (parent === null && ! categorySelectable && selectable !== true) {
return;
}
onPress(item, custom);
}, [onPress, parent, categorySelectable, custom]);
/**
* onLayout.
*/
const onLayout = useCallback(({nativeEvent: {layout: {y}}}) => {
setPosition(value, y);
}, [value]);
return (
<TouchableOpacity style={_listItemContainerStyle} onPress={__onPress} onLayout={onLayout} {...props} disabled={selectable === false || disabled} testID={item.testID}>
{IconComponent}
<Text style={_listItemLabelStyle} {...labelProps}>
{label}
</Text>
{_TickIconComponent}
</TouchableOpacity>
);
}
const areEqual = (nextProps, prevProps) => {
if (nextProps.label !== prevProps.label)
return false;
if (nextProps.value !== prevProps.value)
return false;
if (nextProps.parent !== prevProps.parent)
return false;
if (nextProps.selectable !== prevProps.selectable)
return false;
if (nextProps.disabled !== prevProps.disabled)
return false;
if (nextProps.custom !== prevProps.custom)
return false;
if (nextProps.isSelected !== prevProps.isSelected)
return false;
if (nextProps.categorySelectable !== prevProps.categorySelectable)
return false;
if (nextProps.rtl !== prevProps.rtl)
return false;
if (nextProps.theme !== prevProps.theme)
return false;
return true;
}
export default memo(RenderListItem, areEqual);