Skip to content

Commit

Permalink
fix(react): don't show back button when not appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
elylucas committed Dec 3, 2019
1 parent f9bf8db commit 684293d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/react-router/package.json
Expand Up @@ -50,7 +50,7 @@
"@ionic/core": "4.11.5",
"@ionic/react": "4.11.5",
"@types/jest": "^23.3.9",
"@types/node": "12.6.9",
"@types/node": "^12.12.14",
"@types/react": "^16.9.2",
"@types/react-dom": "^16.9.0",
"@types/react-router": "^5.0.3",
Expand All @@ -70,7 +70,7 @@
"tslint": "^5.20.0",
"tslint-ionic-rules": "0.0.21",
"tslint-react": "^4.1.0",
"typescript": "3.5.3"
"typescript": "^3.7.2"
},
"jest": {
"preset": "ts-jest",
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/src/ReactRouter/NavManager.tsx
Expand Up @@ -3,6 +3,7 @@ import { NavContext, NavContextState } from '@ionic/react';
import { Location as HistoryLocation, UnregisterCallback } from 'history';
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';

import { StackManager } from './StackManager';

interface NavManagerProps extends RouteComponentProps {
Expand Down
24 changes: 10 additions & 14 deletions packages/react-router/src/ReactRouter/Router.tsx
Expand Up @@ -2,7 +2,7 @@ import { NavDirection } from '@ionic/core';
import { RouterDirection, getConfig } from '@ionic/react';
import { Action as HistoryAction, Location as HistoryLocation, UnregisterCallback } from 'history';
import React from 'react';
import { RouteComponentProps, withRouter, matchPath } from 'react-router-dom';
import { RouteComponentProps, matchPath, withRouter } from 'react-router-dom';

import { generateId, isDevMode } from '../utils';
import { LocationHistory } from '../utils/LocationHistory';
Expand All @@ -23,7 +23,7 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
activeIonPageId?: string;
currentDirection?: RouterDirection;
locationHistory = new LocationHistory();
routes: { [key: string]: any } = {};
routes: { [key: string]: any; } = {};

constructor(props: RouteComponentProps) {
super(props);
Expand All @@ -49,10 +49,6 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
});
}

// componentDidMount() {
// this.setActiveView(this.props.location, this.state.action!);
// }

componentDidUpdate(_prevProps: RouteComponentProps, prevState: RouteManagerState) {
// Trigger a page change if the location or action is different
if (this.state.location && prevState.location !== this.state.location || prevState.action !== this.state.action) {
Expand Down Expand Up @@ -144,12 +140,12 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
leavingView.mount = false;
this.removeOrphanedViews(enteringView, enteringViewStack);
}
leavingViewHtml = enteringView.id === leavingView.id ? leavingView.ionPageElement!.outerHTML : undefined;
leavingViewHtml = enteringView.id === leavingView.id ? leavingView.ionPageElement!.outerHTML : undefined;
} else {
// If there is not a leavingView, then we shouldn't provide a direction
direction = undefined;
}

} else {
enteringView.show = true;
enteringView.mount = true;
Expand All @@ -176,7 +172,7 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
if (enteringEl) {
// Don't animate from an empty view
const navDirection = leavingEl && leavingEl.innerHTML === '' ? undefined : direction === 'none' ? undefined : direction;
const shouldGoBack = !!enteringView.prevId || !!this.locationHistory.previous();
const shouldGoBack = !!enteringView.prevId;
this.commitView(
enteringEl!,
leavingEl!,
Expand Down Expand Up @@ -353,9 +349,9 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
const ionRouterOutlet = React.Children.only(routerOutlet) as React.ReactElement;

React.Children.forEach(ionRouterOutlet.props.children, (child: React.ReactElement) => {
for (let routeKey in this.routes) {
for (const routeKey in this.routes) {
const route = this.routes[routeKey];
if (route.props.path == child.props.path) {
if (route.props.path === child.props.path) {
this.routes[routeKey] = child;
}
}
Expand Down Expand Up @@ -408,11 +404,11 @@ class RouteManager extends React.Component<RouteComponentProps, RouteManagerStat
if (leavingView) {
if (leavingView.id === leavingView.prevId) {
const previousLocation = this.locationHistory.previous();
if(previousLocation) {
if (previousLocation) {
this.handleNavigate('replace', previousLocation.pathname + previousLocation.search, 'back');
} else {
defaultHref && this.handleNavigate('replace', defaultHref, 'back');
}
}
} else {
const { view: enteringView } = this.state.viewStacks.findViewInfoById(leavingView.prevId);
if (enteringView) {
Expand Down Expand Up @@ -459,7 +455,7 @@ function clonePageElement(leavingViewHtml: string) {
const newEl = document.createElement('div');
newEl.innerHTML = leavingViewHtml;
newEl.classList.add('ion-page-hidden');
newEl.style.zIndex = null;
newEl.style.zIndex = '';
// Remove an existing back button so the new element doesn't get two of them
const ionBackButton = newEl.getElementsByTagName('ion-back-button');
if (ionBackButton[0]) {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router/src/ReactRouter/StackManager.tsx
Expand Up @@ -96,7 +96,7 @@ class StackManagerInner extends React.Component<StackManagerProps, StackManagerS
ref: this.routerOutletEl
};

if(ionRouterOutlet.props.forwardedRef) {
if (ionRouterOutlet.props.forwardedRef) {
ionRouterOutlet.props.forwardedRef.current = this.routerOutletEl;
}

Expand All @@ -115,7 +115,7 @@ const withContext = (Component: any) => {
<RouteManagerContext.Consumer>
{context => <Component {...props} routeManager={context} />}
</RouteManagerContext.Consumer>
)
}
);
};

export const StackManager = withContext(StackManagerInner);
5 changes: 4 additions & 1 deletion packages/react-router/tslint.json
Expand Up @@ -27,6 +27,9 @@
"jsx-no-bind": false,
"jsx-no-lambda": false,
"jsx-no-multiline-js": false,
"jsx-wrap-multiline": false
"jsx-wrap-multiline": false,
"forin": false,
"strict-type-predicates": false,
"no-unused-expression": false
}
}
4 changes: 2 additions & 2 deletions packages/react/package.json
Expand Up @@ -48,7 +48,7 @@
},
"devDependencies": {
"@types/jest": "^23.3.9",
"@types/node": "10.12.9",
"@types/node": "^12.12.14",
"@types/react": "^16.9.2",
"@types/react-dom": "^16.9.0",
"fs-extra": "^8.1.0",
Expand All @@ -66,7 +66,7 @@
"tslint": "^5.18.0",
"tslint-ionic-rules": "0.0.21",
"tslint-react": "^4.0.0",
"typescript": "3.5.3"
"typescript": "^3.7.2"
},
"jest": {
"preset": "ts-jest",
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/components/IonPage.tsx
Expand Up @@ -14,19 +14,19 @@ interface IonPageInternalProps extends IonPageProps {

class IonPageInternal extends React.Component<IonPageInternalProps> {
context!: React.ContextType<typeof NavContext>;
ref: React.RefObject<HTMLDivElement>;// React.createRef<HTMLDivElement>();
ref: React.RefObject<HTMLDivElement>;

constructor(props: IonPageInternalProps) {
super(props);
this.ref = this.props.forwardedRef || React.createRef()
this.ref = this.props.forwardedRef || React.createRef();
}

componentDidMount() {
if (this.context && this.ref && this.ref.current) {
if (this.context.hasIonicRouter()) {
this.context.registerIonPage(this.ref.current);
}
}
}
}

render() {
Expand All @@ -46,6 +46,6 @@ class IonPageInternal extends React.Component<IonPageInternalProps> {
static get contextType() {
return NavContext;
}
};
}

export const IonPage = createForwardRef(IonPageInternal, 'IonPage');
12 changes: 6 additions & 6 deletions packages/react/src/components/createControllerComponent.tsx
Expand Up @@ -15,12 +15,12 @@ export interface ReactControllerProps {

export const createControllerComponent = <OptionsType extends object, OverlayType extends OverlayBase>(
displayName: string,
controller: { create: (options: OptionsType) => Promise<OverlayType> }
controller: { create: (options: OptionsType) => Promise<OverlayType>; }
) => {
const dismissEventName = `on${displayName}DidDismiss`;

type Props = OptionsType & ReactControllerProps & {
forwardedRef?: React.RefObject<OverlayType>
forwardedRef?: React.RefObject<OverlayType>;
};

class Overlay extends React.Component<Props> {
Expand Down Expand Up @@ -80,16 +80,16 @@ export const createControllerComponent = <OptionsType extends object, OverlayTyp
if (this.props.forwardedRef) {
(this.props.forwardedRef as any).current = this.overlay;
}
await this.overlay.present();
await this.overlay.present();
}
}

render(): null {
return null;
}
};
}

return React.forwardRef<OverlayType, Props>((props, ref) => {
return <Overlay {...props} forwardedRef={ref} />
})
return <Overlay {...props} forwardedRef={ref} />;
});
};
10 changes: 5 additions & 5 deletions packages/react/src/components/createOverlayComponent.tsx
Expand Up @@ -17,12 +17,12 @@ export interface ReactOverlayProps {

export const createOverlayComponent = <OverlayComponent extends object, OverlayType extends OverlayElement>(
displayName: string,
controller: { create: (options: any) => Promise<OverlayType> }
controller: { create: (options: any) => Promise<OverlayType>; }
) => {
const dismissEventName = `on${displayName}DidDismiss`;

type Props = OverlayComponent & ReactOverlayProps & {
forwardedRef?: React.RefObject<OverlayType>
forwardedRef?: React.RefObject<OverlayType>;
};

class Overlay extends React.Component<Props> {
Expand Down Expand Up @@ -96,9 +96,9 @@ export const createOverlayComponent = <OverlayComponent extends object, OverlayT
this.el
);
}
};
}

return React.forwardRef<OverlayType, Props>((props, ref) => {
return <Overlay {...props} forwardedRef={ref} />
})
return <Overlay {...props} forwardedRef={ref} />;
});
};
5 changes: 3 additions & 2 deletions packages/react/tslint.json
Expand Up @@ -14,7 +14,6 @@
"trailing-comma": false,
"no-null-keyword": false,
"no-console": false,
"no-unbound-method": true,
"no-floating-promises": false,
"no-invalid-template-strings": true,
"ban-export-const-enum": true,
Expand All @@ -27,6 +26,8 @@
"jsx-no-bind": false,
"jsx-no-lambda": false,
"jsx-no-multiline-js": false,
"jsx-wrap-multiline": false
"jsx-wrap-multiline": false,
"no-empty-interface": false,
"no-unbound-method": false
}
}

0 comments on commit 684293d

Please sign in to comment.