import * as React from 'react'; import { Dimensions, View } from 'react-native'; import { useColors } from '../../hook/useColors'; import { useI18nContext } from '../../i18n'; import { Icon } from '../../ui/Image'; import { PressableHighlight } from '../../ui/Pressable'; import { TabPage } from '../../ui/TabPage'; import { SingleLineText } from '../../ui/Text'; import { useMessageInputBarNameMenu } from '../hooks/useMessageMenu'; import { InitMenuItemsType } from '../types'; import { ContextNameMenuProps, ContextNameMenuRef } from '../types'; import { MESSAGE_INPUT_BAR_EXTENSION_NAME_MENU_HEIGHT, MESSAGE_INPUT_BAR_EXTENSION_NAME_MENU_HEIGHT_HALF, } from './MessageInputBarExtensionNameMenu.const'; /** * The MessageInputBarExtensionNameMenu component provides menu functionality. * */ export const MessageInputBarExtensionNameMenu = React.forwardRef< ContextNameMenuRef, ContextNameMenuProps >(function ( props: ContextNameMenuProps, ref?: React.ForwardedRef ) { const { getColor } = useColors(); const [isShow, setIsShow] = React.useState(false); const { items, updateItems, updateProps, maxRowCount = 2, unitCountPerRow = 4, } = useMessageInputBarNameMenu(props); const perPageMaxCount = maxRowCount * unitCountPerRow; const pageCount = React.useMemo(() => { return Math.ceil(items.length / perPageMaxCount); }, [items.length, perPageMaxCount]); const pageIndexes = React.useMemo( () => Array.from({ length: pageCount }, (_, i) => i), [pageCount] ); React.useImperativeHandle(ref, () => { return { startShow: () => { setIsShow(true); }, startHide: (onFinished?: () => void) => { setIsShow(false); onFinished?.(); }, startShowWithInit: (initItems: InitMenuItemsType[], _?: any) => { setIsShow(true); updateItems(initItems); }, startShowWithProps: (props: ContextNameMenuProps) => { setIsShow(true); updateProps(props); }, getData: () => { return undefined; }, }; }, [updateItems, updateProps]); if (isShow === false) { return null; } return ( {pageCount <= 1 ? ( ) : ( { return { title: v.toString(), }; }), }, }} body={{ type: 'TabPageBodyT', BodyProps: { RenderChildren: BodyPagesT as any, // TODO: fix type !!! RenderChildrenProps: { ...props, initItems: items, maxRowCount, unitCountPerRow, index: 0, currentIndex: 0, } as any, }, }} headerPosition="down" /> )} ); }); function BodyPagesT( props: ContextNameMenuProps & { index: number; currentIndex: number; startPageIndex: number; } ) { const { index } = props; return ; } const ItemsRender = ( props: ContextNameMenuProps & { startPageIndex: number } ) => { const { initItems, maxRowCount = 2, unitCountPerRow = 4, startPageIndex, } = props; const { tr } = useI18nContext(); const { getColor } = useColors(); const screenWidth = Dimensions.get('window').width; const itemWidth = 64; const itemHeight = 88; return ( {initItems ? initItems.map((item, index) => { if ( index >= (startPageIndex + 1) * maxRowCount * unitCountPerRow || index < startPageIndex * maxRowCount * unitCountPerRow ) { return null; } return ( item.onClicked?.(item.name)} > {tr(item.name)} ); }) : ([] as React.ReactElement[])} ); }; export function useMessageInputBarExtensionNameMenu( props: ContextNameMenuProps ) { const { initItems } = props; const getMenuHeight = React.useCallback(() => { if ((initItems?.length ?? 0) <= 4) { return MESSAGE_INPUT_BAR_EXTENSION_NAME_MENU_HEIGHT_HALF; } else { return MESSAGE_INPUT_BAR_EXTENSION_NAME_MENU_HEIGHT; } }, [initItems?.length]); return { getMenuHeight, }; }