Skip to content

Commit 6f4cc65

Browse files
committed
refactor(list): convert MatLineSetter into a function
Currently the `MatLineSetter` is a class, however it doesn't really do anything aside from calling a couple of functions inside the constructor. The following changes turn it into a function and make it a bit more compact. This also avoids having to turn off a tslint rule in a couple of places.
1 parent 88c6548 commit 6f4cc65

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

src/lib/core/line/line.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ElementRef,
1313
QueryList,
1414
} from '@angular/core';
15+
import {startWith} from 'rxjs/operators';
1516
import {MatCommonModule} from '../common-behaviors/common-module';
1617

1718

@@ -30,38 +31,38 @@ export class MatLine {}
3031
* Helper that takes a query list of lines and sets the correct class on the host.
3132
* @docs-private
3233
*/
33-
export class MatLineSetter {
34-
constructor(private _lines: QueryList<MatLine>, private _element: ElementRef<HTMLElement>) {
35-
this._setLineClass(this._lines.length);
36-
37-
this._lines.changes.subscribe(() => {
38-
this._setLineClass(this._lines.length);
39-
});
40-
}
34+
export function setLines(lines: QueryList<MatLine>, element: ElementRef<HTMLElement>) {
35+
// Note: doesn't need to unsubscribe, because `changes`
36+
// gets completed by Angular when the view is destroyed.
37+
lines.changes.pipe(startWith<QueryList<MatLine>>(lines)).subscribe(({length}) => {
38+
setClass(element, 'mat-2-line', false);
39+
setClass(element, 'mat-3-line', false);
40+
setClass(element, 'mat-multi-line', false);
4141

42-
private _setLineClass(count: number): void {
43-
this._resetClasses();
44-
if (count === 2 || count === 3) {
45-
this._setClass(`mat-${count}-line`, true);
46-
} else if (count > 3) {
47-
this._setClass(`mat-multi-line`, true);
42+
if (length === 2 || length === 3) {
43+
setClass(element, `mat-${length}-line`, true);
44+
} else if (length > 3) {
45+
setClass(element, `mat-multi-line`, true);
4846
}
49-
}
47+
});
48+
}
5049

51-
private _resetClasses(): void {
52-
this._setClass('mat-2-line', false);
53-
this._setClass('mat-3-line', false);
54-
this._setClass('mat-multi-line', false);
55-
}
50+
/** Adds or removes a class from an element. */
51+
function setClass(element: ElementRef<HTMLElement>, className: string, isAdd: boolean): void {
52+
const classList = element.nativeElement.classList;
53+
isAdd ? classList.add(className) : classList.remove(className);
54+
}
5655

57-
private _setClass(className: string, isAdd: boolean): void {
58-
if (isAdd) {
59-
this._element.nativeElement.classList.add(className);
60-
} else {
61-
this._element.nativeElement.classList.remove(className);
62-
}
56+
/**
57+
* Helper that takes a query list of lines and sets the correct class on the host.
58+
* @docs-private
59+
* @deprecated Use `setLines` instead.
60+
* @breaking-change 8.0.0
61+
*/
62+
export class MatLineSetter {
63+
constructor(lines: QueryList<MatLine>, element: ElementRef<HTMLElement>) {
64+
setLines(lines, element);
6365
}
64-
6566
}
6667

6768
@NgModule({

src/lib/grid-list/grid-tile.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
ChangeDetectionStrategy,
2020
Inject,
2121
} from '@angular/core';
22-
import {MatLine, MatLineSetter} from '@angular/material/core';
22+
import {MatLine, setLines} from '@angular/material/core';
2323
import {coerceNumberProperty} from '@angular/cdk/coercion';
2424
import {MAT_GRID_LIST, MatGridListBase} from './grid-list-base';
2525

@@ -70,17 +70,12 @@ export class MatGridTile {
7070
encapsulation: ViewEncapsulation.None,
7171
})
7272
export class MatGridTileText implements AfterContentInit {
73-
/**
74-
* Helper that watches the number of lines in a text area and sets
75-
* a class on the host element that matches the line count.
76-
*/
77-
_lineSetter: MatLineSetter;
7873
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
7974

8075
constructor(private _element: ElementRef<HTMLElement>) {}
8176

8277
ngAfterContentInit() {
83-
this._lineSetter = new MatLineSetter(this._lines, this._element);
78+
setLines(this._lines, this._element);
8479
}
8580
}
8681

src/lib/list/list.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
CanDisableRipple,
2323
CanDisableRippleCtor,
2424
MatLine,
25-
MatLineSetter,
25+
setLines,
2626
mixinDisableRipple,
2727
} from '@angular/material/core';
2828

@@ -136,9 +136,7 @@ export class MatListItem extends _MatListItemMixinBase implements AfterContentIn
136136
}
137137

138138
ngAfterContentInit() {
139-
// TODO: consider turning the setter into a function, it doesn't do anything as a class.
140-
// tslint:disable-next-line:no-unused-expression
141-
new MatLineSetter(this._lines, this._element);
139+
setLines(this._lines, this._element);
142140
}
143141

144142
/** Whether this list item should show a ripple effect when clicked. */

src/lib/list/selection-list.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
import {
3434
CanDisableRipple, CanDisableRippleCtor,
3535
MatLine,
36-
MatLineSetter,
36+
setLines,
3737
mixinDisableRipple,
3838
} from '@angular/material/core';
3939
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
@@ -161,9 +161,7 @@ export class MatListOption extends _MatListOptionMixinBase
161161
}
162162

163163
ngAfterContentInit() {
164-
// TODO: consider turning the setter into a function, it doesn't do anything as a class.
165-
// tslint:disable-next-line:no-unused-expression
166-
new MatLineSetter(this._lines, this._element);
164+
setLines(this._lines, this._element);
167165
}
168166

169167
ngOnDestroy(): void {

0 commit comments

Comments
 (0)