From 395e53af736dfbf0dbf0bcaab94d9abf5ea3e94a Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Fri, 12 Oct 2018 12:39:44 -0700 Subject: [PATCH] Fix modular header back button to default to showing Back and respect visibility prop --- packages/stack/example/src/SimpleStack.js | 9 ++ packages/stack/src/views/Header/Header.js | 5 +- .../views/Header/ModularHeaderBackButton.js | 83 +++++++++++-------- 3 files changed, 62 insertions(+), 35 deletions(-) diff --git a/packages/stack/example/src/SimpleStack.js b/packages/stack/example/src/SimpleStack.js index 68b40f00..1f10eea0 100644 --- a/packages/stack/example/src/SimpleStack.js +++ b/packages/stack/example/src/SimpleStack.js @@ -3,6 +3,10 @@ import { Button, View, Text } from 'react-native'; import { createStackNavigator } from 'react-navigation-stack'; class ListScreen extends React.Component { + static navigationOptions = { + title: 'List', + }; + render() { return ( ; } + _getTitleText = () => { + const { width, title, truncatedTitle } = this.props; + + let { initialTextWidth } = this.state; + + if (title === null) { + return null; + } else if (!title) { + return truncatedTitle; + } else if (initialTextWidth && width && initialTextWidth > width) { + return truncatedTitle; + } else { + return title.length > 8 ? truncatedTitle : title; + } + }; + + _maybeRenderTitle() { + const { backTitleVisible, titleStyle, tintColor } = this.props; + let backTitleText = this._getTitleText(); + + if (!backTitleVisible || backTitleText === null) { + return null; + } + + const { LabelContainerComponent } = this.props; + + return ( + + + {this._getTitleText()} + + + ); + } + render() { const { onPress, @@ -61,25 +104,12 @@ class ModularHeaderBackButton extends React.PureComponent { truncatedTitle, } = this.props; - const renderTruncated = - this.state.initialTextWidth && width - ? this.state.initialTextWidth > width - : false; - - let backButtonTitle = renderTruncated ? truncatedTitle : title; - - // TODO: When we've sorted out measuring in the header, let's revisit this. - // This is clearly a bad workaround. - if (backButtonTitle && backButtonTitle.length > 8) { - backButtonTitle = truncatedTitle; - } - - const { ButtonContainerComponent, LabelContainerComponent } = this.props; + const { ButtonContainerComponent } = this.props; return ( {this._renderBackImage()} - {typeof backButtonTitle === 'string' && ( - - - {backButtonTitle} - - - )} + {this._maybeRenderTitle()} ); @@ -117,6 +133,7 @@ const styles = StyleSheet.create({ alignItems: 'center', flexDirection: 'row', backgroundColor: 'transparent', + marginBottom: 1, }, title: { fontSize: 17,