Skip to content

Commit

Permalink
Merge pull request #29763 from software-mansion-labs/ts-migration/Swi…
Browse files Browse the repository at this point in the history
…peableView

[No QA][TS migration] Migrate 'SwipeableView' component to TypeScript
  • Loading branch information
NikkiWines authored Oct 21, 2023
2 parents 554347e + 742aa83 commit 6031f3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/components/SwipeableView/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
import React, {useRef} from 'react';
import {PanResponder, View} from 'react-native';
import PropTypes from 'prop-types';
import CONST from '../../CONST';
import SwipeableViewProps from './types';

const propTypes = {
children: PropTypes.element.isRequired,

/** Callback to fire when the user swipes down on the child content */
onSwipeDown: PropTypes.func.isRequired,
};

function SwipeableView(props) {
function SwipeableView({children, onSwipeDown}: SwipeableViewProps) {
const minimumPixelDistance = CONST.COMPOSER_MAX_HEIGHT;
const oldYRef = useRef(0);
const panResponder = useRef(
PanResponder.create({
// The PanResponder gets focus only when the y-axis movement is over minimumPixelDistance
// & swipe direction is downwards
// The PanResponder gets focus only when the y-axis movement is over minimumPixelDistance & swipe direction is downwards
// eslint-disable-next-line @typescript-eslint/naming-convention
onMoveShouldSetPanResponderCapture: (_event, gestureState) => {
if (gestureState.dy - oldYRef.current > 0 && gestureState.dy > minimumPixelDistance) {
return true;
}
oldYRef.current = gestureState.dy;
return false;
},

// Calls the callback when the swipe down is released; after the completion of the gesture
onPanResponderRelease: props.onSwipeDown,
onPanResponderRelease: onSwipeDown,
}),
).current;

return (
// eslint-disable-next-line react/jsx-props-no-spreading
<View {...panResponder.panHandlers}>{props.children}</View>
<View {...panResponder.panHandlers}>{children}</View>
);
}

SwipeableView.propTypes = propTypes;
SwipeableView.displayName = 'SwipeableView';

export default SwipeableView;
4 changes: 4 additions & 0 deletions src/components/SwipeableView/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import SwipeableViewProps from './types';

// Swipeable View is available just on Android/iOS for now.
export default ({children}: SwipeableViewProps) => children;
11 changes: 11 additions & 0 deletions src/components/SwipeableView/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {ReactNode} from 'react';

type SwipeableViewProps = {
/** The content to be rendered within the SwipeableView */
children: ReactNode;

/** Callback to fire when the user swipes down on the child content */
onSwipeDown: () => void;
};

export default SwipeableViewProps;

0 comments on commit 6031f3e

Please sign in to comment.