-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(icon): new component and example usage in menu
- Loading branch information
1 parent
c42b73b
commit f504079
Showing
6 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { Icon, PIconComponent, P_ICON_COMPONENT } from './icon'; | ||
|
||
describe('Icon', () => { | ||
let fixture: ComponentFixture<Icon>; | ||
|
||
beforeEach(() => TestBed.configureTestingModule({ imports: [Icon] })); | ||
|
||
it('should display default template if no override has been defined', () => { | ||
fixture = TestBed.createComponent(Icon); | ||
fixture.componentInstance.icon = 'pi pi-check'; | ||
fixture.componentInstance.iconStyle = { color: 'red' }; | ||
|
||
fixture.detectChanges(); | ||
const iconEl = fixture.debugElement.query(By.css('i')); | ||
expect(iconEl).toBeTruthy(); | ||
}); | ||
|
||
it('should display custom component if override has been defined', () => { | ||
@Component({ | ||
standalone: true, | ||
selector: 'custom-icon', | ||
template: ` Hello world! ` | ||
}) | ||
class CustomIconComponent implements PIconComponent { | ||
@Input() icon: string; | ||
@Input() iconStyle?: any; | ||
@Input() iconClass?: any; | ||
} | ||
|
||
TestBed.overrideProvider(P_ICON_COMPONENT, { useValue: CustomIconComponent }); | ||
fixture = TestBed.createComponent(Icon); | ||
fixture.componentInstance.icon = 'pi pi-check'; | ||
fixture.componentInstance.iconStyle = { color: 'red' }; | ||
|
||
fixture.detectChanges(); | ||
const customIconEl = fixture.debugElement.query(By.css('custom-icon')); | ||
expect(customIconEl).toBeTruthy(); | ||
|
||
const customIconComponent = customIconEl.componentInstance as CustomIconComponent; | ||
expect(customIconComponent.icon).toBe('pi pi-check'); | ||
expect(customIconComponent.iconStyle).toEqual({ color: 'red' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NgClass, NgComponentOutlet, NgStyle } from '@angular/common'; | ||
import { ChangeDetectionStrategy, Component, InjectionToken, Input, Type, inject } from '@angular/core'; | ||
|
||
export interface PIconComponent { | ||
icon: string; | ||
iconStyle?: NgStyle['ngStyle']; | ||
} | ||
|
||
export const P_ICON_COMPONENT = new InjectionToken<Type<PIconComponent>>('P_ICON_COMPONENT'); | ||
|
||
/** | ||
* Icon is a simple component to render an icon. | ||
* @group Components | ||
*/ | ||
@Component({ | ||
standalone: true, | ||
imports: [NgClass, NgStyle, NgComponentOutlet], | ||
selector: 'p-icon', | ||
template: ` | ||
@if(!iconComponentClass) { | ||
<i [ngClass]="icon" [ngStyle]="iconStyle"></i> | ||
} @else { | ||
<ng-container *ngComponentOutlet="iconComponentClass; inputs: { icon: icon, iconStyle: iconStyle }" /> | ||
} | ||
`, | ||
styleUrls: ['./icon.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
host: { | ||
class: 'p-element' | ||
// style: 'display: contents' | ||
} | ||
}) | ||
export class Icon implements PIconComponent { | ||
@Input({ required: true }) icon: string; | ||
@Input() iconStyle?: NgStyle['ngStyle']; | ||
|
||
protected readonly iconComponentClass = inject(P_ICON_COMPONENT, { optional: true }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "public_api.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './icon'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters