Skip to content

Commit

Permalink
feat(back-button): add 'backButtonDefaultHref' property to Ionic Conf…
Browse files Browse the repository at this point in the history
…ig (#20491)

closes #19305

Co-authored-by: Brandy Carney <brandy@ionic.io>
  • Loading branch information
EinfachHans and brandyscarney committed Apr 27, 2020
1 parent 32ecdd6 commit 1b11ff7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
10 changes: 7 additions & 3 deletions angular/src/directives/navigation/ion-back-button.ts
@@ -1,5 +1,6 @@
import { Directive, HostListener, Optional } from '@angular/core';

import { Config } from '../../providers/config';
import { NavController } from '../../providers/nav-controller';

import { IonRouterOutlet } from './ion-router-outlet';
Expand All @@ -14,19 +15,22 @@ export class IonBackButtonDelegate {

constructor(
@Optional() private routerOutlet: IonRouterOutlet,
private navCtrl: NavController
private navCtrl: NavController,
private config: Config
) {}

/**
* @internal
*/
@HostListener('click', ['$event'])
onClick(ev: Event) {
const defaultHref = this.defaultHref || this.config.get('backButtonDefaultHref');

if (this.routerOutlet && this.routerOutlet.canGoBack()) {
this.routerOutlet.pop();
ev.preventDefault();
} else if (this.defaultHref != null) {
this.navCtrl.navigateBack(this.defaultHref);
} else if (defaultHref != null) {
this.navCtrl.navigateBack(defaultHref);
ev.preventDefault();
}
}
Expand Down
8 changes: 7 additions & 1 deletion core/src/components/back-button/back-button.tsx
Expand Up @@ -31,7 +31,7 @@ export class BackButton implements ComponentInterface, ButtonInterface {
/**
* The url to navigate back to by default when there is no history.
*/
@Prop() defaultHref?: string;
@Prop({ mutable: true }) defaultHref?: string;

/**
* If `true`, the user cannot interact with the button.
Expand All @@ -53,6 +53,12 @@ export class BackButton implements ComponentInterface, ButtonInterface {
*/
@Prop() type: 'submit' | 'reset' | 'button' = 'button';

componentWillLoad() {
if (this.defaultHref === undefined) {
this.defaultHref = config.get('backButtonDefaultHref');
}
}

get backButtonIcon() {
const icon = this.icon;
if (icon != null) {
Expand Down
33 changes: 33 additions & 0 deletions core/src/components/back-button/test/back-button.spec.ts
Expand Up @@ -70,4 +70,37 @@ describe('back button', () => {

});

describe('backButtonDefaultHref', () => {

it('set custom defaultHref in the config', async () => {
config.reset({
backButtonDefaultHref: 'custom-default-href-config'
});
const bb = await newBackButton();
expect(bb.defaultHref).toBe('custom-default-href-config');
});

it('set custom defaultHref on the instance', async () => {
const bb = await newBackButton();
bb.defaultHref = 'custom-default-href';
expect(bb.defaultHref).toBe('custom-default-href');
});

it('set custom defaultHref on the instance, override config', async () => {
const bb = await newBackButton();
bb.defaultHref = 'custom-default-href';

config.reset({
backButtonDefaultHref: 'custom-default-href-config'
});

expect(bb.defaultHref).toBe('custom-default-href');

const bb2 = await newBackButton();
bb2.defaultHref = 'custom-default-href-second';
expect(bb2.defaultHref).toBe('custom-default-href-second');
});

});

});
5 changes: 5 additions & 0 deletions core/src/utils/config.ts
Expand Up @@ -40,6 +40,11 @@ export interface IonicConfig {
*/
backButtonText?: string;

/**
* Overrides the default defaultHref in all `<ion-back-button>` components.
*/
backButtonDefaultHref?: string;

/**
* Overrides the default icon in all `<ion-menu-button>` components.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/react-router/src/ReactRouter/Router.tsx
Expand Up @@ -448,6 +448,9 @@ export class RouteManager extends React.Component<RouteManagerProps, RouteManage
}

navigateBack(defaultHref?: string) {
const config = getConfig();
defaultHref = defaultHref ? defaultHref : config && config.get('backButtonDefaultHref');

const { view: leavingView } = this.state.viewStacks.findViewInfoById(this.activeIonPageId);
if (leavingView) {
if (leavingView.id === leavingView.prevId) {
Expand Down

0 comments on commit 1b11ff7

Please sign in to comment.