import { ReactNode, CSSProperties } from 'react' import classNames from 'classnames' import { useEvent } from '../../use' import { Popup, PopupProps } from '../popup/Popup' import { ShareSheetItem, ShareSheetItemProps } from './ShareSheetItem' import { CommonComponentProps } from '../../utils/types' import './ShareSheet.scss' export * from './ShareSheetItem' export type ShareSheetItemList = ShareSheetItemProps[] | ShareSheetItemProps[][] export interface ShareSheetProps extends CommonComponentProps { className?: string style?: CSSProperties children?: ReactNode maskClosable?: boolean title?: ReactNode description?: ReactNode itemList?: ShareSheetItemList cancel?: ReactNode onSelect?: (itemProps: ShareSheetItemProps, index: number) => any onCancel?: () => any visible?: boolean popupProps?: PopupProps } export function ShareSheet(props: ShareSheetProps) { const { className, children, visible, maskClosable = true, title, description, itemList = [], cancel, onSelect, onCancel, popupProps = {}, ...restProps } = props const { placement = 'bottom', ...restPopupProps } = popupProps const handleItemClick = useEvent( (itemProps: ShareSheetItemProps, index: number) => { onSelect?.(itemProps, index) } ) const handleMaskClick = useEvent(() => { if (maskClosable) { onCancel?.() } }) const handleCancelClick = useEvent(() => { onCancel?.() }) const shareSheetClass = classNames( 's-share-sheet', { 's-share-sheet-headless': !title && !description, }, className ) const row = (list: ShareSheetItemProps[], index?: number) => { return (
{list.map((ItemProps, index) => ( handleItemClick(ItemProps, index)} /> ))}
) } return (
{(title || description) && (
{title &&
{title}
} {description && (
{description}
)}
)}
{children || (itemList.length && Array.isArray(itemList[0]) ? (itemList as ShareSheetItemProps[][]).map((list, index) => row(list, index) ) : row(itemList as ShareSheetItemProps[]))}
{cancel && ( <>
{cancel}
)}
) } export default ShareSheet