Skip to content

Commit

Permalink
fix: various fix on back button
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoninoBonanno authored and astagi committed Jan 9, 2023
1 parent a599178 commit 2718c5c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 89 deletions.
1 change: 1 addition & 0 deletions projects/design-angular-kit/assets/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"navigation": {
"home": "Home",
"go-back": "Torna indietro",
"upper-level": "Livello superiore",
"secondary-navigation": "Navigazione secondaria",
"login": "Accedi",
"full-login": "Accedi all'area personale",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
<a *ngIf="isLink" href="#" (click) = "goBack()">
<ng-template [ngTemplateOutlet]="content"></ng-template>
<a *ngIf="buttonStyle === 'link'" class="go-back" data-bs-toggle="historyback" (click)="goBack()" [routerLink]="null">
<ng-container *ngTemplateOutlet="content"></ng-container>
</a>
<it-button *ngIf = "!isLink" (click) = "goBack()" [color] = "buttonColor">
<ng-template [ngTemplateOutlet]="content"></ng-template>

<it-button *ngIf="buttonStyle === 'button'"
color="primary"
class="go-back"
data-bs-toggle="historyback"
(click)="goBack()">

<ng-container *ngTemplateOutlet="content"></ng-container>
</it-button>

<ng-template #content>
<it-icon *ngIf = "showIcon"
[name] = "icon"
[color] = "iconColor"
[size] = "iconSize"
[class]="iconClass"></it-icon>
<span [ngClass] = "{'visually-hidden': !showText}">
{{ text }}
<it-icon *ngIf="isShowIcon"
size="sm"
[name]="direction === 'left' ? 'arrow-left' : 'arrow-up'"
[color]="buttonStyle === 'link' ? 'primary' : 'white'"
[class.me-2]="isShowText"></it-icon>

<span [class.visually-hidden]="!isShowText">
{{(direction === 'left' ? 'it.navigation.go-back' : 'it.navigation.upper-level') | translate}}
</span>
</ng-template>
</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a.go-back {
cursor: pointer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { By } from '@angular/platform-browser';
import { ButtonComponent } from '../../core/button/button.component';
import { IconComponent } from '../../utils/icon/icon.component';
import { BackButtonComponent } from './back-button.component';
import { TranslateModule } from '@ngx-translate/core';

describe('BackButtonComponent', () => {
let component: BackButtonComponent;
let fixture: ComponentFixture<BackButtonComponent>;

beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [ BackButtonComponent, IconComponent, ButtonComponent]
declarations: [ BackButtonComponent, IconComponent, ButtonComponent],
imports: [ TranslateModule.forRoot() ]
})
.overrideComponent(BackButtonComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default }
Expand All @@ -28,18 +30,18 @@ describe('BackButtonComponent', () => {
});

it('deve essere di default un bottone', () => {
expect(component.isLink).toBe(false);
expect(component.buttonStyle).toBe('button');
const buttonElement = fixture.debugElement.query(By.css('button'));
expect(buttonElement).toBeTruthy();
const aElement = fixture.debugElement.query(By.css('a'));
expect(aElement).toBeFalsy();
});

it('deve poter essere un link', () => {
expect(component.isLink).toBe(false);
component.isLink = true;
expect(component.buttonStyle).toBe('button');
component.buttonStyle = 'link';
fixture.detectChanges();
expect(component.isLink).toBe(true);
expect(component.buttonStyle).toBe('link');
const aElement = fixture.debugElement.query(By.css('a'));
expect(aElement).toBeTruthy();
});
Expand All @@ -62,21 +64,21 @@ describe('BackButtonComponent', () => {
});

it('il click deve triggerare il location.back() se bottone', () => {
spyOn(component.location, 'back').and.returnValue();
spyOn(component._location, 'back').and.returnValue();
fixture.detectChanges();
const buttonElement = fixture.debugElement.query(By.css('button'));
buttonElement.nativeElement.click();
expect(component.location.back).toHaveBeenCalled();
expect(component._location.back).toHaveBeenCalled();
});

it('il click deve triggerare il location.back() se link', () => {
spyOn(component.location, 'back').and.returnValue();
component.isLink = true;
spyOn(component._location, 'back').and.returnValue();
component.buttonStyle = 'link';
fixture.detectChanges();
const aElement = fixture.debugElement.query(By.css('a'));
aElement.nativeElement.removeAttribute("href");
fixture.detectChanges();
aElement.nativeElement.click();
expect(component.location.back).toHaveBeenCalled();
expect(component._location.back).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,78 +1,73 @@
import { Component, Input } from '@angular/core';
import { Location } from '@angular/common';
import { IconColor, IconSize } from '../../../interfaces/icon';
import { ButtonColor } from '../../../interfaces/core';
import { BooleanInput, isTrueBooleanInput } from '../../../utils/boolean-input';

@Component({
selector: 'it-back-button',
templateUrl: './back-button.component.html'
templateUrl: './back-button.component.html',
styleUrls: ['./back-button.component.scss'],
exportAs: 'itBackButton'
})
export class BackButtonComponent {

/**
* Indica se va mostrato un link invece che un bottone.
*/
@Input()
get isLink(): boolean { return this._isLink; }
set isLink(value: boolean) {
this._isLink = value;
if (value) {
this.iconColor = 'primary';
} else {
this.iconColor = 'white';
}
}
private _isLink = false;
iconColor: IconColor = 'white';
* Back button style
* - <b>link</b>: use a link with icon and text
* - <b>button</b>: use a button with icon and text
* @default link
*/
@Input() buttonStyle: 'link' | 'button' = 'button';

/**
* Indica se l'icona va mostrata o meno.
*/
@Input()
get showIcon(): boolean { return this._showIcon; }
set showIcon(value: boolean) { this._showIcon = value; }
private _showIcon = true;
* Button direction
* - <b>left</b>: Back direction
* - <b>up</b>: Upper direction
* @default left
*/
@Input() direction: 'left' | 'up' = 'left';

/**
* Indica se il testo va mostrato o meno.
*/
@Input()
get showText(): boolean { return this._showText; }
set showText(value: boolean) { this._showText = value; }
private _showText = true;
* Show/Hide icon
* @default true
*/
@Input() showIcon: BooleanInput = true;

/**
* Indica se il tipo di BackButton è livello superiore o torna indietro.
*/
@Input()
get type(): 'tornaIndietro' | 'livelloSuperiore' { return this._type; }
set type(value: 'tornaIndietro' | 'livelloSuperiore') {
this._type = value;
if (value === 'tornaIndietro') {
this.icon = 'arrow-left';
this.text = ' Torna indietro';
} else if (value === 'livelloSuperiore') {
this.icon = 'arrow-up';
this.text = ' Livello superiore';
}
}
private _type: 'tornaIndietro' | 'livelloSuperiore' = 'tornaIndietro';
icon = 'it-arrow-left';
text = 'Torna indietro';
* Show/Hide text
* @default true
*/
@Input() showText: BooleanInput = true;

iconSize: IconSize = 'sm';
buttonColor: ButtonColor = 'primary';
/**
* Custom back logic <br/>
*
* NOTE: to use 'this' need bind function <br/>
* @example backCbFn = this.errorCallback.bind(this);
* (errorCallback is your function, pass backCbFn to the component)
*/
@Input() backFn?: (location: Location) => void;

get iconClass(): string {
return this.showText ? 'me-2' : '';
get isShowIcon(): boolean {
return isTrueBooleanInput(this.showIcon);
}

get isShowText(): boolean {
return isTrueBooleanInput(this.showText);
}

constructor(
public location: Location
) { }
public readonly _location: Location
) {
}

/**
* Go back function
*/
public goBack(): void {
if (this.backFn) {
return this.backFn(this._location);
}

goBack(): void {
this.location.back();
this._location.back();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ <h3>Pulsanti</h3>
<div class="bd-example">
<div class="row my-2">
<div class="col">
<it-back-button [type]="typeTornaIndietro"></it-back-button>
<it-back-button></it-back-button>
</div>
</div>
<div class="row my-2">
<div class="col">
<it-back-button [type]="typeLivelloSuperiore"></it-back-button>
<it-back-button direction="up"></it-back-button>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ import { Component } from '@angular/core';
})
export class BackButtonButtonComponent {

typeTornaIndietro: 'tornaIndietro' | 'livelloSuperiore' = 'tornaIndietro';
typeLivelloSuperiore: 'tornaIndietro' | 'livelloSuperiore' = 'livelloSuperiore';

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ <h3>Link</h3>
<div class="bd-example">
<div class="row my-2">
<div class="col">
<it-back-button [isLink]="true" [type]="typeTornaIndietro"></it-back-button>
<it-back-button buttonStyle="link"></it-back-button>
</div>
</div>
<div class="row my-2">
<div class="col">
<it-back-button [isLink]="true" [type]="typeLivelloSuperiore"></it-back-button>
<it-back-button buttonStyle="link" direction="up"></it-back-button>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ import { Component } from '@angular/core';
})
export class BackButtonLinkComponent {

typeTornaIndietro: 'tornaIndietro' | 'livelloSuperiore' = 'tornaIndietro';
typeLivelloSuperiore: 'tornaIndietro' | 'livelloSuperiore' = 'livelloSuperiore';

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ <h3>Pulsanti con sola icona</h3>
<div class="bd-example">
<div class="row my-2">
<div class="col-auto">
<it-back-button [showText]="false" [type]="typeTornaIndietro"></it-back-button>
<it-back-button [showText]="false"></it-back-button>
</div>
<div class="col-auto">
<it-back-button [showText]="false" [type]="typeLivelloSuperiore"></it-back-button>
<it-back-button [showText]="false" direction="up"></it-back-button>
</div>
</div>
</div>

0 comments on commit 2718c5c

Please sign in to comment.