diff --git a/src/components/BottomNavigation.js b/src/components/BottomNavigation.js index 595a6fe..15050d2 100644 --- a/src/components/BottomNavigation.js +++ b/src/components/BottomNavigation.js @@ -41,6 +41,10 @@ type Props = { * By default, this is `true` when you have more than 3 tabs. */ shifting?: boolean, + /** + * Whether to show labels in tabs. When `false`, only icons will be displayed. + */ + labeled?: boolean, /** * State for the bottom navigation. The state should contain the following properties: * @@ -289,6 +293,10 @@ class BottomNavigation extends React.Component, State> { ); } + static defaultProps = { + labeled: true, + }; + static getDerivedStateFromProps(nextProps, prevState) { const { index, routes } = nextProps.navigationState; @@ -436,6 +444,7 @@ class BottomNavigation extends React.Component, State> { activeTintColor, inactiveTintColor, barStyle, + labeled, style, theme, } = this.props; @@ -574,20 +583,23 @@ class BottomNavigation extends React.Component, State> { const focused = this.state.tabs[index]; // Scale up in the label - const scale = shifting - ? focused.interpolate({ - inputRange: [0, 1], - outputRange: [0.5, 1], - }) - : 1; + const scale = + labeled && shifting + ? focused.interpolate({ + inputRange: [0, 1], + outputRange: [0.5, 1], + }) + : 1; // Move down the icon to account for no-label in shifting and smaller label in non-shifting. - const translateY = shifting - ? focused.interpolate({ - inputRange: [0, 1], - outputRange: [10, 0], - }) - : 0; + const translateY = labeled + ? shifting + ? focused.interpolate({ + inputRange: [0, 1], + outputRange: [10, 0], + }) + : 0 + : 10; // We render the active icon and label on top of inactive ones and cross-fade them on change. // This trick gives the illusion that we are animating between active and inactive colors. @@ -651,58 +663,33 @@ class BottomNavigation extends React.Component, State> { )} - + {labeled ? ( - {renderLabel ? ( - renderLabel({ - route, - focused: true, - tintColor: activeColor, - }) - ) : ( - - {getLabelText({ route })} - - )} - - {shifting ? null : ( {renderLabel ? ( renderLabel({ route, - focused: false, - tintColor: inactiveColor, + focused: true, + tintColor: activeColor, }) ) : ( @@ -710,8 +697,37 @@ class BottomNavigation extends React.Component, State> { )} - )} - + {shifting ? null : ( + + {renderLabel ? ( + renderLabel({ + route, + focused: false, + tintColor: inactiveColor, + }) + ) : ( + + {getLabelText({ route })} + + )} + + )} + + ) : ( + + )} ); diff --git a/src/components/__tests__/BottomNavigation.test.js b/src/components/__tests__/BottomNavigation.test.js index c749269..bfa06d1 100644 --- a/src/components/__tests__/BottomNavigation.test.js +++ b/src/components/__tests__/BottomNavigation.test.js @@ -128,3 +128,35 @@ it('renders custom icon and label with custom colors in non-shifting bottom navi expect(tree).toMatchSnapshot(); }); + +it('hides labels in shifting bottom navigation', () => { + const tree = renderer + .create( + route.title} + /> + ) + .toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('hides labels in non-shifting bottom navigation', () => { + const tree = renderer + .create( + route.title} + /> + ) + .toJSON(); + + expect(tree).toMatchSnapshot(); +}); diff --git a/src/components/__tests__/__snapshots__/BottomNavigation.test.js.snap b/src/components/__tests__/__snapshots__/BottomNavigation.test.js.snap index 007fa26..60bfa7f 100644 --- a/src/components/__tests__/__snapshots__/BottomNavigation.test.js.snap +++ b/src/components/__tests__/__snapshots__/BottomNavigation.test.js.snap @@ -1,5 +1,1008 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`hides labels in non-shifting bottom navigation 1`] = ` + + + + + Route: 0 + + + + + + + + + + +  + + + + +  + + + + + + + + + + + +  + + + + +  + + + + + + + + + + + +  + + + + +  + + + + + + + + + +`; + +exports[`hides labels in shifting bottom navigation 1`] = ` + + + + + Route: 0 + + + + + + + + + + + +  + + + + +  + + + + + + + + + + + +  + + + + +  + + + + + + + + + + + +  + + + + +  + + + + + + + + + +`; + exports[`renders custom icon and label in non-shifting bottom navigation 1`] = `