Skip to content

Commit

Permalink
fix: expose ref on ActionSheetProvider for statically invoking (#283)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bradbyte authored Nov 4, 2022
1 parent fc9cefa commit b2f5f43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/ActionSheetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ 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;
useNativeDriver?: boolean;
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(
Expand All @@ -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]
Expand All @@ -42,4 +51,4 @@ export default function ActionSheetProvider({
</ActionSheet>
</Provider>
);
}
});
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down

0 comments on commit b2f5f43

Please sign in to comment.