-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathListItem.tsx
More file actions
313 lines (297 loc) · 7.57 KB
/
Copy pathListItem.tsx
File metadata and controls
313 lines (297 loc) · 7.57 KB
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
import * as React from 'react';
import {
GestureResponderEvent,
NativeSyntheticEvent,
StyleProp,
StyleSheet,
TextLayoutEventData,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import color from 'color';
import { Style, getLeftStyles, getRightStyles } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { $RemoveChildren, EllipsizeProp, ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
type Title =
| React.ReactNode
| ((props: {
selectable: boolean;
ellipsizeMode: EllipsizeProp | undefined;
color: string;
fontSize: number;
}) => React.ReactNode);
type Description =
| React.ReactNode
| ((props: {
selectable: boolean;
ellipsizeMode: EllipsizeProp | undefined;
color: string;
fontSize: number;
}) => React.ReactNode);
export type Props = $RemoveChildren<typeof TouchableRipple> & {
/**
* Title text for the list item.
*/
title: Title;
/**
* Description text for the list item or callback which returns a React element to display the description.
*/
description?: Description;
/**
* Callback which returns a React element to display on the left side.
*/
left?: (props: { color: string; style: Style }) => React.ReactNode;
/**
* Callback which returns a React element to display on the right side.
*/
right?: (props: { color: string; style?: Style }) => React.ReactNode;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* @optional
*/
theme?: ThemeProp;
/**
* Style that is passed to the wrapping TouchableRipple element.
*/
style?: StyleProp<ViewStyle>;
/**
* Style that is passed to the container wrapping title and descripton.
*/
contentStyle?: StyleProp<ViewStyle>;
/**
* Style that is passed to Title element.
*/
titleStyle?: StyleProp<TextStyle>;
/**
* Style that is passed to Description element.
*/
descriptionStyle?: StyleProp<TextStyle>;
/**
* Truncate Title text such that the total number of lines does not
* exceed this number.
*/
titleNumberOfLines?: number;
/**
* Truncate Description text such that the total number of lines does not
* exceed this number.
*/
descriptionNumberOfLines?: number;
/**
* Ellipsize Mode for the Title. One of `'head'`, `'middle'`, `'tail'`, `'clip'`.
*
* See [`ellipsizeMode`](https://reactnative.dev/docs/text#ellipsizemode)
*/
titleEllipsizeMode?: EllipsizeProp;
/**
* Ellipsize Mode for the Description. One of `'head'`, `'middle'`, `'tail'`, `'clip'`.
*
* See [`ellipsizeMode`](https://reactnative.dev/docs/text#ellipsizemode)
*/
descriptionEllipsizeMode?: EllipsizeProp;
/**
* Specifies the largest possible scale a title font can reach.
*/
titleMaxFontSizeMultiplier?: number;
/**
* Specifies the largest possible scale a description font can reach.
*/
descriptionMaxFontSizeMultiplier?: number;
/**
* TestID used for testing purposes
*/
testID?: string;
};
/**
* A component to show tiles inside a List.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { List } from 'react-native-paper';
*
* const MyComponent = () => (
* <List.Item
* title="First Item"
* description="Item description"
* left={props => <List.Icon {...props} icon="folder" />}
* />
* );
*
* export default MyComponent;
* ```
*
* @extends TouchableRipple props https://callstack.github.io/react-native-paper/docs/components/TouchableRipple
*/
const ListItem = (
{
left,
right,
title,
description,
onPress,
theme: themeOverrides,
style,
contentStyle,
titleStyle,
titleNumberOfLines = 1,
descriptionNumberOfLines = 2,
titleEllipsizeMode,
descriptionEllipsizeMode,
descriptionStyle,
descriptionMaxFontSizeMultiplier,
titleMaxFontSizeMultiplier,
testID,
...rest
}: Props,
ref: React.ForwardedRef<View>
) => {
const theme = useInternalTheme(themeOverrides);
const [alignToTop, setAlignToTop] = React.useState(false);
const onDescriptionTextLayout = (
event: NativeSyntheticEvent<TextLayoutEventData>
) => {
if (!theme.isV3) {
return;
}
const { nativeEvent } = event;
setAlignToTop(nativeEvent.lines.length >= 2);
};
const renderDescription = (
descriptionColor: string,
description?: Description | null
) => {
return typeof description === 'function' ? (
description({
selectable: false,
ellipsizeMode: descriptionEllipsizeMode,
color: descriptionColor,
fontSize: styles.description.fontSize,
})
) : (
<Text
selectable={false}
numberOfLines={descriptionNumberOfLines}
ellipsizeMode={descriptionEllipsizeMode}
style={[
styles.description,
{ color: descriptionColor },
descriptionStyle,
]}
onTextLayout={onDescriptionTextLayout}
maxFontSizeMultiplier={descriptionMaxFontSizeMultiplier}
>
{description}
</Text>
);
};
const renderTitle = () => {
const titleColor = theme.isV3
? theme.colors.onSurface
: color(theme.colors.text).alpha(0.87).rgb().string();
return typeof title === 'function' ? (
title({
selectable: false,
ellipsizeMode: titleEllipsizeMode,
color: titleColor,
fontSize: styles.title.fontSize,
})
) : (
<Text
selectable={false}
ellipsizeMode={titleEllipsizeMode}
numberOfLines={titleNumberOfLines}
style={[styles.title, { color: titleColor }, titleStyle]}
maxFontSizeMultiplier={titleMaxFontSizeMultiplier}
>
{title}
</Text>
);
};
const descriptionColor = theme.isV3
? theme.colors.onSurfaceVariant
: color(theme.colors.text).alpha(0.54).rgb().string();
return (
<TouchableRipple
{...rest}
ref={ref}
style={[theme.isV3 ? styles.containerV3 : styles.container, style]}
onPress={onPress}
theme={theme}
testID={testID}
>
<View style={theme.isV3 ? styles.rowV3 : styles.row}>
{left
? left({
color: descriptionColor,
style: getLeftStyles(alignToTop, description, theme.isV3),
})
: null}
<View
style={[
theme.isV3 ? styles.itemV3 : styles.item,
styles.content,
contentStyle,
]}
testID={`${testID}-content`}
>
{renderTitle()}
{description
? renderDescription(descriptionColor, description)
: null}
</View>
{right
? right({
color: descriptionColor,
style: getRightStyles(alignToTop, description, theme.isV3),
})
: null}
</View>
</TouchableRipple>
);
};
ListItem.displayName = 'List.Item';
const Component = forwardRef(ListItem);
const styles = StyleSheet.create({
container: {
padding: 8,
},
containerV3: {
paddingVertical: 8,
paddingRight: 24,
},
row: {
width: '100%',
flexDirection: 'row',
},
rowV3: {
width: '100%',
flexDirection: 'row',
marginVertical: 6,
},
title: {
fontSize: 16,
},
description: {
fontSize: 14,
},
item: {
marginVertical: 6,
paddingLeft: 8,
},
itemV3: {
paddingLeft: 16,
},
content: {
flexShrink: 1,
flexGrow: 1,
justifyContent: 'center',
},
});
export default Component;