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.
This commit is contained in:
BradByte authored and GitHub committed 2022-11-04 15:13:18 -05:00
1 parent fc9cefa2c1
commit b2f5f433ff
2 files changed
+23 -7

No files matched your search

+16 -7
View File
@@ -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<ActionSheetProviderRef, Props>(function ActionSheetProvider(
{ children, useNativeDriver, useCustomActionSheet = false },
ref
) {
const actionSheetRef = React.useRef<NativeActionSheet>(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({
</ActionSheet>
</Provider>
);
}
});
+7
View File
@@ -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[];