import * as React from 'react'; import { AccessibilityState, ColorValue, GestureResponderEvent, PressableAndroidRippleConfig, StyleProp, StyleSheet, TextStyle, View, ViewStyle, } from 'react-native'; import { getContentMaxWidth, getMenuItemColor, MAX_WIDTH, MIN_WIDTH, } from './utils'; import { useInternalTheme } from '../../core/theming'; import type { ThemeProp } from '../../types'; import Icon, { IconSource } from '../Icon'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; export type Props = { /** * Title text for the `MenuItem`. */ title: React.ReactNode; /** * @renamed Renamed from 'icon' to 'leadingIcon' in v5.x * * Leading icon to display for the `MenuItem`. */ leadingIcon?: IconSource; /** * @supported Available in v5.x with theme version 3 * * Trailing icon to display for the `MenuItem`. */ trailingIcon?: IconSource; /** * Whether the 'item' is disabled. A disabled 'item' is greyed out and `onPress` is not called on touch. */ disabled?: boolean; /** * @supported Available in v5.x with theme version 3 * * Sets min height with densed layout. */ dense?: boolean; /** * Type of background drawabale to display the feedback (Android). * https://reactnative.dev/docs/pressable#rippleconfig */ background?: PressableAndroidRippleConfig; /** * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; /** * Specifies the largest possible scale a title font can reach. */ titleMaxFontSizeMultiplier?: number; /** * @optional */ style?: StyleProp; contentStyle?: StyleProp; titleStyle?: StyleProp; /** * Color of the ripple effect. */ rippleColor?: ColorValue; /** * @optional */ theme?: ThemeProp; /** * TestID used for testing purposes */ testID?: string; /** * Accessibility label for the Touchable. This is read by the screen reader when the user taps the component. */ accessibilityLabel?: string; /** * Accessibility state for the Touchable. This is read by the screen reader when the user taps the component. */ accessibilityState?: AccessibilityState; }; /** * A component to show a single list item inside a Menu. * * ## Usage * ```js * import * as React from 'react'; * import { View } from 'react-native'; * import { Menu } from 'react-native-paper'; * * const MyComponent = () => ( * * {}} title="Redo" /> * {}} title="Undo" /> * {}} title="Cut" disabled /> * {}} title="Copy" disabled /> * {}} title="Paste" /> * * ); * * export default MyComponent; * ``` */ const MenuItem = ({ leadingIcon, trailingIcon, dense, title, disabled, background, onPress, style, contentStyle, titleStyle, rippleColor: customRippleColor, testID = 'menu-item', accessibilityLabel, accessibilityState, theme: themeOverrides, titleMaxFontSizeMultiplier = 1.5, }: Props) => { const theme = useInternalTheme(themeOverrides); const { titleColor, iconColor, rippleColor } = getMenuItemColor({ theme, disabled, customRippleColor, }); const { isV3 } = theme; const containerPadding = isV3 ? 12 : 8; const iconWidth = isV3 ? 24 : 40; const minWidth = MIN_WIDTH - (isV3 ? 12 : 16); const maxWidth = getContentMaxWidth({ isV3, iconWidth, leadingIcon, trailingIcon, }); const titleTextStyle = { color: titleColor, ...(isV3 ? theme.fonts.bodyLarge : {}), }; const newAccessibilityState = { ...accessibilityState, disabled }; return ( {leadingIcon ? ( ) : null} {title} {isV3 && trailingIcon ? ( ) : null} ); }; MenuItem.displayName = 'Menu.Item'; const styles = StyleSheet.create({ container: { minWidth: MIN_WIDTH, maxWidth: MAX_WIDTH, height: 48, justifyContent: 'center', }, md3DenseContainer: { height: 32, }, row: { flexDirection: 'row', }, title: { fontSize: 16, }, item: { marginHorizontal: 8, }, content: { justifyContent: 'center', }, md3LeadingIcon: { marginLeft: 12, }, md3WithoutLeadingIcon: { marginLeft: 4, }, }); export default MenuItem;