Skip to content

Commit 99c2eee

Browse files
jhonyeduardojhosefmarks
authored andcommitted
feat(info): possibilita transformar o valor em um link
- Adiciona nova propriedade `p-url` que da possibilidade de transformar o conteúdo em um link. - Adiciona ChangeDetectionStrategy.OnPush no componente po-info Fixes DTHFUI-2458
1 parent 4bdab45 commit 99c2eee

File tree

7 files changed

+117
-16
lines changed

7 files changed

+117
-16
lines changed

projects/ui/src/lib/components/po-info/po-info-base.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ export class PoInfoBaseComponent {
5959
return this._orientation;
6060
}
6161

62+
/**
63+
* Ao informar uma URL, o conteúdo será exibido na forma de um *link* e ao ser clicado será redirecionado para a URL informada.
64+
*
65+
* > Caso informar `http://` será aberto uma nova aba.
66+
* Caso informar um caminho relativo, exemplo: `/customers`, será aberto na aba atual.
67+
*
68+
*/
69+
@Input('p-url') url?: string;
70+
6271
/** Valor do conteúdo a ser exibido. */
6372
@Input('p-value') value?: string;
6473

projects/ui/src/lib/components/po-info/po-info.component.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
<div [ngClass]="labelSize && orientation === poInfoOrientation.Horizontal ? 'po-sm-' + (12 - labelSize) : ''"
1212
[class.po-info-container-content]="orientation !== poInfoOrientation.Horizontal"
1313
[class.po-info-value-horizontal]="orientation === poInfoOrientation.Horizontal">
14-
<span class="po-info-value">{{ value }}</span>
14+
15+
<ng-container *ngIf="url; then infoLink; else infoValue"></ng-container>
1516
</div>
1617
</div>
18+
19+
<ng-template #infoLink>
20+
<ng-container *ngIf="isExternalLink; then externalLink; else internalLink">
21+
</ng-container>
22+
</ng-template>
23+
24+
<ng-template #infoValue>
25+
<span class="po-info-value">{{ value }}</span>
26+
</ng-template>
27+
28+
<ng-template #externalLink>
29+
<a class="po-info-value po-info-link" [href]="url" target="_blank">{{ value }}</a>
30+
</ng-template>
31+
32+
<ng-template #internalLink>
33+
<a class="po-info-value po-info-link" [routerLink]="url">{{ value }}</a>
34+
</ng-template>

projects/ui/src/lib/components/po-info/po-info.component.spec.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { RouterModule } from '@angular/router';
23

4+
import * as UtilsFunctions from './../../utils/util';
35
import { configureTestSuite } from './../../util-test/util-expect.spec';
46

57
import { PoInfoBaseComponent } from './po-info-base.component';
@@ -14,6 +16,7 @@ describe('PoInfoComponent', () => {
1416

1517
configureTestSuite(() => {
1618
TestBed.configureTestingModule({
19+
imports: [ RouterModule.forRoot([]) ],
1720
declarations: [ PoInfoComponent ]
1821
});
1922
});
@@ -22,8 +25,6 @@ describe('PoInfoComponent', () => {
2225
fixture = TestBed.createComponent(PoInfoComponent);
2326
component = fixture.componentInstance;
2427

25-
fixture.detectChanges();
26-
2728
nativeElement = fixture.debugElement.nativeElement;
2829
});
2930

@@ -32,6 +33,28 @@ describe('PoInfoComponent', () => {
3233
expect(component instanceof PoInfoComponent).toBeTruthy();
3334
});
3435

36+
describe('Properties:', () => {
37+
38+
it('isExternalLink: should return true if `component.url` is truthy', () => {
39+
component.url = 'http://portinari.io';
40+
41+
const spyIsExternalLink = spyOn(UtilsFunctions, 'isExternalLink').and.callThrough();
42+
43+
expect(component.isExternalLink).toBe(true);
44+
expect(spyIsExternalLink).toHaveBeenCalled();
45+
});
46+
47+
it('isExternalLink: should return false if `component.url` is falsy', () => {
48+
component.url = '';
49+
50+
const spyIsExternalLink = spyOn(UtilsFunctions, 'isExternalLink').and.callThrough();
51+
52+
expect(component.isExternalLink).toBe(false);
53+
expect(spyIsExternalLink).toHaveBeenCalled();
54+
});
55+
56+
});
57+
3558
describe('Template:', () => {
3659
it('should only start with the default classes and elements, shouldn`t have variations', () => {
3760
component.label = 'Po Info';
@@ -159,6 +182,34 @@ describe('PoInfoComponent', () => {
159182
expect(nativeElement.querySelector('.po-text-nowrap')).toBeTruthy();
160183
});
161184

185+
it('should find `a.po-info-link` if `component.url` is truthy', () => {
186+
component.value = 'John Doe';
187+
component.url = 'http://portinari.io';
188+
189+
fixture.detectChanges();
190+
191+
expect(nativeElement.querySelector('a.po-info-link[target=_blank]')).toBeTruthy();
192+
});
193+
194+
it('shouldn`t find `a.po-info-link` if `component.url` is falsy', () => {
195+
component.value = 'John Doe';
196+
component.url = '';
197+
198+
fixture.detectChanges();
199+
200+
expect(nativeElement.querySelector('a.po-info-link')).toBeNull();
201+
});
202+
203+
it('should find `a.po-info-link` and not find `a.po-info-link[target=_blank]` if URL is an internal link ', () => {
204+
component.value = 'John Doe';
205+
component.url = '/customers';
206+
207+
fixture.detectChanges();
208+
209+
expect(nativeElement.querySelector('a.po-info-link[target=_blank]')).toBeNull();
210+
expect(nativeElement.querySelector('a.po-info-link')).toBeTruthy();
211+
});
212+
162213
});
163214

164215
});

projects/ui/src/lib/components/po-info/po-info.component.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
3+
import { isExternalLink } from '../../utils/util';
24

35
import { PoInfoBaseComponent } from './po-info-base.component';
46

@@ -24,6 +26,13 @@ import { PoInfoBaseComponent } from './po-info-base.component';
2426
*/
2527
@Component({
2628
selector: 'po-info',
27-
templateUrl: './po-info.component.html'
29+
templateUrl: './po-info.component.html',
30+
changeDetection: ChangeDetectionStrategy.OnPush
2831
})
29-
export class PoInfoComponent extends PoInfoBaseComponent { }
32+
export class PoInfoComponent extends PoInfoBaseComponent {
33+
34+
get isExternalLink() {
35+
return isExternalLink(this.url);
36+
}
37+
38+
}

projects/ui/src/lib/components/po-info/po-info.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33

44
import { PoInfoComponent } from './po-info.component';
5+
import { RouterModule } from '@angular/router';
56
/**
67
* @description
78
*
89
* Módulo do componente po-info.
910
*/
1011
@NgModule({
1112
imports: [
12-
CommonModule
13+
CommonModule,
14+
RouterModule
1315
],
1416
declarations: [
1517
PoInfoComponent

projects/ui/src/lib/components/po-info/samples/sample-po-info-labs/sample-po-info-labs.component.html

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
21
<po-info
32
[p-label]="label"
43
[p-label-size]="labelSize"
54
[p-orientation]="orientation"
5+
[p-url]="url"
66
[p-value]="value">
77
</po-info>
88

@@ -26,24 +26,34 @@
2626
p-clean
2727
p-label="Value">
2828
</po-input>
29+
</div>
2930

30-
<po-radio-group
31-
class="po-md-8"
32-
name="orientation"
33-
[(ngModel)]="orientation"
34-
p-label="Orientation"
35-
[p-options]="orientationOptions">
36-
</po-radio-group>
31+
<div class="po-row">
32+
<po-input
33+
class="po-lg-4 po-md-6"
34+
name="url"
35+
[(ngModel)]="url"
36+
p-clean
37+
p-label="Url">
38+
</po-input>
3739

3840
<po-number
39-
class="po-md-4"
41+
class="po-lg-2 po-md-6"
4042
name="labelSize"
4143
[(ngModel)]="labelSize"
4244
p-clean
4345
p-label="Label size"
4446
p-max="11"
4547
p-min="1">
4648
</po-number>
49+
50+
<po-radio-group
51+
class="po-lg-6 po-md-12"
52+
name="orientation"
53+
[(ngModel)]="orientation"
54+
p-label="Orientation"
55+
[p-options]="orientationOptions">
56+
</po-radio-group>
4757
</div>
4858

4959
<div class="po-row">

projects/ui/src/lib/components/po-info/samples/sample-po-info-labs/sample-po-info-labs.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class SamplePoInfoLabsComponent implements OnInit {
1111
label: string;
1212
labelSize: number;
1313
orientation: PoInfoOrientation;
14+
url: string;
1415
value: string;
1516

1617
public readonly orientationOptions: Array<PoRadioGroupOption> = [
@@ -26,6 +27,7 @@ export class SamplePoInfoLabsComponent implements OnInit {
2627
this.label = 'Portinari Info';
2728
this.labelSize = undefined;
2829
this.orientation = undefined;
30+
this.url = undefined;
2931
this.value = undefined;
3032
}
3133

0 commit comments

Comments
 (0)