Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Enable dynamic sliding tabs #361

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/ExNavigationReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,13 @@ class ExNavigationReducer {
const defaultRouteConfig = navigatorState.defaultRouteConfig;

const newChildren = routes.map(child => {
const newChild = child.clone();
newChild.config = _.merge({}, defaultRouteConfig, child.config);
return newChild;
if (child.clone) {
const newChild = child.clone();
newChild.config = _.merge({}, defaultRouteConfig, child.config);
return newChild;
} else {
return child;
}
});

return {
Expand Down
33 changes: 24 additions & 9 deletions src/sliding-tab/ExNavigationSlidingTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Props = {
renderFooter?: (props: any) => ?React.Element<any>,
renderLabel?: (routeParams: any) => ?React.Element<any>,
getRenderLabel?: (props: any) => (routeParams: any) => ?React.Element<any>,
tabBarScrollEnabled?: boolean,
style?: any,
swipeEnabled?: boolean,
tabBarStyle?: any,
Expand Down Expand Up @@ -87,6 +88,7 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
indicatorStyle: {},
position: 'top',
pressColor: 'rgba(0,0,0,0.2)',
tabBarScrollEnabled: false,
tabStyle: {},
renderBefore: () => null,
};
Expand Down Expand Up @@ -119,12 +121,9 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
}

componentWillMount() {
let tabItems = this._parseTabItems(this.props);

this._registerNavigatorContext();

let routes = tabItems.map(({ id, title }) => ({ title, key: id }));
let routeKeys = routes.map(r => r.key);
const { routes, routeKeys } = this._parseRoutes(this._parseTabItems(this.props));

this.props.navigation.dispatch(Actions.setCurrentNavigator(
this.state.navigatorUID,
Expand All @@ -142,10 +141,16 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
}

componentWillReceiveProps(nextProps) {
// TODO: Should make it possible to dynamically add children after initial render?
// if (nextProps.children && nextProps.children !== this.props.children) {
// this._parseTabItems(nextProps);
// }
if (nextProps.children && nextProps.children !== this.props.children) {
const { routes, routeKeys } = this._parseRoutes(this._parseTabItems(nextProps));
const navigationState = nextProps.navigationState;
const currentTabKey = (navigationState && navigationState.routes[navigationState.index].key) || nextProps.initialTab;
nextProps.navigation.dispatch(Actions.immediatelyResetStack(
this.state.navigatorUID,
routes,
routeKeys.indexOf(currentTabKey),
));
}
}

componentDidUpdate(prevProps) {
Expand All @@ -167,7 +172,6 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
}
}


render() {
if (!this.props.children || !this.state.tabItems) {
return null;
Expand Down Expand Up @@ -226,6 +230,7 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
tabStyle: this.props.tabStyle,
labelStyle: this.props.labelStyle,
renderLabel: renderLabelFn,
scrollEnabled: this.props.tabBarScrollEnabled,
style: [{backgroundColor: this.props.barBackgroundColor}, this.props.tabBarStyle],
};

Expand Down Expand Up @@ -283,6 +288,16 @@ class ExNavigationSlidingTab extends PureComponent<any, Props, State> {
return tabItems;
}

_parseRoutes(tabItems) {
const routeKeys = [];
const routes = tabItems.map(({ id, title }) => {
routeKeys.push(id);
return { title, key: id };
});

return { routes, routeKeys };
}

_setActiveTab = (i) => {
let tabItem = this.state.tabItems[i];
let key = tabItem.id;
Expand Down