Skip to content

Commit

Permalink
feat: support for titleMaxFontSizeMultiplier (#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak authored Mar 6, 2023
1 parent db04a8f commit 43a9b17
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
9 changes: 8 additions & 1 deletion src/components/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export type Props = {
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Specifies the largest possible scale a title font can reach.
*/
titleMaxFontSizeMultiplier?: number;
/**
* @optional
*/
Expand Down Expand Up @@ -113,11 +117,12 @@ const MenuItem = ({
onPress,
style,
contentStyle,
testID,
testID = 'menu-item',
titleStyle,
accessibilityLabel,
accessibilityState,
theme: themeOverrides,
titleMaxFontSizeMultiplier = 1.5,
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const { titleColor, iconColor, underlayColor } = getMenuItemColor({
Expand Down Expand Up @@ -188,7 +193,9 @@ const MenuItem = ({
variant="bodyLarge"
selectable={false}
numberOfLines={1}
testID={`${testID}-title`}
style={[!isV3 && styles.title, titleTextStyle, titleStyle]}
maxFontSizeMultiplier={titleMaxFontSizeMultiplier}
>
{title}
</Text>
Expand Down
99 changes: 59 additions & 40 deletions src/components/__tests__/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,65 @@ import { black, white } from '../../styles/themes/v2/colors';
import Menu from '../Menu/Menu';
import { getMenuItemColor } from '../Menu/utils';

describe('Menu Item', () => {
it('renders menu item', () => {
const tree = renderer
.create(
<>
<Menu.Item leadingIcon="redo" onPress={() => {}} title="Redo" />
<Menu.Item leadingIcon="undo" onPress={() => {}} title="Undo" />
<Menu.Item
leadingIcon="content-cut"
onPress={() => {}}
title="Cut"
disabled
/>
<Menu.Item
leadingIcon="content-copy"
onPress={() => {}}
title="Copy"
disabled
/>
<Menu.Item onPress={() => {}} title="Paste" />
</>
)
.toJSON();

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

it('should have titleMaxFontSizeMultiplier passed to title', () => {
const labelMaxFontSizeMultiplier = 2;

const { getByTestId } = render(
<Menu.Item
titleMaxFontSizeMultiplier={labelMaxFontSizeMultiplier}
leadingIcon="content-cut"
onPress={() => {}}
title="Cut"
/>
);

expect(getByTestId('menu-item-title').props.maxFontSizeMultiplier).toBe(
labelMaxFontSizeMultiplier
);
});

it('accepts different values for accessibilityState', () => {
const { getByTestId } = render(
<Menu.Item
accessibilityState={{ checked: true }}
title="Option 1"
testID="touchable"
/>
);

expect(getByTestId('touchable').props.accessibilityState).toMatchObject({
checked: true,
});
});
});

describe('getMenuItemColor - title color', () => {
it('should return disabled color if disabled, for theme version 3', () => {
expect(
Expand Down Expand Up @@ -145,43 +204,3 @@ describe('getMenuItemColor - underlay color', () => {
});
});
});

it('renders menu item', () => {
const tree = renderer
.create(
<>
<Menu.Item leadingIcon="redo" onPress={() => {}} title="Redo" />
<Menu.Item leadingIcon="undo" onPress={() => {}} title="Undo" />
<Menu.Item
leadingIcon="content-cut"
onPress={() => {}}
title="Cut"
disabled
/>
<Menu.Item
leadingIcon="content-copy"
onPress={() => {}}
title="Copy"
disabled
/>
<Menu.Item onPress={() => {}} title="Paste" />
</>
)
.toJSON();

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

it('accepts different values for accessibilityState', () => {
const { getByTestId } = render(
<Menu.Item
accessibilityState={{ checked: true }}
title="Option 1"
testID="touchable"
/>
);

expect(getByTestId('touchable').props.accessibilityState).toMatchObject({
checked: true,
});
});
12 changes: 12 additions & 0 deletions src/components/__tests__/__snapshots__/Menu.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -365,6 +366,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -397,6 +399,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Undo
</Text>
Expand Down Expand Up @@ -441,6 +444,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -469,6 +473,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -501,6 +506,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Redo
</Text>
Expand Down Expand Up @@ -1020,6 +1026,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -1048,6 +1055,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -1080,6 +1088,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Undo
</Text>
Expand Down Expand Up @@ -1124,6 +1133,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -1152,6 +1162,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -1184,6 +1195,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Redo
</Text>
Expand Down
17 changes: 16 additions & 1 deletion src/components/__tests__/__snapshots__/MenuItem.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders menu item 1`] = `
exports[`Menu Item renders menu item 1`] = `
Array [
<View
accessibilityRole="menuitem"
Expand Down Expand Up @@ -40,6 +40,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -116,6 +117,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -148,6 +150,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Redo
</Text>
Expand Down Expand Up @@ -192,6 +195,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -268,6 +272,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -300,6 +305,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Undo
</Text>
Expand Down Expand Up @@ -344,6 +350,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -420,6 +427,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -452,6 +460,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Cut
</Text>
Expand Down Expand Up @@ -496,6 +505,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -572,6 +582,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -604,6 +615,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Copy
</Text>
Expand Down Expand Up @@ -648,6 +660,7 @@ Array [
],
]
}
testID="menu-item"
>
<View
style={
Expand Down Expand Up @@ -676,6 +689,7 @@ Array [
}
>
<Text
maxFontSizeMultiplier={1.5}
numberOfLines={1}
selectable={false}
style={
Expand Down Expand Up @@ -708,6 +722,7 @@ Array [
],
]
}
testID="menu-item-title"
>
Paste
</Text>
Expand Down

0 comments on commit 43a9b17

Please sign in to comment.