import hoistNonReactStatics from 'hoist-non-react-statics'; import React, { createRef, ForwardedRef, forwardRef, useContext } 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; enableContentPanningGesture?: boolean; }; 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; }; const actionSheetRef: React.Ref = createRef(); export const ActionSheetProvider = React.memo(({ children }: { children: React.ReactElement | React.ReactElement[] }) => { const getContext = () => ({ showActionSheet: (options: TActionSheetOptions) => { actionSheetRef.current?.showActionSheet(options); }, hideActionSheet: () => { actionSheetRef.current?.hideActionSheet(); } }); return ( <>{children} ); }); export const hideActionSheetRef = (): void => { actionSheetRef?.current?.hideActionSheet(); };