Skip to content

Commit 287a588

Browse files
committed
move logic out of lifecycle hooks
1 parent e40e868 commit 287a588

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/material-experimental/mdc-list/list-base.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
4949
private _rippleRenderer: RippleRenderer;
5050

5151
protected constructor(public _elementRef: ElementRef<HTMLElement>, protected _ngZone: NgZone,
52-
listBase: MatListBase, platform: Platform) {
53-
this.rippleDisabled = listBase._isNonInteractive;
54-
if (!listBase._isNonInteractive) {
55-
this._elementRef.nativeElement.classList.add('mat-mdc-list-item-interactive');
56-
}
57-
this._rippleRenderer =
58-
new RippleRenderer(this, this._ngZone, this._elementRef.nativeElement, platform);
59-
this._rippleRenderer.setupTriggerEvents(this._elementRef.nativeElement);
52+
private _listBase: MatListBase, private _platform: Platform) {
53+
this._initRipple();
6054
}
6155

6256
ngAfterContentInit() {
@@ -68,6 +62,23 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
6862
this._rippleRenderer._removeTriggerEvents();
6963
}
7064

65+
_initDefaultTabIndex(tabIndex: number) {
66+
const el = this._elementRef.nativeElement;
67+
if (!el.hasAttribute('tabIndex')) {
68+
el.tabIndex = tabIndex;
69+
}
70+
}
71+
72+
private _initRipple() {
73+
this.rippleDisabled = this._listBase._isNonInteractive;
74+
if (!this._listBase._isNonInteractive) {
75+
this._elementRef.nativeElement.classList.add('mat-mdc-list-item-interactive');
76+
}
77+
this._rippleRenderer =
78+
new RippleRenderer(this, this._ngZone, this._elementRef.nativeElement, this._platform);
79+
this._rippleRenderer.setupTriggerEvents(this._elementRef.nativeElement);
80+
}
81+
7182
/**
7283
* Subscribes to changes in `MatLine` content children and annotates them appropriately when they
7384
* change.
@@ -177,13 +188,8 @@ export abstract class MatInteractiveListBase extends MatListBase
177188
}
178189

179190
ngAfterViewInit() {
180-
this._subscriptions.add(
181-
this._items.changes.pipe(startWith(null))
182-
.subscribe(() => this._itemsArr = this._items.toArray()));
191+
this._initItems();
183192
this._foundation.init();
184-
for (let i = 0; this._items.length; i++) {
185-
this._elementAtIndex(i).tabIndex = i === 0 ? 0 : -1;
186-
}
187193
this._foundation.layout();
188194
}
189195

@@ -192,6 +198,15 @@ export abstract class MatInteractiveListBase extends MatListBase
192198
this._subscriptions.unsubscribe();
193199
}
194200

201+
private _initItems() {
202+
this._subscriptions.add(
203+
this._items.changes.pipe(startWith(null))
204+
.subscribe(() => this._itemsArr = this._items.toArray()));
205+
for (let i = 0; this._itemsArr.length; i++) {
206+
this._itemsArr[i]._initDefaultTabIndex(i === 0 ? 0 : -1);
207+
}
208+
}
209+
195210
private _itemAtIndex(index: number): MatListItemBase {
196211
return this._itemsArr[index];
197212
}

0 commit comments

Comments
 (0)