Skip to content

Commit

Permalink
Add headerBackImage, closes react-navigation#1999
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvatne authored and sourcecode911 committed Mar 9, 2020
1 parent cd0cc63 commit a7f11c9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
19 changes: 15 additions & 4 deletions examples/NavigationPlayground/js/SimpleStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class MyNavScreen extends React.Component<MyNavScreenProps> {
onPress={() => navigation.navigate('Photos', { name: 'Jane' })}
title="Go to a photos screen"
/>
<Button
onPress={() =>
navigation.navigate('Profile', {
name: 'Dog',
headerBackImage: require('./assets/dog-back.png'),
})
}
title="Custom back button"
/>
<Button onPress={() => navigation.goBack(null)} title="Go back" />
<StatusBar barStyle="default" />
</SafeAreaView>
Expand Down Expand Up @@ -129,9 +138,9 @@ class MyPhotosScreen extends React.Component<MyPhotosScreenProps> {

const MyProfileScreen = ({ navigation }) => (
<MyNavScreen
banner={`${navigation.state.params.mode === 'edit'
? 'Now Editing '
: ''}${navigation.state.params.name}'s Profile`}
banner={`${navigation.state.params.mode === 'edit' ? 'Now Editing ' : ''}${
navigation.state.params.name
}'s Profile`}
navigation={navigation}
/>
);
Expand All @@ -141,14 +150,16 @@ MyProfileScreen.navigationOptions = props => {
const { state, setParams } = navigation;
const { params } = state;
return {
headerBackImage: params.headerBackImage,
headerTitle: `${params.name}'s Profile!`,
// Render a button on the right side of the header.
// When pressed switches the screen to edit mode.
headerRight: (
<Button
title={params.mode === 'edit' ? 'Done' : 'Edit'}
onPress={() =>
setParams({ mode: params.mode === 'edit' ? '' : 'edit' })}
setParams({ mode: params.mode === 'edit' ? '' : 'edit' })
}
/>
),
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/TypeDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';

import type { StyleObj } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';

import { Animated } from 'react-native';
import { Animated, Image } from 'react-native';

export type ViewStyleProp = StyleObj;
export type TextStyleProp = StyleObj;
Expand Down Expand Up @@ -315,6 +315,7 @@ export type NavigationStackScreenOptions = NavigationScreenOptions & {
headerTintColor?: string,
headerLeft?: React.Node | React.ComponentType<any>,
headerBackTitle?: string,
headerBackImage?: Image.propTypes.source,
headerTruncatedBackTitle?: string,
headerBackTitleStyle?: TextStyleProp,
headerPressColorAndroid?: string,
Expand Down
1 change: 1 addition & 0 deletions src/views/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Header extends React.PureComponent {
onPress={this._navigateBack}
pressColorAndroid={options.headerPressColorAndroid}
tintColor={options.headerTintColor}
buttonImage={options.headerBackImage}
title={backButtonTitle}
truncatedTitle={truncatedBackButtonTitle}
titleStyle={options.headerBackTitleStyle}
Expand Down
8 changes: 4 additions & 4 deletions src/views/Header/HeaderBackButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class HeaderBackButton extends React.PureComponent {
ios: '#037aff',
}),
truncatedTitle: 'Back',
// eslint-disable-next-line global-require
buttonImage: require('../assets/back-icon.png'),
};

state = {};
Expand All @@ -32,6 +34,7 @@ class HeaderBackButton extends React.PureComponent {

render() {
const {
buttonImage,
onPress,
pressColorAndroid,
width,
Expand All @@ -48,9 +51,6 @@ class HeaderBackButton extends React.PureComponent {

const backButtonTitle = renderTruncated ? truncatedTitle : title;

// eslint-disable-next-line global-require
const asset = require('../assets/back-icon.png');

return (
<TouchableItem
accessibilityComponentType="button"
Expand All @@ -70,7 +70,7 @@ class HeaderBackButton extends React.PureComponent {
!!title && styles.iconWithTitle,
!!tintColor && { tintColor },
]}
source={asset}
source={buttonImage}
/>
{Platform.OS === 'ios' &&
typeof backButtonTitle === 'string' && (
Expand Down

0 comments on commit a7f11c9

Please sign in to comment.