import hoistNonReactStatics from 'hoist-non-react-statics'; import React, { ForwardedRef, forwardRef, useContext, useRef } from 'react'; import { TIconsName } from '../CustomIcon'; import ActionSheet from './ActionSheet'; export type TActionSheetOptionsItem = { title: string; icon: TIconsName; danger?: boolean; testID?: string; onPress: () => void; right?: () => React.ReactElement; }; export type TActionSheetOptions = { options?: TActionSheetOptionsItem[]; headerHeight?: number; customHeader?: React.ReactElement | null; hasCancel?: boolean; type?: string; children?: React.ReactElement | null; snaps?: (string | number)[]; onClose?: () => void; }; export interface IActionSheetProvider { showActionSheet: (item: TActionSheetOptions) => void; hideActionSheet: () => void; } const context = React.createContext({ showActionSheet: () => {}, hideActionSheet: () => {} }); export const useActionSheet = () => useContext(context); const { Provider, Consumer } = context; export const withActionSheet = (Component: React.ComponentType): typeof Component => { const WithActionSheetComponent = forwardRef((props: typeof React.Component, ref: ForwardedRef) => ( {(contexts: IActionSheetProvider) => } )); hoistNonReactStatics(WithActionSheetComponent, Component); return WithActionSheetComponent; }; export const ActionSheetProvider = React.memo(({ children }: { children: React.ReactElement | React.ReactElement[] }) => { const ref: ForwardedRef = useRef(null); const getContext = () => ({ showActionSheet: (options: TActionSheetOptions) => { ref.current?.showActionSheet(options); }, hideActionSheet: () => { ref.current?.hideActionSheet(); } }); return ( <>{children} ); });