Skip to content

refactor(list): convert MatLineSetter into a function #13369

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

Merged
merged 1 commit into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 28 additions & 27 deletions src/lib/core/line/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ElementRef,
QueryList,
} from '@angular/core';
import {startWith} from 'rxjs/operators';
import {MatCommonModule} from '../common-behaviors/common-module';


Expand All @@ -30,38 +31,38 @@ export class MatLine {}
* Helper that takes a query list of lines and sets the correct class on the host.
* @docs-private
*/
export class MatLineSetter {
constructor(private _lines: QueryList<MatLine>, private _element: ElementRef<HTMLElement>) {
this._setLineClass(this._lines.length);

this._lines.changes.subscribe(() => {
this._setLineClass(this._lines.length);
});
}
export function setLines(lines: QueryList<MatLine>, element: ElementRef<HTMLElement>) {
// Note: doesn't need to unsubscribe, because `changes`
// gets completed by Angular when the view is destroyed.
lines.changes.pipe(startWith<QueryList<MatLine>>(lines)).subscribe(({length}) => {
setClass(element, 'mat-2-line', false);
setClass(element, 'mat-3-line', false);
setClass(element, 'mat-multi-line', false);

private _setLineClass(count: number): void {
this._resetClasses();
if (count === 2 || count === 3) {
this._setClass(`mat-${count}-line`, true);
} else if (count > 3) {
this._setClass(`mat-multi-line`, true);
if (length === 2 || length === 3) {
setClass(element, `mat-${length}-line`, true);
} else if (length > 3) {
setClass(element, `mat-multi-line`, true);
}
}
});
}

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

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

}

@NgModule({
Expand Down
9 changes: 2 additions & 7 deletions src/lib/grid-list/grid-tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ChangeDetectionStrategy,
Inject,
} from '@angular/core';
import {MatLine, MatLineSetter} from '@angular/material/core';
import {MatLine, setLines} from '@angular/material/core';
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {MAT_GRID_LIST, MatGridListBase} from './grid-list-base';

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

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

ngAfterContentInit() {
this._lineSetter = new MatLineSetter(this._lines, this._element);
setLines(this._lines, this._element);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/lib/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
CanDisableRipple,
CanDisableRippleCtor,
MatLine,
MatLineSetter,
setLines,
mixinDisableRipple,
} from '@angular/material/core';

Expand Down Expand Up @@ -136,9 +136,7 @@ export class MatListItem extends _MatListItemMixinBase implements AfterContentIn
}

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

/** Whether this list item should show a ripple effect when clicked. */
Expand Down
6 changes: 2 additions & 4 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import {
CanDisableRipple, CanDisableRippleCtor,
MatLine,
MatLineSetter,
setLines,
mixinDisableRipple,
} from '@angular/material/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
Expand Down Expand Up @@ -161,9 +161,7 @@ export class MatListOption extends _MatListOptionMixinBase
}

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

ngOnDestroy(): void {
Expand Down