Skip to content

Commit

Permalink
feat: add contentStyle to the list item title and description contain…
Browse files Browse the repository at this point in the history
…er (#4205)
  • Loading branch information
lukewalczak authored Jan 8, 2024
1 parent bdc2cbc commit c42cfa2
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/components/List/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
* 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.
*/
Expand Down Expand Up @@ -104,6 +108,10 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
* Specifies the largest possible scale a description font can reach.
*/
descriptionMaxFontSizeMultiplier?: number;
/**
* TestID used for testing purposes
*/
testID?: string;
};

/**
Expand Down Expand Up @@ -136,6 +144,7 @@ const ListItem = (
onPress,
theme: themeOverrides,
style,
contentStyle,
titleStyle,
titleNumberOfLines = 1,
descriptionNumberOfLines = 2,
Expand All @@ -144,6 +153,7 @@ const ListItem = (
descriptionStyle,
descriptionMaxFontSizeMultiplier,
titleMaxFontSizeMultiplier,
testID,
...rest
}: Props,
ref: React.ForwardedRef<View>
Expand Down Expand Up @@ -226,6 +236,7 @@ const ListItem = (
style={[theme.isV3 ? styles.containerV3 : styles.container, style]}
onPress={onPress}
theme={theme}
testID={testID}
>
<View style={theme.isV3 ? styles.rowV3 : styles.row}>
{left
Expand All @@ -235,7 +246,12 @@ const ListItem = (
})
: null}
<View
style={[theme.isV3 ? styles.itemV3 : styles.item, styles.content]}
style={[
theme.isV3 ? styles.itemV3 : styles.item,
styles.content,
contentStyle,
]}
testID={`${testID}-content`}
>
{renderTitle()}

Expand Down
36 changes: 34 additions & 2 deletions src/components/__tests__/ListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ const styles = StyleSheet.create({
description: {
color: red500,
},
content: {
paddingLeft: 0,
},
});

const testID = 'list-item';

it('renders list item with title and description', () => {
const tree = render(
<ListItem title="First Item" description="Description for first item" />
<ListItem
title="First Item"
testID={testID}
description="Description for first item"
/>
).toJSON();

expect(tree).toMatchSnapshot();
Expand All @@ -31,6 +40,7 @@ it('renders list item with left item', () => {
const tree = render(
<ListItem
title="First Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
).toJSON();
Expand All @@ -40,7 +50,11 @@ it('renders list item with left item', () => {

it('renders list item with right item', () => {
const tree = render(
<ListItem title="First Item" right={() => <Text>GG</Text>} />
<ListItem
title="First Item"
testID={testID}
right={() => <Text>GG</Text>}
/>
).toJSON();

expect(tree).toMatchSnapshot();
Expand All @@ -51,6 +65,7 @@ it('renders list item with left and right items', () => {
<ListItem
title="First Item"
description="Item description"
testID={testID}
left={() => <Text>GG</Text>}
right={(props) => <ListIcon {...props} icon="folder" />}
/>
Expand All @@ -64,6 +79,7 @@ it('renders list item with custom title and description styles', () => {
<ListItem
title="First Item"
description="Item description"
testID={testID}
titleStyle={styles.title}
descriptionStyle={styles.description}
/>
Expand Down Expand Up @@ -93,6 +109,7 @@ it('renders list item with custom description', () => {
</View>
</View>
)}
testID={testID}
/>
).toJSON();

Expand All @@ -106,6 +123,7 @@ it('renders with a description with typeof number', () => {
description={123}
titleStyle={styles.title}
descriptionStyle={styles.description}
testID={testID}
/>
).toJSON();

Expand All @@ -120,10 +138,24 @@ it('calling onPress on ListItem right component', () => {
<ListItem
title="First Item"
description="Item description"
testID={testID}
right={() => <IconButton icon="pencil" onPress={onPress} />}
/>
);

fireEvent(getByTestId('icon-button'), 'onPress');
expect(onPress).toHaveBeenCalledTimes(1);
});

it('renders list item with custom content style', () => {
const { getByTestId } = render(
<ListItem
title="First Item"
description="Item description"
contentStyle={styles.content}
testID={testID}
/>
);

expect(getByTestId('list-item-content')).toHaveStyle(styles.content);
});
8 changes: 8 additions & 0 deletions src/components/__tests__/ListSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ const styles = StyleSheet.create({
},
});

const testID = 'list-item';

it('renders list section without subheader', () => {
const tree = render(
<ListSection>
<ListItem
title="First Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
<ListItem
title="Second Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
</ListSection>
Expand All @@ -38,10 +42,12 @@ it('renders list section with subheader', () => {
<ListSubheader>Some title</ListSubheader>
<ListItem
title="First Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
<ListItem
title="Second Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
</ListSection>
Expand All @@ -55,10 +61,12 @@ it('renders list section with custom title style', () => {
<ListSection title="Some title" titleStyle={styles.itemColor}>
<ListItem
title="First Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
<ListItem
title="Second Item"
testID={testID}
left={(props) => <ListIcon {...props} icon="folder" />}
/>
</ListSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ exports[`renders expanded accordion 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="undefined-content"
>
<Text
numberOfLines={1}
Expand Down
21 changes: 21 additions & 0 deletions src/components/__tests__/__snapshots__/ListItem.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exports[`renders list item with custom description 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -64,8 +65,10 @@ exports[`renders list item with custom description 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -367,6 +370,7 @@ exports[`renders list item with custom title and description styles 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -388,8 +392,10 @@ exports[`renders list item with custom title and description styles 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -506,6 +512,7 @@ exports[`renders list item with left and right items 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -530,8 +537,10 @@ exports[`renders list item with left and right items 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -697,6 +706,7 @@ exports[`renders list item with left item 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand Down Expand Up @@ -772,8 +782,10 @@ exports[`renders list item with left item 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -854,6 +866,7 @@ exports[`renders list item with right item 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -875,8 +888,10 @@ exports[`renders list item with right item 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -960,6 +975,7 @@ exports[`renders list item with title and description 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -981,8 +997,10 @@ exports[`renders list item with title and description 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down Expand Up @@ -1095,6 +1113,7 @@ exports[`renders with a description with typeof number 1`] = `
],
]
}
testID="list-item"
>
<View
style={
Expand All @@ -1116,8 +1135,10 @@ exports[`renders with a description with typeof number 1`] = `
"flexShrink": 1,
"justifyContent": "center",
},
undefined,
]
}
testID="list-item-content"
>
<Text
numberOfLines={1}
Expand Down
Loading

0 comments on commit c42cfa2

Please sign in to comment.