2022-07-06 19:57:00 +00:00
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
2021-09-13 20:41:05 +00:00
|
|
|
import React, { ForwardedRef, forwardRef, useContext, useRef } from 'react';
|
|
|
|
|
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;
|
|
|
|
icon: TIconsName;
|
|
|
|
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;
|
|
|
|
snaps?: string[] | number[];
|
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;
|
|
|
|
};
|
|
|
|
|
2022-03-22 20:44:27 +00:00
|
|
|
export const ActionSheetProvider = React.memo(({ children }: { children: React.ReactElement | React.ReactElement[] }) => {
|
|
|
|
const ref: ForwardedRef<IActionSheetProvider> = useRef(null);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const getContext = () => ({
|
2022-03-22 20:44:27 +00:00
|
|
|
showActionSheet: (options: TActionSheetOptions) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
ref.current?.showActionSheet(options);
|
|
|
|
},
|
|
|
|
hideActionSheet: () => {
|
|
|
|
ref.current?.hideActionSheet();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Provider value={getContext()}>
|
2022-03-22 20:44:27 +00:00
|
|
|
<ActionSheet ref={ref}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<>{children}</>
|
|
|
|
</ActionSheet>
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
});
|