This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use pure component for stack items
- Loading branch information
Showing
5 changed files
with
220 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import * as React from 'react'; | ||
import { StyleSheet, Platform } from 'react-native'; | ||
import Animated from 'react-native-reanimated'; | ||
import { Props as HeaderContainerProps } from '../Header/HeaderContainer'; | ||
import Card from './Card'; | ||
import { | ||
Route, | ||
HeaderScene, | ||
GestureDirection, | ||
Layout, | ||
TransitionSpec, | ||
CardStyleInterpolator, | ||
HeaderMode, | ||
NavigationProp, | ||
HeaderStyleInterpolator, | ||
} from '../../types'; | ||
|
||
type Props = { | ||
index: number; | ||
active: boolean; | ||
focused: boolean; | ||
closing: boolean; | ||
layout: Layout; | ||
current: Animated.Value<number>; | ||
previousScene?: HeaderScene<Route>; | ||
scene: HeaderScene<Route>; | ||
navigation: NavigationProp; | ||
transparentCard?: boolean; | ||
cardOverlayEnabled?: boolean; | ||
cardShadowEnabled?: boolean; | ||
gesturesEnabled?: boolean; | ||
direction: GestureDirection; | ||
getPreviousRoute: (props: { route: Route }) => Route | undefined; | ||
renderHeader: (props: HeaderContainerProps) => React.ReactNode; | ||
renderScene: (props: { route: Route }) => React.ReactNode; | ||
onOpenRoute: (props: { route: Route }) => void; | ||
onCloseRoute: (props: { route: Route }) => void; | ||
onGoBack: (props: { route: Route }) => void; | ||
onTransitionStart?: ( | ||
curr: { index: number }, | ||
prev: { index: number } | ||
) => void; | ||
onGestureBegin?: () => void; | ||
onGestureCanceled?: () => void; | ||
onGestureEnd?: () => void; | ||
gestureResponseDistance?: { | ||
vertical?: number; | ||
horizontal?: number; | ||
}; | ||
transitionSpec: { | ||
open: TransitionSpec; | ||
close: TransitionSpec; | ||
}; | ||
headerStyleInterpolator: HeaderStyleInterpolator; | ||
cardStyleInterpolator: CardStyleInterpolator; | ||
headerMode: HeaderMode; | ||
floaingHeaderHeight: number; | ||
hasCustomHeader: boolean; | ||
}; | ||
|
||
export default class StackItem extends React.PureComponent<Props> { | ||
private handleOpen = () => | ||
this.props.onOpenRoute({ route: this.props.scene.route }); | ||
|
||
private handleClose = () => | ||
this.props.onOpenRoute({ route: this.props.scene.route }); | ||
|
||
private handleTransitionStart = ({ closing }: { closing: boolean }) => { | ||
const { index, scene, onTransitionStart, onGoBack } = this.props; | ||
|
||
onTransitionStart && | ||
onTransitionStart({ index: closing ? index - 1 : index }, { index }); | ||
|
||
closing && onGoBack({ route: scene.route }); | ||
}; | ||
|
||
render() { | ||
const { | ||
layout, | ||
active, | ||
focused, | ||
closing, | ||
current, | ||
navigation, | ||
scene, | ||
previousScene, | ||
direction, | ||
transparentCard, | ||
cardOverlayEnabled, | ||
cardShadowEnabled, | ||
gesturesEnabled, | ||
onGestureBegin, | ||
onGestureCanceled, | ||
onGestureEnd, | ||
gestureResponseDistance, | ||
transitionSpec, | ||
headerStyleInterpolator, | ||
cardStyleInterpolator, | ||
floaingHeaderHeight, | ||
hasCustomHeader, | ||
getPreviousRoute, | ||
headerMode, | ||
renderHeader, | ||
renderScene, | ||
} = this.props; | ||
|
||
return ( | ||
<Card | ||
active={active} | ||
transparent={transparentCard} | ||
direction={direction} | ||
layout={layout} | ||
current={current} | ||
next={scene.progress.next} | ||
closing={closing} | ||
onOpen={this.handleOpen} | ||
onClose={this.handleClose} | ||
overlayEnabled={cardOverlayEnabled} | ||
shadowEnabled={cardShadowEnabled} | ||
gesturesEnabled={gesturesEnabled} | ||
onTransitionStart={this.handleTransitionStart} | ||
onGestureBegin={onGestureBegin} | ||
onGestureCanceled={onGestureCanceled} | ||
onGestureEnd={onGestureEnd} | ||
gestureResponseDistance={gestureResponseDistance} | ||
transitionSpec={transitionSpec} | ||
styleInterpolator={cardStyleInterpolator} | ||
accessibilityElementsHidden={!focused} | ||
importantForAccessibility={focused ? 'auto' : 'no-hide-descendants'} | ||
pointerEvents="box-none" | ||
style={[ | ||
StyleSheet.absoluteFill, | ||
headerMode === 'float' && !hasCustomHeader | ||
? { marginTop: floaingHeaderHeight } | ||
: null, | ||
]} | ||
> | ||
{headerMode === 'screen' | ||
? renderHeader({ | ||
mode: 'screen', | ||
layout, | ||
scenes: [previousScene, scene], | ||
navigation, | ||
getPreviousRoute, | ||
styleInterpolator: headerStyleInterpolator, | ||
style: styles.header, | ||
}) | ||
: null} | ||
{renderScene({ route: scene.route })} | ||
</Card> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
header: { | ||
// This is needed to show elevation shadow | ||
zIndex: Platform.OS === 'android' ? 1 : 0, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters