From 76330410e7a1c7d4c2a27cbad2f2f4314370896e Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Tue, 13 Sep 2022 13:23:19 +0500 Subject: [PATCH] fix ref not updated --- dist/src/index.d.ts.map | 2 +- dist/src/index.js | 19 ++++-- src/index.tsx | 140 ++++++++++++++++++++++------------------ 3 files changed, 92 insertions(+), 69 deletions(-) diff --git a/dist/src/index.d.ts.map b/dist/src/index.d.ts.map index bedca4f..ee59cca 100644 --- a/dist/src/index.d.ts.map +++ b/dist/src/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAON,MAAM,OAAO,CAAC;AACf,OAAO,EAML,eAAe,EAShB,MAAM,cAAc,CAAC;AAUtB,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,SAAS,CAAC;AAE9C,oBAAY,cAAc,GAAG;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,IAAI,CAAC;IAEjB;;OAEG;IACH,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;IAC3B;;OAEG;IACH,eAAe,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAE7C;;OAEG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC;;OAEG;IACH;;;OAGG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC;;OAEG;IACH,oBAAoB,EAAE,MAAM,IAAI,CAAC;IAEjC;;OAEG;IACH,uBAAuB,EAAE,CACvB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,eAAe,GAAG,SAAS,EACnC,YAAY,EAAE,MAAM,KACjB,IAAI,CAAC;IAEV,gBAAgB,EAAE,MAAM,OAAO,CAAC;IAEhC,MAAM,EAAE,MAAM,OAAO,CAAC;CACvB,CAAC;;AAIF,wBAw4BE"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAQN,MAAM,OAAO,CAAC;AACf,OAAO,EAML,eAAe,EAShB,MAAM,cAAc,CAAC;AAUtB,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,SAAS,CAAC;AAE9C,oBAAY,cAAc,GAAG;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,IAAI,CAAC;IAEjB;;OAEG;IACH,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;IAC3B;;OAEG;IACH,eAAe,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAE7C;;OAEG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC;;OAEG;IACH;;;OAGG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC;;OAEG;IACH,oBAAoB,EAAE,MAAM,IAAI,CAAC;IAEjC;;OAEG;IACH,uBAAuB,EAAE,CACvB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,eAAe,GAAG,SAAS,EACnC,YAAY,EAAE,MAAM,KACjB,IAAI,CAAC;IAEV,gBAAgB,EAAE,MAAM,OAAO,CAAC;IAEhC,MAAM,EAAE,MAAM,OAAO,CAAC;CACvB,CAAC;;AAIF,wBAq5BE"} \ No newline at end of file diff --git a/dist/src/index.js b/dist/src/index.js index 34da9ba..3bbf2c3 100644 --- a/dist/src/index.js +++ b/dist/src/index.js @@ -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'; @@ -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); @@ -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, @@ -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]); @@ -668,7 +675,9 @@ export default forwardRef(function ActionSheet(_a, ref) { {ExtraOverlayComponent} {!(props === null || props === void 0 ? void 0 : props.backgroundInteractionEnabled) ? (( // 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; @@ -670,71 +671,82 @@ export default forwardRef( ], ); - 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); + } + }, [getRef, props.id]); const onRequestClose = React.useCallback(() => { hideSheet(); @@ -870,7 +882,9 @@ export default forwardRef( 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,