Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(nb-icon): return null if icon wasn't found #2466

Merged
merged 6 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/framework/theme/components/icon/icon-libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`);
}
Expand Down Expand Up @@ -105,6 +101,10 @@ export class NbIconLibraries {

const icon = this.getIconFromPack(name, iconsPack);

if (!icon) {
return null;
mmfKupl marked this conversation as resolved.
Show resolved Hide resolved
}

return {
name,
pack: iconsPack.name,
Expand Down Expand Up @@ -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);
mmfKupl marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
14 changes: 11 additions & 3 deletions src/framework/theme/components/icon/icon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,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);
yggg marked this conversation as resolved.
Show resolved Hide resolved
}

renderIcon(name: string, pack?: string, options?: { [name: string]: any }) {
const iconDefinition = this.iconLibrary.getIcon(name, pack);

if (!iconDefinition) {
this.clearIcon();
return;
}
yggg marked this conversation as resolved.
Show resolved Hide resolved

const content = iconDefinition.icon.getContent(options);
if (content) {
this.html = this.sanitizer.bypassSecurityTrustHtml(content);
Expand All @@ -235,6 +238,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);
Expand Down
12 changes: 4 additions & 8 deletions src/framework/theme/components/icon/icons-libraries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<svg><rect></rect></svg>', gear: '<svg></svg>' });
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', () => {
Expand All @@ -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: '<svg><rect></rect></svg>', gear: '<svg></svg>' });
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', () => {
Expand Down