2023-03-05 13:08:57 +08:00

72 lines
1.7 KiB
TypeScript

import React, { useMemo, useRef, useImperativeHandle, forwardRef, useState } from 'react'
import { useI18n } from '@/lang'
import Menu, { type MenuType, type Position } from '@/components/common/Menu'
export interface SelectInfo {
listId: string
name: string
index: number
}
const initSelectInfo = {}
export interface ListMenuProps {
onPlay: (selectInfo: SelectInfo) => void
onCollect: (selectInfo: SelectInfo) => void
onHideMenu: () => void
}
export interface ListMenuType {
show: (selectInfo: SelectInfo, position: Position) => void
}
export type {
Position,
}
export default forwardRef<ListMenuType, ListMenuProps>((props, ref) => {
const t = useI18n()
const [visible, setVisible] = useState(false)
const menuRef = useRef<MenuType>(null)
const selectInfoRef = useRef<SelectInfo>(initSelectInfo as SelectInfo)
useImperativeHandle(ref, () => ({
show(selectInfo, position) {
selectInfoRef.current = selectInfo
if (visible) menuRef.current?.show(position)
else {
setVisible(true)
requestAnimationFrame(() => {
menuRef.current?.show(position)
})
}
},
}))
const menus = useMemo(() => {
return [
{ action: 'play', label: t('play') },
{ action: 'collect', label: t('collect') },
] as const
}, [t])
const handleMenuPress = ({ action }: typeof menus[number]) => {
const selectInfo = selectInfoRef.current
switch (action) {
case 'play':
props.onPlay(selectInfo)
break
case 'collect':
props.onCollect(selectInfo)
break
default:
break
}
}
return (
visible
? <Menu ref={menuRef} menus={menus} onPress={handleMenuPress} onHide={props.onHideMenu} />
: null
)
})