/** @jsxImportSource @fluentui-react-native/framework-base */ import React from 'react'; import { I18nManager, Image, Pressable, View } from 'react-native'; import type { UseSlots } from '@fluentui-react-native/framework'; import { compose } from '@fluentui-react-native/framework'; import { memoize, mergeProps } from '@fluentui-react-native/framework-base'; import { IconV1 as Icon } from '@fluentui-react-native/icon'; import { TextV1 as Text } from '@fluentui-react-native/text'; import { SvgXml } from 'react-native-svg'; import { stylingSettings } from './MenuItem.styling'; import type { MenuItemProps, MenuItemType } from './MenuItem.types'; import { menuItemName } from './MenuItem.types'; import { useMenuItem } from './useMenuItem'; import { directComponent, getSingleChild } from '@fluentui-react-native/framework-base'; export const MenuItem = compose({ displayName: menuItemName, ...stylingSettings, slots: { root: Pressable, checkmark: View, content: Text, iconPlaceholder: View, imgIcon: Image, fontOrSvgIcon: Icon, submenuIndicator: SvgXml, }, useRender: (userProps: MenuItemProps, useSlots: UseSlots) => { const menuItem = useMenuItem(userProps); const Slots = useSlots(userProps, (layer): boolean => menuItem.state[layer] || userProps[layer]); return directComponent(({ children, ...final }: MenuItemProps) => { const { accessibilityLabel, icon, tooltip, ...mergedProps } = mergeProps(menuItem.props, final); const chevronXml = I18nManager.isRTL ? ` ` : ` `; // We only automatically support the one child string. const child = getSingleChild(children); const label = getAccessibilityLabel(accessibilityLabel, child); const tooltipResult = getTooltip(tooltip, menuItem.state.hasTooltips, child); return ( {menuItem.state.hasCheckmarks && } {(icon || menuItem.state.hasIcons) && ( {icon && icon.source && } {icon && (icon.svgSource || icon.fontSource) && } )} {child && ( {child} )} {menuItem.state.hasSubmenu && } ); }); }, }); const getAccessibilityLabelWorker = (accessibilityLabel: string, child: React.ReactNode) => { if (accessibilityLabel !== undefined) { return accessibilityLabel; } if (typeof child === 'string') { return child; } return undefined; }; export const getAccessibilityLabel = memoize(getAccessibilityLabelWorker); const getTooltipWorker = (tooltip: string, hasTooltips: boolean, child: React.ReactNode) => { if (tooltip !== undefined) { return tooltip; } if (hasTooltips && typeof child === 'string') { return child; } return undefined; }; export const getTooltip = memoize(getTooltipWorker);