Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export type Props = $Omit<React.ComponentProps<typeof Surface>, 'mode'> & {
* Icon to display for the `Button`.
*/
icon?: IconSource;
/**
* Trailing icon to display for the `Button`.
*/
trailingIcon?: IconSource;
/**
* Whether the button is disabled. A disabled button is greyed out and `onPress` is not called on touch.
*/
Expand Down Expand Up @@ -170,6 +174,7 @@ const Button = (
dark,
loading,
icon,
trailingIcon,
buttonColor: customButtonColor,
textColor: customTextColor,
rippleColor: customRippleColor,
Expand Down Expand Up @@ -314,6 +319,23 @@ const Button = (
styles[`md3IconTextMode${compact ? 'Compact' : ''}`],
];

const trailingIconStyle =
StyleSheet.flatten(contentStyle)?.flexDirection === 'row-reverse'
? [
styles.icon,
isV3 && styles[`md3Icon${compact ? 'Compact' : ''}`],
isV3 &&
isMode('text') &&
styles[`md3IconTextMode${compact ? 'Compact' : ''}`],
]
: [
styles.iconReverse,
isV3 && styles[`md3IconReverse${compact ? 'Compact' : ''}`],
isV3 &&
isMode('text') &&
styles[`md3IconReverseTextMode${compact ? 'Compact' : ''}`],
];

return (
<Surface
{...rest}
Expand Down Expand Up @@ -397,6 +419,22 @@ const Button = (
>
{children}
</Text>
{trailingIcon ? (
<View
style={trailingIconStyle}
testID={`${testID}-trailing-icon-container`}
>
<Icon
source={trailingIconStyle}
size={customLabelSize ?? iconSize}
color={
typeof customLabelColor === 'string'
? customLabelColor
: textColor
}
/>
</View>
) : null}
</View>
</TouchableRipple>
</Surface>
Expand Down
8 changes: 8 additions & 0 deletions src/components/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ it('renders button with icon', () => {
expect(tree).toMatchSnapshot();
});

it('renders button with trailing icon', () => {
const tree = render(
<Button trailingIcon="camera">Icon Button</Button>
).toJSON();

expect(tree).toMatchSnapshot();
});

it('renders button with icon in reverse order', () => {
const tree = render(
<Button icon="chevron-right" contentStyle={styles.flexing}>
Expand Down
169 changes: 169 additions & 0 deletions src/components/__tests__/__snapshots__/Button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,175 @@ exports[`renders button with icon in reverse order 1`] = `
</View>
`;

exports[`renders button with trailing icon 1`] = `
<View
collapsable={false}
style={
{
"backgroundColor": "transparent",
"borderRadius": 20,
"shadowColor": "#000",
"shadowOffset": {
"height": 0,
"width": 0,
},
"shadowOpacity": 0,
"shadowRadius": 0,
}
}
testID="button-container-outer-layer"
>
<View
collapsable={false}
style={
{
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 20,
"borderStyle": "solid",
"borderWidth": 0,
"flex": undefined,
"minWidth": 64,
"shadowColor": "#000",
"shadowOffset": {
"height": 0,
"width": 0,
},
"shadowOpacity": 0,
"shadowRadius": 0,
}
}
testID="button-container"
>
<View
accessibilityRole="button"
accessibilityState={
{
"busy": undefined,
"checked": undefined,
"disabled": true,
"expanded": undefined,
"selected": undefined,
}
}
accessibilityValue={
{
"max": undefined,
"min": undefined,
"now": undefined,
"text": undefined,
}
}
accessible={true}
collapsable={false}
focusable={true}
onBlur={[Function]}
onClick={[Function]}
onFocus={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
[
{
"overflow": "hidden",
},
{
"borderRadius": 20,
},
]
}
testID="button"
>
<View
style={
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
undefined,
]
}
>
<Text
numberOfLines={1}
selectable={false}
style={
[
{
"textAlign": "left",
},
{
"color": "rgba(28, 27, 31, 1)",
"writingDirection": "ltr",
},
[
{
"fontFamily": "System",
"fontSize": 14,
"fontWeight": "500",
"letterSpacing": 0.1,
"lineHeight": 20,
},
[
{
"marginHorizontal": 16,
"marginVertical": 9,
"textAlign": "center",
},
false,
{
"marginHorizontal": 12,
},
undefined,
false,
{
"color": "rgba(103, 80, 164, 1)",
"fontFamily": "System",
"fontSize": 14,
"fontWeight": "500",
"letterSpacing": 0.1,
"lineHeight": 20,
},
undefined,
],
],
]
}
testID="button-text"
>
Icon Button
</Text>
<View
style={
[
{
"marginLeft": -4,
"marginRight": 12,
},
{
"marginLeft": -16,
"marginRight": 16,
},
{
"marginLeft": -8,
"marginRight": 12,
},
]
}
testID="button-trailing-icon-container"
/>
</View>
</View>
</View>
</View>
`;

exports[`renders contained contained with mode 1`] = `
<View
collapsable={false}
Expand Down