Skip to content

Commit e155220

Browse files
committed
feat: add typed leading and trailing slots to List.Item
1 parent c8c24e0 commit e155220

5 files changed

Lines changed: 124 additions & 28 deletions

File tree

‎example/src/Examples/ListItemExample.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const SelectableSection = () => {
1616
description="Supporting text"
1717
selected={index === selected}
1818
onPress={() => setSelected(index)}
19-
left={(props) => <List.Icon {...props} icon="account-outline" />}
19+
leading={<List.Icon icon="account-outline" />}
2020
/>
2121
))}
2222
<Divider />

‎src/components/List/ListIcon.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import * as React from 'react';
12
import { StyleSheet, View } from 'react-native';
23
import type { ColorValue, StyleProp, ViewStyle } from 'react-native';
34

5+
import { ListItemContext } from './ListItemContext';
46
import { useInternalTheme } from '../../core/theming';
57
import type { ThemeProp } from '../../types';
68
import Icon from '../Icon';
@@ -50,10 +52,12 @@ const ListIcon = ({
5052
theme: themeOverrides,
5153
}: Props) => {
5254
const theme = useInternalTheme(themeOverrides);
55+
const listItem = React.useContext(ListItemContext);
56+
const color = iconColor ?? listItem?.color;
5357

5458
return (
5559
<View style={[styles.item, style]} pointerEvents="box-none">
56-
<Icon source={icon} size={ICON_SIZE} color={iconColor} theme={theme} />
60+
<Icon source={icon} size={ICON_SIZE} color={color} theme={theme} />
5761
</View>
5862
);
5963
};

‎src/components/List/ListItem.tsx‎

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
ViewStyle,
1111
} from 'react-native';
1212

13+
import { ListItemContext } from './ListItemContext';
1314
import { ListTokens } from './tokens';
1415
import { ListRowContext, getLeftStyles, getRightStyles } from './utils';
1516
import type { Style } from './utils';
@@ -45,6 +46,14 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
4546
* Description text for the list item or callback which returns a React element to display the description.
4647
*/
4748
description?: Description;
49+
/**
50+
* Element to display in the leading slot. Takes precedence over `left`.
51+
*/
52+
leading?: React.ReactNode;
53+
/**
54+
* Element to display in the trailing slot. Takes precedence over `right`.
55+
*/
56+
trailing?: React.ReactNode;
4857
/**
4958
* Callback which returns a React element to display on the left side.
5059
*/
@@ -146,6 +155,8 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
146155
const ListItem = ({
147156
left,
148157
right,
158+
leading,
159+
trailing,
149160
title,
150161
description,
151162
selected,
@@ -247,6 +258,45 @@ const ListItem = ({
247258
[isDescriptionMultiline]
248259
);
249260

261+
const accessoryContext = React.useMemo(
262+
() => ({ color: descriptionColor }),
263+
[descriptionColor]
264+
);
265+
266+
const renderLeading = () => {
267+
const accessoryStyle = getLeftStyles(isDescriptionMultiline, description);
268+
269+
if (leading) {
270+
return <View style={accessoryStyle}>{leading}</View>;
271+
}
272+
273+
return left
274+
? left({
275+
color: selected
276+
? theme.colors[ListTokens.selectedContentColor]
277+
: theme.colors[ListTokens.leadingIconColor],
278+
style: accessoryStyle,
279+
})
280+
: null;
281+
};
282+
283+
const renderTrailing = () => {
284+
const accessoryStyle = getRightStyles(isDescriptionMultiline, description);
285+
286+
if (trailing) {
287+
return <View style={accessoryStyle}>{trailing}</View>;
288+
}
289+
290+
return right
291+
? right({
292+
color: selected
293+
? theme.colors[ListTokens.selectedContentColor]
294+
: theme.colors[ListTokens.trailingIconColor],
295+
style: accessoryStyle,
296+
})
297+
: null;
298+
};
299+
250300
return (
251301
<ListRowContext.Provider value={rowContext}>
252302
<TouchableRipple
@@ -264,34 +314,22 @@ const ListItem = ({
264314
theme={theme}
265315
testID={testID}
266316
>
267-
<View style={[styles.row, containerStyle]}>
268-
{left
269-
? left({
270-
color: selected
271-
? theme.colors[ListTokens.selectedContentColor]
272-
: theme.colors[ListTokens.leadingIconColor],
273-
style: getLeftStyles(isDescriptionMultiline, description),
274-
})
275-
: null}
276-
<View
277-
style={[styles.item, styles.content, contentStyle]}
278-
testID={`${testID}-content`}
279-
>
280-
{renderTitle()}
317+
<ListItemContext.Provider value={accessoryContext}>
318+
<View style={[styles.row, containerStyle]}>
319+
{renderLeading()}
320+
<View
321+
style={[styles.item, styles.content, contentStyle]}
322+
testID={`${testID}-content`}
323+
>
324+
{renderTitle()}
281325

282-
{description
283-
? renderDescription(descriptionColor, description)
284-
: null}
326+
{description
327+
? renderDescription(descriptionColor, description)
328+
: null}
329+
</View>
330+
{renderTrailing()}
285331
</View>
286-
{right
287-
? right({
288-
color: selected
289-
? theme.colors[ListTokens.selectedContentColor]
290-
: theme.colors[ListTokens.trailingIconColor],
291-
style: getRightStyles(isDescriptionMultiline, description),
292-
})
293-
: null}
294-
</View>
332+
</ListItemContext.Provider>
295333
</TouchableRipple>
296334
</ListRowContext.Provider>
297335
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as React from 'react';
2+
import type { ColorValue } from 'react-native';
3+
4+
export type ListItemContextType = {
5+
color: ColorValue;
6+
};
7+
8+
export const ListItemContext = React.createContext<ListItemContextType | null>(
9+
null
10+
);

‎src/components/__tests__/ListItem.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,50 @@ it('keeps a 64dp leading video on the same height once the description wraps', a
308308
});
309309
});
310310

311+
it('colors a leading List.Icon from the list item context', async () => {
312+
await render(
313+
<ListItem
314+
title="First Item"
315+
leading={<ListIcon icon="folder" />}
316+
testID={testID}
317+
/>
318+
);
319+
320+
expect(
321+
screen.getByText('folder', { includeHiddenElements: true })
322+
).toHaveStyle({ color: getTheme().colors.onSurfaceVariant });
323+
});
324+
325+
it('renders the trailing slot', async () => {
326+
await render(
327+
<ListItem
328+
title="First Item"
329+
trailing={<Text>Trailing</Text>}
330+
testID={testID}
331+
/>
332+
);
333+
334+
expect(screen.getByText('Trailing')).toBeOnTheScreen();
335+
});
336+
337+
it('prefers the typed slots over the left and right render props', async () => {
338+
await render(
339+
<ListItem
340+
title="First Item"
341+
leading={<Text>Leading slot</Text>}
342+
trailing={<Text>Trailing slot</Text>}
343+
left={() => <Text>Left render prop</Text>}
344+
right={() => <Text>Right render prop</Text>}
345+
testID={testID}
346+
/>
347+
);
348+
349+
expect(screen.getByText('Leading slot')).toBeOnTheScreen();
350+
expect(screen.getByText('Trailing slot')).toBeOnTheScreen();
351+
expect(screen.queryByText('Left render prop')).not.toBeOnTheScreen();
352+
expect(screen.queryByText('Right render prop')).not.toBeOnTheScreen();
353+
});
354+
311355
it('top aligns the accessories once the description wraps', async () => {
312356
await render(
313357
<ListItem

0 commit comments

Comments
 (0)