@@ -19,10 +19,8 @@ import {
19
19
Inject ,
20
20
InjectionToken ,
21
21
Input ,
22
- OnChanges ,
23
22
OnDestroy ,
24
23
OnInit ,
25
- SimpleChanges ,
26
24
ViewEncapsulation ,
27
25
} from '@angular/core' ;
28
26
import { CanColor , CanColorCtor , mixinColor } from '@angular/material/core' ;
@@ -137,8 +135,8 @@ const funcIriPattern = /^url\(['"]?#(.*?)['"]?\)$/;
137
135
encapsulation : ViewEncapsulation . None ,
138
136
changeDetection : ChangeDetectionStrategy . OnPush ,
139
137
} )
140
- export class MatIcon extends _MatIconMixinBase implements OnChanges , OnInit , AfterViewChecked ,
141
- CanColor , OnDestroy {
138
+ export class MatIcon extends _MatIconMixinBase implements OnInit , AfterViewChecked , CanColor ,
139
+ OnDestroy {
142
140
143
141
/**
144
142
* Whether the icon should be inlined, automatically sizing the icon to match the font size of
@@ -154,21 +152,43 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
154
152
private _inline : boolean = false ;
155
153
156
154
/** Name of the icon in the SVG icon set. */
157
- @Input ( ) svgIcon : string ;
155
+ @Input ( )
156
+ get svgIcon ( ) : string { return this . _svgIcon ; }
157
+ set svgIcon ( value : string ) {
158
+ if ( value !== this . _svgIcon ) {
159
+ if ( value ) {
160
+ this . _updateSvgIcon ( value ) ;
161
+ } else if ( this . _svgIcon ) {
162
+ this . _clearSvgElement ( ) ;
163
+ }
164
+ this . _svgIcon = value ;
165
+ }
166
+ }
167
+ private _svgIcon : string ;
158
168
159
169
/** Font set that the icon is a part of. */
160
170
@Input ( )
161
171
get fontSet ( ) : string { return this . _fontSet ; }
162
172
set fontSet ( value : string ) {
163
- this . _fontSet = this . _cleanupFontValue ( value ) ;
173
+ const newValue = this . _cleanupFontValue ( value ) ;
174
+
175
+ if ( newValue !== this . _fontSet ) {
176
+ this . _fontSet = newValue ;
177
+ this . _updateFontIconClasses ( ) ;
178
+ }
164
179
}
165
180
private _fontSet : string ;
166
181
167
182
/** Name of an icon within a font set. */
168
183
@Input ( )
169
184
get fontIcon ( ) : string { return this . _fontIcon ; }
170
185
set fontIcon ( value : string ) {
171
- this . _fontIcon = this . _cleanupFontValue ( value ) ;
186
+ const newValue = this . _cleanupFontValue ( value ) ;
187
+
188
+ if ( newValue !== this . _fontIcon ) {
189
+ this . _fontIcon = newValue ;
190
+ this . _updateFontIconClasses ( ) ;
191
+ }
172
192
}
173
193
private _fontIcon : string ;
174
194
@@ -226,49 +246,10 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
226
246
}
227
247
}
228
248
229
- ngOnChanges ( changes : SimpleChanges ) {
230
- // Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
231
- const svgIconChanges = changes [ 'svgIcon' ] ;
232
-
233
- this . _svgNamespace = null ;
234
- this . _svgName = null ;
235
-
236
- if ( svgIconChanges ) {
237
- this . _currentIconFetch . unsubscribe ( ) ;
238
-
239
- if ( this . svgIcon ) {
240
- const [ namespace , iconName ] = this . _splitIconName ( this . svgIcon ) ;
241
-
242
- if ( namespace ) {
243
- this . _svgNamespace = namespace ;
244
- }
245
-
246
- if ( iconName ) {
247
- this . _svgName = iconName ;
248
- }
249
-
250
- this . _currentIconFetch = this . _iconRegistry . getNamedSvgIcon ( iconName , namespace )
251
- . pipe ( take ( 1 ) )
252
- . subscribe ( svg => this . _setSvgElement ( svg ) , ( err : Error ) => {
253
- const errorMessage = `Error retrieving icon ${ namespace } :${ iconName } ! ${ err . message } ` ;
254
- this . _errorHandler . handleError ( new Error ( errorMessage ) ) ;
255
- } ) ;
256
- } else if ( svgIconChanges . previousValue ) {
257
- this . _clearSvgElement ( ) ;
258
- }
259
- }
260
-
261
- if ( this . _usingFontIcon ( ) ) {
262
- this . _updateFontIconClasses ( ) ;
263
- }
264
- }
265
-
266
249
ngOnInit ( ) {
267
250
// Update font classes because ngOnChanges won't be called if none of the inputs are present,
268
251
// e.g. <mat-icon>arrow</mat-icon> In this case we need to add a CSS class for the default font.
269
- if ( this . _usingFontIcon ( ) ) {
270
- this . _updateFontIconClasses ( ) ;
271
- }
252
+ this . _updateFontIconClasses ( ) ;
272
253
}
273
254
274
255
ngAfterViewChecked ( ) {
@@ -430,5 +411,31 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
430
411
}
431
412
}
432
413
414
+ /** Sets a new SVG icon with a particular name. */
415
+ private _updateSvgIcon ( rawName : string | undefined ) {
416
+ this . _svgNamespace = null ;
417
+ this . _svgName = null ;
418
+ this . _currentIconFetch . unsubscribe ( ) ;
419
+
420
+ if ( rawName ) {
421
+ const [ namespace , iconName ] = this . _splitIconName ( rawName ) ;
422
+
423
+ if ( namespace ) {
424
+ this . _svgNamespace = namespace ;
425
+ }
426
+
427
+ if ( iconName ) {
428
+ this . _svgName = iconName ;
429
+ }
430
+
431
+ this . _currentIconFetch = this . _iconRegistry . getNamedSvgIcon ( iconName , namespace )
432
+ . pipe ( take ( 1 ) )
433
+ . subscribe ( svg => this . _setSvgElement ( svg ) , ( err : Error ) => {
434
+ const errorMessage = `Error retrieving icon ${ namespace } :${ iconName } ! ${ err . message } ` ;
435
+ this . _errorHandler . handleError ( new Error ( errorMessage ) ) ;
436
+ } ) ;
437
+ }
438
+ }
439
+
433
440
static ngAcceptInputType_inline : BooleanInput ;
434
441
}
0 commit comments