From b2f5f433ff2070c77fdd62b273d2d80e636efd3a Mon Sep 17 00:00:00 2001 From: BradByte Date: Fri, 4 Nov 2022 15:13:18 -0500 Subject: [PATCH] fix: expose ref on ActionSheetProvider for statically invoking (#283) This fixes an unknown breaking change from 4.0.0 where attempting to put a ref directly on `ActionSheetProvider` would error. For simplicity, the ref on that component will now return an object containing `showActionSheetWithOptions()`, but for backwards compatibility `getContext()` is also available. --- src/ActionSheetProvider.tsx | 23 ++++++++++++++++------- src/types.ts | 7 +++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/ActionSheetProvider.tsx b/src/ActionSheetProvider.tsx index abf598a..d744657 100644 --- a/src/ActionSheetProvider.tsx +++ b/src/ActionSheetProvider.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; import NativeActionSheet from './ActionSheet'; import CustomActionSheet from './ActionSheet/CustomActionSheet'; import { Provider } from './context'; -import { ActionSheetOptions } from './types'; +import { ActionSheetOptions, ActionSheetProviderRef } from './types'; interface Props { children: React.ReactNode; @@ -12,11 +12,10 @@ interface Props { useCustomActionSheet?: boolean; } -export default function ActionSheetProvider({ - children, - useNativeDriver, - useCustomActionSheet = false, -}: Props) { +export default React.forwardRef(function ActionSheetProvider( + { children, useNativeDriver, useCustomActionSheet = false }, + ref +) { const actionSheetRef = React.useRef(null); const context = React.useMemo( @@ -30,6 +29,16 @@ export default function ActionSheetProvider({ [actionSheetRef] ); + React.useImperativeHandle( + ref, + () => ({ + // backwards compatible with 13.x before context was being passed right on the ref + getContext: () => context, + showActionSheetWithOptions: context.showActionSheetWithOptions, + }), + [context] + ); + const ActionSheet = React.useMemo( () => (useCustomActionSheet ? CustomActionSheet : NativeActionSheet), [useCustomActionSheet] @@ -42,4 +51,4 @@ export default function ActionSheetProvider({ ); -} +}); diff --git a/src/types.ts b/src/types.ts index d50fc1d..44ad7ca 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,6 +8,13 @@ export interface ActionSheetProps { ) => void; } +export interface ActionSheetProviderRef extends ActionSheetProps { + /** + * @deprecated Simply call `showActionSheetWithOptions()` directly from the ref now + */ + getContext: () => ActionSheetProps; +} + // for iOS export interface ActionSheetIOSOptions { options: string[];