-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14c7092
commit ae5b948
Showing
4 changed files
with
67 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,7 +1,36 @@ | ||
# zu-icon | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
This library provides a helper component for the [zupit-fantasticon](https://www.npmjs.com/package/zupit-fantasticon) package. It allows you to easily use the icons generated by `zupit-fantasticon` in your Angular application. | ||
|
||
## Usage | ||
|
||
Once installed `zupit-fantasticon` and generated the icons, you can use the `ZuIconComponent` to display the icons in your Angular application. | ||
First you will need to import the `ZuIconModule` and to provide the `CUSTOM_ICONS_ENUM` injection token with the enum generated by `zupit-fantasticon`: | ||
|
||
|
||
`app.module.ts`: | ||
```typescript | ||
import {Icons} from "../path-to-your-icons-enum"; | ||
import {CUSTOM_ICONS_ENUM, ZuIconModule} from "@zupit-it/zu-icon"; | ||
|
||
@NgModule({ | ||
imports: [ | ||
ZuIconModule, | ||
], | ||
providers: [ | ||
{provide: CUSTOM_ICONS_ENUM, useValue: Icons}, | ||
] | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
Then you can use the `zu-icon` component in your templates: | ||
|
||
```html | ||
<zu-icon [icon]="Icons.MyIcon"></zu-icon> | ||
``` | ||
|
||
If you don't provide a valid icon (i.e. an icon that is not in the enum), the component will not be displayed and an error will be thrown in the console. | ||
|
||
|
||
## Running unit tests | ||
|
||
Run `nx test zu-icon` to execute the unit tests. |
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
37 changes: 30 additions & 7 deletions
37
packages/zu-icon/src/lib/components/zupit-icon.component.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 |
---|---|---|
@@ -1,18 +1,41 @@ | ||
import {Component, Inject, InjectionToken, Input} from '@angular/core'; | ||
|
||
export const ICONS_TOKEN = new InjectionToken<object>('ICONS_TOKEN'); | ||
export interface IconsEnum { | ||
[key: string]: string; | ||
} | ||
|
||
export const CUSTOM_ICONS_ENUM = new InjectionToken<IconsEnum>('CUSTOM_ICONS_ENUM'); | ||
|
||
@Component({ | ||
selector: 'zu-icon', | ||
templateUrl: './zupit-icon.component.html', | ||
styleUrls: ['./zupit-icon.component.scss'] | ||
styleUrls: ['./zupit-icon.component.scss'], | ||
}) | ||
export class ZupitIconComponent<T extends object> { | ||
@Input() icon?: T; | ||
export class ZupitIconComponent { | ||
@Input() set icon(value: string) { | ||
if (!this.validateIcon(value)) { | ||
throw new Error(`Invalid icon '${value}' provided.`); | ||
} | ||
this._icon = value; | ||
} | ||
|
||
get icon(): string { | ||
return this._icon; | ||
} | ||
|
||
private _icon: string; | ||
|
||
private validateIcon(icon: string): boolean { | ||
return Object.values(this.icons).includes(icon); | ||
} | ||
|
||
constructor(@Inject(CUSTOM_ICONS_ENUM) public icons: IconsEnum) { | ||
if (!icons) { | ||
throw new Error('CUSTOM_ICONS_ENUM not provided. Please provide the Icons enum.'); | ||
} | ||
|
||
constructor(@Inject(ICONS_TOKEN) private icons: T) { | ||
if (!icons || typeof icons !== 'object') { | ||
throw new Error('ICONS_TOKEN not provided. Please provide the Icons enum.'); | ||
if(typeof icons !== "object") { | ||
throw new Error('CUSTOM_ICONS_ENUM not valid. Please provide a valid Icons enum.'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { NgModule } from '@angular/core' | ||
import { CommonModule } from '@angular/common' | ||
import {NgModule} from '@angular/core' | ||
import {CommonModule} from '@angular/common' | ||
import {ZupitIconComponent} from "./components/zupit-icon.component"; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
declarations: [ZupitIconComponent], | ||
exports: [ZupitIconComponent] | ||
}) | ||
export class ZuIconModule {} |