Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ScrollableTabBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ const ScrollableTabBar = React.createClass({
</View>;
},

componentWillReceiveProps(nextProps) {
// If the tabs change, force the width of the tabs container to be recalculated
if (JSON.stringify(this.props.tabs) != JSON.stringify(nextProps.tabs) && this.state._containerWidth) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think that shallow comparison would be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I used JSON.stringify. It's a poor man's deep equals. There are more precise ways to do it but they all take a lot more code and have slower performance.

Regardless, yes, I believe this should be sufficient to ensure that any change will trigger the state to update correctly.

this.setState({ _containerWidth: null, });
}
},

onTabContainerLayout(e) {
this._tabContainerMeasurements = e.nativeEvent.layout;
let width = this._tabContainerMeasurements.width;
Expand Down
48 changes: 48 additions & 0 deletions examples/FacebookTabsExample/ChangingScrollableTabsExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import {
Text,
View,
TouchableHighlight,
} from 'react-native';

import ScrollableTabView, { ScrollableTabBar, } from 'react-native-scrollable-tab-view';

export default React.createClass({
getInitialState() {
return {
tabs: [ 'short', 'list' ],
};
},

render() {
return (
<ScrollableTabView
renderTabBar = { () => <ScrollableTabBar /> }
>
{
this.state.tabs.map(
(tab,index) =>
(
<View
tabLabel = { tab }
key = {index}
>
<TouchableHighlight
onPress = { () => this.setState({ tabs: [ 'short', 'list' ]})}
>
<Text> { 'short' } </Text>
</TouchableHighlight>
<TouchableHighlight
onPress = { () => this.setState({ tabs: [ 'long', 'list', 'with', 'very', 'many long tabs forcing', 'it to', 'scroll', 'and even longer' ]})}
>
<Text> { 'long' } </Text>
</TouchableHighlight>

</View>
)
)
}
</ScrollableTabView>
);
},
});
10 changes: 10 additions & 0 deletions examples/FacebookTabsExample/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ScrollableTabsExample from './ScrollableTabsExample';
import OverlayExample from './OverlayExample';
import FacebookExample from './FacebookExample';
import DynamicExample from './DynamicExample';
import ChangingScrollableTabsExample from './ChangingScrollableTabsExample';

export default React.createClass({
render() {
Expand All @@ -34,6 +35,8 @@ export default React.createClass({
return <FacebookExample />;
case 'dynamic':
return <DynamicExample />;
case 'changingScrollable':
return <ChangingScrollableTabsExample />;
default:
return <View style={styles.container}>
<TouchableOpacity
Expand Down Expand Up @@ -70,6 +73,13 @@ export default React.createClass({
>
<Text>Dynamic tabs example</Text>
</TouchableOpacity>

<TouchableOpacity
style={styles.button}
onPress={() => nav.push({id: 'changingScrollable', })}
>
<Text>Changing scrollable tabs example</Text>
</TouchableOpacity>
</View>;
}
},
Expand Down