2022-07-06 19:57:00 +00:00
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
2023-07-04 00:03:39 +00:00
|
|
|
import React, { createRef, ForwardedRef, forwardRef, useContext } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-05-02 19:21:15 +00:00
|
|
|
import { TIconsName } from '../CustomIcon';
|
2022-07-06 19:57:00 +00:00
|
|
|
import ActionSheet from './ActionSheet';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-05-03 00:48:08 +00:00
|
|
|
export type TActionSheetOptionsItem = {
|
|
|
|
title: string;
|
2022-08-01 16:35:05 +00:00
|
|
|
icon?: TIconsName;
|
2022-05-03 00:48:08 +00:00
|
|
|
danger?: boolean;
|
|
|
|
testID?: string;
|
|
|
|
onPress: () => void;
|
|
|
|
right?: () => React.ReactElement;
|
|
|
|
};
|
2022-03-22 20:44:27 +00:00
|
|
|
|
|
|
|
export type TActionSheetOptions = {
|
2022-05-19 16:53:45 +00:00
|
|
|
options?: TActionSheetOptionsItem[];
|
2022-05-02 22:58:23 +00:00
|
|
|
headerHeight?: number;
|
|
|
|
customHeader?: React.ReactElement | null;
|
2022-03-22 20:44:27 +00:00
|
|
|
hasCancel?: boolean;
|
2022-05-19 16:53:45 +00:00
|
|
|
type?: string;
|
|
|
|
children?: React.ReactElement | null;
|
2022-07-13 19:31:58 +00:00
|
|
|
snaps?: (string | number)[];
|
|
|
|
onClose?: () => void;
|
2022-08-03 16:32:16 +00:00
|
|
|
enableContentPanningGesture?: boolean;
|
2022-03-22 20:44:27 +00:00
|
|
|
};
|
2022-06-22 12:24:25 +00:00
|
|
|
export interface IActionSheetProvider {
|
2022-03-22 20:44:27 +00:00
|
|
|
showActionSheet: (item: TActionSheetOptions) => void;
|
|
|
|
hideActionSheet: () => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 20:44:27 +00:00
|
|
|
const context = React.createContext<IActionSheetProvider>({
|
2021-09-13 20:41:05 +00:00
|
|
|
showActionSheet: () => {},
|
|
|
|
hideActionSheet: () => {}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const useActionSheet = () => useContext(context);
|
|
|
|
|
|
|
|
const { Provider, Consumer } = context;
|
|
|
|
|
2022-07-06 19:57:00 +00:00
|
|
|
export const withActionSheet = (Component: React.ComponentType<any>): typeof Component => {
|
|
|
|
const WithActionSheetComponent = forwardRef((props: typeof React.Component, ref: ForwardedRef<IActionSheetProvider>) => (
|
2022-03-22 20:44:27 +00:00
|
|
|
<Consumer>{(contexts: IActionSheetProvider) => <Component {...props} {...contexts} ref={ref} />}</Consumer>
|
2021-09-13 20:41:05 +00:00
|
|
|
));
|
|
|
|
|
2022-07-06 19:57:00 +00:00
|
|
|
hoistNonReactStatics(WithActionSheetComponent, Component);
|
|
|
|
return WithActionSheetComponent;
|
|
|
|
};
|
|
|
|
|
2023-07-04 00:03:39 +00:00
|
|
|
const actionSheetRef: React.Ref<IActionSheetProvider> = createRef();
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2023-07-04 00:03:39 +00:00
|
|
|
export const ActionSheetProvider = React.memo(({ children }: { children: React.ReactElement | React.ReactElement[] }) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const getContext = () => ({
|
2022-03-22 20:44:27 +00:00
|
|
|
showActionSheet: (options: TActionSheetOptions) => {
|
2023-07-04 00:03:39 +00:00
|
|
|
actionSheetRef.current?.showActionSheet(options);
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
hideActionSheet: () => {
|
2023-07-04 00:03:39 +00:00
|
|
|
actionSheetRef.current?.hideActionSheet();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Provider value={getContext()}>
|
2023-07-04 00:03:39 +00:00
|
|
|
<ActionSheet ref={actionSheetRef}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<>{children}</>
|
|
|
|
</ActionSheet>
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
});
|
2023-07-04 00:03:39 +00:00
|
|
|
|
|
|
|
export const hideActionSheetRef = (): void => {
|
|
|
|
actionSheetRef?.current?.hideActionSheet();
|
|
|
|
};
|