22 AfterViewInit ,
33 Directive ,
44 ElementRef ,
5- Input
5+ Input ,
6+ OnChanges ,
7+ SimpleChanges
68} from "@angular/core" ;
79import { IconService } from "./icon.service" ;
810import { getAttributes } from "@carbon/icon-helpers" ;
@@ -21,7 +23,7 @@ import { getAttributes } from "@carbon/icon-helpers";
2123@Directive ( {
2224 selector : "[cdsIcon], [ibmIcon]"
2325} )
24- export class IconDirective implements AfterViewInit {
26+ export class IconDirective implements AfterViewInit , OnChanges {
2527
2628 /**
2729 * @deprecated since v5 - Use `cdsIcon` input property instead
@@ -30,12 +32,10 @@ export class IconDirective implements AfterViewInit {
3032 this . cdsIcon = iconName ;
3133 }
3234
33- @Input ( ) set cdsIcon ( iconName : string ) {
34- this . iconName = iconName ;
35- this . renderIcon ( iconName ) ;
36- }
3735 static titleIdCounter = 0 ;
3836
37+ @Input ( ) cdsIcon = "" ;
38+
3939 @Input ( ) size = "16" ;
4040
4141 @Input ( ) title = "" ;
@@ -48,12 +48,10 @@ export class IconDirective implements AfterViewInit {
4848
4949 @Input ( ) isFocusable = false ;
5050
51- private iconName : string ;
52-
5351 constructor (
5452 protected elementRef : ElementRef ,
5553 protected iconService : IconService
56- ) { }
54+ ) { }
5755
5856 renderIcon ( iconName : string ) {
5957 const root = this . elementRef . nativeElement as HTMLElement ;
@@ -122,6 +120,14 @@ export class IconDirective implements AfterViewInit {
122120 }
123121
124122 ngAfterViewInit ( ) {
125- this . renderIcon ( this . iconName ) ;
123+ this . renderIcon ( this . cdsIcon ) ;
124+ }
125+
126+ ngOnChanges ( { cdsIcon } : SimpleChanges ) {
127+ // We want to ignore first change to let the icon register
128+ // and add only after view has been initialized
129+ if ( ! cdsIcon . isFirstChange ( ) ) {
130+ this . renderIcon ( this . cdsIcon ) ;
131+ }
126132 }
127133}
0 commit comments