2020-06-26 20:22:56 +00:00
|
|
|
import React, { useRef, useContext, forwardRef } from 'react';
|
2020-06-15 19:35:45 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import ActionSheet from './ActionSheet';
|
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
|
|
|
|
const context = React.createContext({
|
|
|
|
showActionSheet: () => {},
|
|
|
|
hideActionSheet: () => {}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const useActionSheet = () => useContext(context);
|
|
|
|
|
|
|
|
const { Provider, Consumer } = context;
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
export const withActionSheet = Component => forwardRef((props, ref) => (
|
|
|
|
<Consumer>
|
|
|
|
{contexts => <Component {...props} {...contexts} ref={ref} />}
|
|
|
|
</Consumer>
|
|
|
|
));
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
export const ActionSheetProvider = React.memo(({ children }) => {
|
|
|
|
const ref = useRef();
|
|
|
|
const { theme } = useTheme();
|
|
|
|
|
|
|
|
const getContext = () => ({
|
|
|
|
showActionSheet: (options) => {
|
|
|
|
ref.current?.showActionSheet(options);
|
|
|
|
},
|
|
|
|
hideActionSheet: () => {
|
|
|
|
ref.current?.hideActionSheet();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Provider value={getContext()}>
|
|
|
|
<ActionSheet ref={ref} theme={theme}>
|
|
|
|
{children}
|
|
|
|
</ActionSheet>
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
ActionSheetProvider.propTypes = {
|
|
|
|
children: PropTypes.node
|
|
|
|
};
|