Skip to content

Commit 64c1675

Browse files
authored
fix: correct border radius in icon button and card components (#3565)
1 parent fe16b9f commit 64c1675

File tree

7 files changed

+73
-11
lines changed

7 files changed

+73
-11
lines changed

example/src/Examples/IconButtonExample.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ const ButtonExample = () => {
123123
iconColor={MD3Colors.tertiary50}
124124
onPress={() => {}}
125125
/>
126+
<IconButton
127+
icon="eye"
128+
mode="contained"
129+
style={styles.square}
130+
size={24}
131+
iconColor={MD3Colors.tertiary50}
132+
onPress={() => {}}
133+
/>
126134
<IconButton icon="camera" size={36} onPress={() => {}} />
127135
<IconButton
128136
icon="lock"
@@ -151,6 +159,9 @@ const styles = StyleSheet.create({
151159
flexDirection: 'row',
152160
paddingHorizontal: 12,
153161
},
162+
square: {
163+
borderRadius: 0,
164+
},
154165
});
155166

156167
export default ButtonExample;

src/components/Card/Card.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const Card = ({
134134
children,
135135
style,
136136
theme,
137-
testID,
137+
testID = 'card',
138138
accessible,
139139
...rest
140140
}: (OutlinedCardProps | ElevatedCardProps | ContainedCardProps) & Props) => {
@@ -224,7 +224,9 @@ const Card = ({
224224
mode: cardMode,
225225
});
226226

227-
const borderRadius = (isV3 ? 3 : 1) * roundness;
227+
const { borderRadius = (isV3 ? 3 : 1) * roundness } = (StyleSheet.flatten(
228+
style
229+
) || {}) as ViewStyle;
228230

229231
return (
230232
<Surface
@@ -249,6 +251,7 @@ const Card = ({
249251
{isMode('outlined') && (
250252
<View
251253
pointerEvents="none"
254+
testID={`${testID}-outline`}
252255
style={[
253256
{
254257
borderRadius,

src/components/IconButton/IconButton.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,14 @@ const IconButton = React.forwardRef<View, Props>(
147147

148148
const buttonSize = isV3 ? size + 2 * PADDING : size * 1.5;
149149

150+
const {
151+
borderWidth = isV3 && mode === 'outlined' && !selected ? 1 : 0,
152+
borderRadius = buttonSize / 2,
153+
} = (StyleSheet.flatten(style) || {}) as ViewStyle;
154+
150155
const borderStyles = {
151-
borderWidth: isV3 && mode === 'outlined' && !selected ? 1 : 0,
152-
borderRadius: buttonSize / 2,
156+
borderWidth,
157+
borderRadius,
153158
borderColor,
154159
};
155160

@@ -177,10 +182,7 @@ const IconButton = React.forwardRef<View, Props>(
177182
onPress={onPress}
178183
rippleColor={rippleColor}
179184
accessibilityLabel={accessibilityLabel}
180-
style={[
181-
styles.touchable,
182-
{ borderRadius: borderStyles.borderRadius },
183-
]}
185+
style={[styles.touchable, { borderRadius }]}
184186
// @ts-expect-error We keep old a11y props for backwards compat with old RN versions
185187
accessibilityTraits={disabled ? ['button', 'disabled'] : 'button'}
186188
accessibilityComponentType="button"

src/components/__tests__/Card/Card.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { StyleSheet } from 'react-native';
23

34
import { render } from '@testing-library/react-native';
45
import color from 'color';
@@ -10,13 +11,34 @@ import Button from '../../Button/Button';
1011
import Card from '../../Card/Card';
1112
import { getCardColors, getCardCoverStyle } from '../../Card/utils';
1213

14+
const styles = StyleSheet.create({
15+
customBorderRadius: {
16+
borderRadius: 32,
17+
},
18+
});
19+
1320
describe('Card', () => {
1421
it('renders an outlined card', () => {
1522
const tree = renderer.create(<Card mode="outlined" />).toJSON();
1623

1724
expect(tree).toMatchSnapshot();
1825
});
1926

27+
it('renders an outlined card with custom border radius and color', () => {
28+
const { getByTestId } = render(
29+
<Card
30+
mode="outlined"
31+
theme={{ colors: { outline: 'purple' } }}
32+
style={styles.customBorderRadius}
33+
/>
34+
);
35+
36+
expect(getByTestId('card-outline')).toHaveStyle({
37+
borderRadius: 32,
38+
borderColor: 'purple',
39+
});
40+
});
41+
2042
it('renders with a custom theme', () => {
2143
const { getByLabelText } = render(
2244
<Card

src/components/__tests__/Card/__snapshots__/Card.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ exports[`Card renders an outlined card 1`] = `
6262
},
6363
]
6464
}
65+
testID="card-outline"
6566
/>
6667
<View
6768
accessibilityState={
@@ -83,6 +84,7 @@ exports[`Card renders an outlined card 1`] = `
8384
"flexShrink": 1,
8485
}
8586
}
87+
testID="card"
8688
/>
8789
</View>
8890
</View>

src/components/__tests__/IconButton.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from 'react';
2+
import { StyleSheet } from 'react-native';
23

4+
import { render } from '@testing-library/react-native';
35
import color from 'color';
46
import renderer from 'react-test-renderer';
57

@@ -8,6 +10,12 @@ import { pink500 } from '../../styles/themes/v2/colors';
810
import IconButton from '../IconButton/IconButton.tsx';
911
import { getIconButtonColor } from '../IconButton/utils';
1012

13+
const styles = StyleSheet.create({
14+
square: {
15+
borderRadius: 0,
16+
},
17+
});
18+
1119
it('renders icon button by default', () => {
1220
const tree = renderer.create(<IconButton icon="camera" />).toJSON();
1321

@@ -40,6 +48,20 @@ it('renders icon change animated', () => {
4048
expect(tree).toMatchSnapshot();
4149
});
4250

51+
it('renders icon button with custom border radius', () => {
52+
const { getByTestId } = render(
53+
<IconButton
54+
icon="camera"
55+
testID="icon-button"
56+
size={36}
57+
onPress={() => {}}
58+
style={styles.square}
59+
/>
60+
);
61+
62+
expect(getByTestId('icon-button')).toHaveStyle({ borderRadius: 0 });
63+
});
64+
4365
describe('getIconButtonColor - icon color', () => {
4466
it('should return custom icon color', () => {
4567
expect(

src/components/__tests__/__snapshots__/ToggleButton.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ exports[`renders disabled toggle button 1`] = `
9999
"justifyContent": "center",
100100
},
101101
Object {
102-
"borderRadius": 20,
102+
"borderRadius": 4,
103103
},
104104
],
105105
]
@@ -240,7 +240,7 @@ exports[`renders toggle button 1`] = `
240240
"justifyContent": "center",
241241
},
242242
Object {
243-
"borderRadius": 20,
243+
"borderRadius": 4,
244244
},
245245
],
246246
]
@@ -387,7 +387,7 @@ exports[`renders unchecked toggle button 1`] = `
387387
"justifyContent": "center",
388388
},
389389
Object {
390-
"borderRadius": 20,
390+
"borderRadius": 4,
391391
},
392392
],
393393
]

0 commit comments

Comments
 (0)