From 4ba3bbf813fce4cc401401f987feff1a024eaabe Mon Sep 17 00:00:00 2001 From: Ivan Kuplevich Date: Wed, 29 Jul 2020 15:07:29 +0300 Subject: [PATCH 1/5] fix(icon-component): remove icon not found error and allow empty icon --- .../theme/components/icon/icon-libraries.ts | 10 +++++----- .../theme/components/icon/icon.component.ts | 16 +++++++++++++--- .../components/icon/icons-libraries.spec.ts | 12 ++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/framework/theme/components/icon/icon-libraries.ts b/src/framework/theme/components/icon/icon-libraries.ts index b1c24a1bfc..7c2eb2bcc2 100644 --- a/src/framework/theme/components/icon/icon-libraries.ts +++ b/src/framework/theme/components/icon/icon-libraries.ts @@ -23,10 +23,6 @@ function throwNoDefaultPackError() { throw Error('Default pack is not registered.'); } -function throwIconNotFoundError(name: string, pack: string) { - throw Error(`Icon '${name}' is not registered in pack '${pack}'. Check icon name or consider switching icon pack.`); -} - function throwWrongPackTypeError(name: string, type: string, desiredType: string) { throw Error(`Pack '${name}' is not an '${desiredType}' Pack and its type is '${type}'`); } @@ -105,6 +101,10 @@ export class NbIconLibraries { const icon = this.getIconFromPack(name, iconsPack); + if (!icon) { + return null; + } + return { name, pack: iconsPack.name, @@ -181,7 +181,7 @@ export class NbIconLibraries { protected getIconFromPack(name: string, pack: NbIconPack, shouldThrow = true): NbIcon | string { if (shouldThrow && !pack.icons.has(name)) { - throwIconNotFoundError(name, pack.name); + return null } return pack.icons.get(name); diff --git a/src/framework/theme/components/icon/icon.component.ts b/src/framework/theme/components/icon/icon.component.ts index 6fe222c6ae..c2d53b28dc 100644 --- a/src/framework/theme/components/icon/icon.component.ts +++ b/src/framework/theme/components/icon/icon.component.ts @@ -188,6 +188,7 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { get config(): string | NbIconConfig { return this._config; } + set config(value: string | NbIconConfig) { if (!value) { return; @@ -204,6 +205,7 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { this.options = value.options; } } + protected _config: string | NbIconConfig; constructor( @@ -218,14 +220,17 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { } ngOnChanges() { - if (this.iconDef) { - this.iconDef = this.renderIcon(this.icon, this.pack, this.options); - } + this.iconDef = this.renderIcon(this.icon, this.pack, this.options); } renderIcon(name: string, pack?: string, options?: { [name: string]: any }) { const iconDefinition = this.iconLibrary.getIcon(name, pack); + if (!iconDefinition) { + this.clearIcon(); + return; + } + const content = iconDefinition.icon.getContent(options); if (content) { this.html = this.sanitizer.bypassSecurityTrustHtml(content); @@ -235,6 +240,11 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { return iconDefinition; } + protected clearIcon(): void { + this.html = ''; + this.assignClasses([]); + } + protected assignClasses(classes: string[]) { this.prevClasses.forEach((className: string) => { this.renderer.removeClass(this.el.nativeElement, className); diff --git a/src/framework/theme/components/icon/icons-libraries.spec.ts b/src/framework/theme/components/icon/icons-libraries.spec.ts index bdba40e237..1e081aecf0 100644 --- a/src/framework/theme/components/icon/icons-libraries.spec.ts +++ b/src/framework/theme/components/icon/icons-libraries.spec.ts @@ -68,15 +68,13 @@ describe('icons-library', () => { expect(icon.type).toEqual('svg'); }); - it('should throw for unknown svg icon', () => { + it('should return null for unknown svg icon', () => { iconsLibrary.registerSvgPack('super-pack', { home: '', gear: '' }); iconsLibrary.setDefaultPack('super-pack'); - expect(() => iconsLibrary.getSvgIcon('unknown')) - // tslint:disable-next-line:max-line-length - .toThrowError(`Icon 'unknown' is not registered in pack 'super-pack'. Check icon name or consider switching icon pack.`); + expect(iconsLibrary.getSvgIcon('unknown')).toBeNull(); }); it('should throw for no default pack', () => { @@ -94,15 +92,13 @@ describe('icons-library', () => { .toThrowError(`Pack 'font-pack' is not an 'SVG' Pack and its type is 'font'`); }); - it('should throw for wrong pack', () => { + it('should return null for wrong pack', () => { iconsLibrary.registerSvgPack('super-pack', { home: '', gear: '' }); iconsLibrary.registerFontPack('font-pack'); iconsLibrary.setDefaultPack('super-pack'); - expect(() => iconsLibrary.getSvgIcon('unknown')) - // tslint:disable-next-line:max-line-length - .toThrowError(`Icon 'unknown' is not registered in pack 'super-pack'. Check icon name or consider switching icon pack.`); + expect(iconsLibrary.getSvgIcon('unknown')).toBeNull(); }); it('should throw for wrong pack when setting default', () => { From 9a5c56b6a4d228d8bba0c9a59efacc1b10c6a5ac Mon Sep 17 00:00:00 2001 From: Ivan Kuplevich Date: Wed, 29 Jul 2020 15:17:20 +0300 Subject: [PATCH 2/5] fix(icon-component): remove redundant spaces --- src/framework/theme/components/icon/icon.component.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/framework/theme/components/icon/icon.component.ts b/src/framework/theme/components/icon/icon.component.ts index c2d53b28dc..6d7818edb2 100644 --- a/src/framework/theme/components/icon/icon.component.ts +++ b/src/framework/theme/components/icon/icon.component.ts @@ -188,7 +188,6 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { get config(): string | NbIconConfig { return this._config; } - set config(value: string | NbIconConfig) { if (!value) { return; @@ -205,7 +204,6 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { this.options = value.options; } } - protected _config: string | NbIconConfig; constructor( From f604f8eef25da24b942e43c12d5dd36a4711b52c Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 30 Jul 2020 19:26:53 +0300 Subject: [PATCH 3/5] refactor(icon libraries): add null to return types --- src/framework/theme/components/icon/icon-libraries.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/icon/icon-libraries.ts b/src/framework/theme/components/icon/icon-libraries.ts index 7c2eb2bcc2..b9081711b3 100644 --- a/src/framework/theme/components/icon/icon-libraries.ts +++ b/src/framework/theme/components/icon/icon-libraries.ts @@ -92,7 +92,7 @@ export class NbIconLibraries { * * @returns NbIconDefinition */ - getSvgIcon(name: string, pack?: string): NbIconDefinition { + getSvgIcon(name: string, pack?: string): NbIconDefinition | null { const iconsPack = pack ? this.getPackOrThrow(pack) : this.getDefaultPackOrThrow(); if (iconsPack.type !== NbIconPackType.SVG) { @@ -144,7 +144,7 @@ export class NbIconLibraries { * * @returns NbIconDefinition */ - getIcon(name: string, pack?: string): NbIconDefinition { + getIcon(name: string, pack?: string): NbIconDefinition | null { const iconsPack = pack ? this.getPackOrThrow(pack) : this.getDefaultPackOrThrow(); if (iconsPack.type === NbIconPackType.SVG) { From df957625090a46d11f2c4eb787d123515305da5b Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 30 Jul 2020 19:28:41 +0300 Subject: [PATCH 4/5] refactor(icon libraries): remove unnecessary method argument --- src/framework/theme/components/icon/icon-libraries.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/framework/theme/components/icon/icon-libraries.ts b/src/framework/theme/components/icon/icon-libraries.ts index b9081711b3..7847bfa614 100644 --- a/src/framework/theme/components/icon/icon-libraries.ts +++ b/src/framework/theme/components/icon/icon-libraries.ts @@ -127,7 +127,7 @@ export class NbIconLibraries { throwWrongPackTypeError(iconsPack.name, iconsPack.type, 'Font'); } - const icon = this.getIconFromPack(name, iconsPack, false); + const icon = this.getIconFromPack(name, iconsPack); return { name, @@ -179,11 +179,11 @@ export class NbIconLibraries { return this.defaultPack; } - protected getIconFromPack(name: string, pack: NbIconPack, shouldThrow = true): NbIcon | string { - if (shouldThrow && !pack.icons.has(name)) { - return null + protected getIconFromPack(name: string, pack: NbIconPack): NbIcon | string | null { + if (pack.icons.has(name)) { + return pack.icons.get(name); } - return pack.icons.get(name); + return null; } } From d79b2dcdd9201d1b6803a3342859091ae94092b2 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 30 Jul 2020 19:46:44 +0300 Subject: [PATCH 5/5] refactor(icon): extract clear logic outside render method --- src/framework/theme/components/icon/icon.component.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/icon/icon.component.ts b/src/framework/theme/components/icon/icon.component.ts index 6d7818edb2..7234de6be2 100644 --- a/src/framework/theme/components/icon/icon.component.ts +++ b/src/framework/theme/components/icon/icon.component.ts @@ -218,14 +218,18 @@ export class NbIconComponent implements NbIconConfig, OnChanges, OnInit { } ngOnChanges() { - this.iconDef = this.renderIcon(this.icon, this.pack, this.options); + const iconDef = this.iconLibrary.getIcon(this.icon, this.pack); + if (iconDef) { + this.renderIcon(this.icon, this.pack, this.options); + } else { + this.clearIcon(); + } } renderIcon(name: string, pack?: string, options?: { [name: string]: any }) { const iconDefinition = this.iconLibrary.getIcon(name, pack); if (!iconDefinition) { - this.clearIcon(); return; }