Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add iconPosition to FAB #4436

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions example/src/Examples/FABExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ScreenWrapper from '../ScreenWrapper';
type FABVariant = 'primary' | 'secondary' | 'tertiary' | 'surface';
type FABSize = 'small' | 'medium' | 'large';
type FABMode = 'flat' | 'elevated';
type FABIconPosition = 'left' | 'right';

const FABExample = () => {
const [visible, setVisible] = React.useState<boolean>(true);
Expand All @@ -21,6 +22,7 @@ const FABExample = () => {
const variants = ['primary', 'secondary', 'tertiary', 'surface'];
const sizes = ['small', 'medium', 'large'];
const modes = ['flat', 'elevated'];
const iconPositions = ['left', 'right'];

return (
<ScreenWrapper style={styles.container}>
Expand Down Expand Up @@ -76,6 +78,23 @@ const FABExample = () => {
</View>
))}
</View>
<View style={styles.row}>
{iconPositions.map((position) => (
<View style={styles.fabVariant} key={position}>
<FAB
icon="pencil"
label={`icon on the ${position}`}
style={styles.fab}
onPress={() => {}}
visible={visible}
iconPosition={position as FABIconPosition}
/>
{visible && (
<Text variant="bodyMedium">icon on the {position}</Text>
)}
</View>
))}
</View>
</>
)}
<View style={styles.column}>
Expand Down
18 changes: 16 additions & 2 deletions src/components/FAB/FAB.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ export type Props = $Omit<$RemoveChildren<typeof Surface>, 'mode'> & {
* TestID used for testing purposes
*/
testID?: string;
/**
* Specifies the position of the icon in relation to the label.
*/
iconPosition?: 'left' | 'right';
ref?: React.RefObject<View>;
} & IconOrLabel;

Expand Down Expand Up @@ -205,6 +209,7 @@ const FAB = forwardRef<View, Props>(
mode = 'elevated',
variant = 'primary',
labelMaxFontSizeMultiplier,
iconPosition = 'left',
...rest
}: Props,
ref
Expand Down Expand Up @@ -306,7 +311,11 @@ const FAB = forwardRef<View, Props>(
{...rest}
>
<View
style={[styles.content, label ? extendedStyle : fabStyle]}
style={[
styles.content,
label ? extendedStyle : fabStyle,
iconPosition === 'left' ? styles.iconLeft : styles.iconRight,
]}
testID={`${testID}-content`}
pointerEvents="none"
>
Expand Down Expand Up @@ -349,8 +358,13 @@ const styles = StyleSheet.create({
elevated: {
elevation: 6,
},
content: {
iconLeft: {
flexDirection: 'row',
},
iconRight: {
flexDirection: 'row-reverse',
},
content: {
alignItems: 'center',
justifyContent: 'center',
},
Expand Down
17 changes: 17 additions & 0 deletions src/components/__tests__/FAB.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ it('renders FAB with uppercase styling if uppercase prop is truthy', () => {
});
});

(['left', 'right'] as const).forEach((iconPosition) => {
it(`renders FAB with icon positioned to the ${iconPosition}`, () => {
const { getByTestId } = render(
<FAB
onPress={() => {}}
icon="plus"
iconPosition={iconPosition}
testID={`${iconPosition}-icon-fab`}
/>
);

expect(getByTestId(`${iconPosition}-icon-fab-content`)).toHaveStyle({
flexDirection: iconPosition === 'left' ? 'row' : 'row-reverse',
});
});
});

describe('getFABColors - background color', () => {
it('should return color from styles', () => {
expect(
Expand Down
48 changes: 36 additions & 12 deletions src/components/__tests__/__snapshots__/FAB.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ exports[`renders FAB with custom size prop 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 25,
"height": 100,
"width": 100,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -273,14 +275,16 @@ exports[`renders custom color for the icon and label of the FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -454,14 +458,16 @@ exports[`renders default FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -635,14 +641,16 @@ exports[`renders disabled FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -817,14 +825,16 @@ exports[`renders extended FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"paddingHorizontal": 16,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -1039,13 +1049,15 @@ exports[`renders extended FAB with custom size prop 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"height": 100,
"paddingHorizontal": 16,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -1259,14 +1271,16 @@ exports[`renders large FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 28,
"height": 96,
"width": 96,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -1440,14 +1454,16 @@ exports[`renders loading FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -1745,14 +1761,16 @@ exports[`renders loading FAB with custom size prop 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 25,
"height": 100,
"width": 100,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -2050,14 +2068,16 @@ exports[`renders not visible FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -2231,14 +2251,16 @@ exports[`renders small FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 12,
"height": 40,
"width": 40,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down Expand Up @@ -2412,14 +2434,16 @@ exports[`renders visible FAB 1`] = `
[
{
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
{
"borderRadius": 16,
"height": 56,
"width": 56,
},
{
"flexDirection": "row",
},
]
}
testID="fab-content"
Expand Down