Skip to content

Commit

Permalink
fix ref not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Sep 13, 2022
1 parent 4bf10e1 commit 7633041
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 69 deletions.
2 changes: 1 addition & 1 deletion dist/src/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions dist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
return to.concat(ar || Array.prototype.slice.call(from));
};
/* eslint-disable curly */
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState, } from 'react';
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState, } from 'react';
import { Animated, BackHandler, Dimensions, Easing, Modal, PanResponder, Platform, SafeAreaView, StatusBar, TouchableOpacity, View, } from 'react-native';
import { actionSheetEventManager } from './eventmanager';
import useSheetManager from './hooks/use-sheet-manager';
Expand Down Expand Up @@ -512,7 +512,7 @@ export default forwardRef(function ActionSheet(_a, ref) {
keyboard.keyboardShown,
keyboard.keyboardHeight,
]);
var getRef = function () { return ({
var getRef = useCallback(function () { return ({
show: function () {
setTimeout(function () {
setVisible(true);
Expand Down Expand Up @@ -554,8 +554,7 @@ export default forwardRef(function ActionSheet(_a, ref) {
},
isGestureEnabled: function () { return gestureEnabled; },
isOpen: function () { return visible; }
}); };
useImperativeHandle(ref, getRef, [
}); }, [
animations.translateY,
gestureEnabled,
getNextPosition,
Expand All @@ -565,6 +564,14 @@ export default forwardRef(function ActionSheet(_a, ref) {
snapPoints.length,
visible,
]);
useImperativeHandle(ref, getRef, [getRef]);
useEffect(function () {
if (props.id) {
SheetManager.registerRef(props.id, contextRef.current, {
current: getRef()
});
}
}, [getRef, props.id]);
var onRequestClose = React.useCallback(function () {
hideSheet();
}, [hideSheet]);
Expand Down Expand Up @@ -668,7 +675,9 @@ export default forwardRef(function ActionSheet(_a, ref) {
{ExtraOverlayComponent}
{!(props === null || props === void 0 ? void 0 : props.backgroundInteractionEnabled) ? (<TouchableOpacity onPress={onTouch} activeOpacity={defaultOverlayOpacity} testID={(_b = props.testIDs) === null || _b === void 0 ? void 0 : _b.backdrop} style={{
height: Dimensions.get('window').height + 100 ||
dimensions.height + (safeAreaPaddingTop.current || 0) + 100,
dimensions.height +
(safeAreaPaddingTop.current || 0) +
100,
width: '100%',
position: 'absolute',
zIndex: 2,
Expand Down
140 changes: 77 additions & 63 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, {
forwardRef,
RefObject,
useCallback,
useEffect,
useImperativeHandle,
useRef,
Expand Down Expand Up @@ -183,7 +184,7 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
// Fix a race condition when you open a action sheet while you have the keyboard opened.
if (initialValue.current === -1) {
return;
}
}

if (initialValue.current < prevKeyboardHeight.current + 50) {
initialValue.current = 0;
Expand Down Expand Up @@ -670,71 +671,82 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
],
);

const getRef = (): ActionSheetRef => ({
show: () => {
setTimeout(() => {
setVisible(true);
}, 1);
},
hide: (data: any) => {
hideSheet(data);
},
setModalVisible: (_visible?: boolean) => {
if (_visible) {
const getRef = useCallback(
(): ActionSheetRef => ({
show: () => {
setTimeout(() => {
setVisible(true);
}, 1);
} else {
hideSheet();
}
},
snapToOffset: (offset: number) => {
initialValue.current =
actionSheetHeight.current +
minTranslateValue.current -
(actionSheetHeight.current * offset) / 100;
Animated.spring(animations.translateY, {
toValue: initialValue.current,
useNativeDriver: true,
...props.openAnimationConfig,
}).start();
},
snapToIndex: (index: number) => {
if (index > snapPoints.length || index < 0) return;
currentSnapIndex.current = index;
initialValue.current = getNextPosition(index);
Animated.spring(animations.translateY, {
toValue: initialValue.current,
useNativeDriver: true,
...props.openAnimationConfig,
}).start();
},
handleChildScrollEnd: () => {
console.warn(
'handleChildScrollEnd has been removed. Please use `useScrollHandlers` hook to enable scrolling in ActionSheet',
);
},
modifyGesturesForLayout: (id, layout, scrollOffset) => {
//@ts-ignore
gestureBoundaries.current[id] = {
...layout,
scrollOffset: scrollOffset,
};
},
isGestureEnabled: () => gestureEnabled,
isOpen: () => visible,
});
},
hide: (data: any) => {
hideSheet(data);
},
setModalVisible: (_visible?: boolean) => {
if (_visible) {
setTimeout(() => {
setVisible(true);
}, 1);
} else {
hideSheet();
}
},
snapToOffset: (offset: number) => {
initialValue.current =
actionSheetHeight.current +
minTranslateValue.current -
(actionSheetHeight.current * offset) / 100;
Animated.spring(animations.translateY, {
toValue: initialValue.current,
useNativeDriver: true,
...props.openAnimationConfig,
}).start();
},
snapToIndex: (index: number) => {
if (index > snapPoints.length || index < 0) return;
currentSnapIndex.current = index;
initialValue.current = getNextPosition(index);
Animated.spring(animations.translateY, {
toValue: initialValue.current,
useNativeDriver: true,
...props.openAnimationConfig,
}).start();
},
handleChildScrollEnd: () => {
console.warn(
'handleChildScrollEnd has been removed. Please use `useScrollHandlers` hook to enable scrolling in ActionSheet',
);
},
modifyGesturesForLayout: (id, layout, scrollOffset) => {
//@ts-ignore
gestureBoundaries.current[id] = {
...layout,
scrollOffset: scrollOffset,
};
},
isGestureEnabled: () => gestureEnabled,
isOpen: () => visible,
}),
[
animations.translateY,
gestureEnabled,
getNextPosition,
hideSheet,
props.openAnimationConfig,
setVisible,
snapPoints.length,
visible,
],
);

useImperativeHandle(ref, getRef, [
animations.translateY,
gestureEnabled,
getNextPosition,
hideSheet,
props.openAnimationConfig,
setVisible,
snapPoints.length,
visible,
]);
useImperativeHandle(ref, getRef, [getRef]);

useEffect(() => {
if (props.id) {
SheetManager.registerRef(props.id, contextRef.current, {
current: getRef(),
} as RefObject<ActionSheetRef>);
}
}, [getRef, props.id]);

const onRequestClose = React.useCallback(() => {
hideSheet();
Expand Down Expand Up @@ -870,7 +882,9 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
style={{
height:
Dimensions.get('window').height + 100 ||
dimensions.height + (safeAreaPaddingTop.current || 0) + 100,
dimensions.height +
(safeAreaPaddingTop.current || 0) +
100,
width: '100%',
position: 'absolute',
zIndex: 2,
Expand Down

1 comment on commit 7633041

@vercel
Copy link

@vercel vercel bot commented on 7633041 Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rnas – ./

rnas-git-master-ammarahm-ed.vercel.app
rnas.vercel.app
rnas-ammarahm-ed.vercel.app

Please sign in to comment.