Skip to content

Commit 7b1d4f4

Browse files
committed
refactor so only interactive lists set up the foundation
1 parent 1f4c3ab commit 7b1d4f4

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
10-
import {MatListBase} from './list-base';
10+
import {MatInteractiveListBase, MatListBase} from './list-base';
1111

1212
@Component({
1313
selector: 'mat-action-list',
@@ -23,4 +23,4 @@ import {MatListBase} from './list-base';
2323
{provide: MatListBase, useExisting: MatActionList},
2424
]
2525
})
26-
export class MatActionList extends MatListBase {}
26+
export class MatActionList extends MatInteractiveListBase {}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
6969
this._monitorLines();
7070
}
7171

72+
ngOnDestroy() {
73+
this._subscriptions.unsubscribe();
74+
this._rippleRenderer._removeTriggerEvents();
75+
}
76+
77+
_setTabIndexForChildren(value: number) {
78+
this._element.querySelectorAll('a, button').forEach(el => (el as HTMLElement).tabIndex = value);
79+
}
80+
7281
/**
7382
* Subscribes to changes in `MatLine` content children and annotates them appropriately when they
7483
* change.
@@ -88,19 +97,18 @@ export abstract class MatListItemBase implements AfterContentInit, OnDestroy, Ri
8897
}));
8998
});
9099
}
91-
92-
ngOnDestroy() {
93-
this._subscriptions.unsubscribe();
94-
this._rippleRenderer._removeTriggerEvents();
95-
}
96100
}
97101

98102
@Directive()
99103
/** @docs-private */
100-
export abstract class MatListBase implements AfterViewInit, OnDestroy {
104+
export abstract class MatListBase {
101105
@HostBinding('class.mdc-list--non-interactive')
102-
_isNonInteractive: boolean = false;
106+
_isNonInteractive: boolean = true;
107+
}
103108

109+
@Directive()
110+
export abstract class MatInteractiveListBase extends MatListBase
111+
implements AfterViewInit, OnDestroy {
104112
@HostListener('keydown', ['$event'])
105113
_handleKeydown(event: KeyboardEvent) {
106114
const index = this._indexForElement(event.target as HTMLElement);
@@ -138,7 +146,7 @@ export abstract class MatListBase implements AfterViewInit, OnDestroy {
138146
setAttributeForElementIndex:
139147
(index, attr, value) => this._itemAtIndex(index)._element.setAttribute(attr, value),
140148
setTabIndexForListItemChildren:
141-
(index, value) => this._itemAtIndex(index)._element.tabIndex = value as unknown as number,
149+
(index, value) => this._itemAtIndex(index)._setTabIndexForChildren(Number(value)),
142150
getFocusedElementIndex: () => this._indexForElement(this._document?.activeELement),
143151
isFocusInsideList: () => this._element.nativeElement.contains(this._document?.activeElement),
144152
isRootFocused: () => this._element.nativeElement === this._document?.activeElement,
@@ -151,21 +159,27 @@ export abstract class MatListBase implements AfterViewInit, OnDestroy {
151159
hasRadioAtIndex: () => false,
152160
setCheckedCheckboxOrRadioAtIndex: () => {},
153161
isCheckboxCheckedAtIndex: () => false,
154-
notifyAction: () => {},
155162

156-
// TODO(mmalerba): Determine if we need to implement this.
163+
// TODO(mmalerba): Determine if we need to implement these.
157164
getPrimaryTextAtIndex: () => '',
165+
notifyAction: () => {},
158166
};
159167

160168
protected _foundation: MDCListFoundation;
161169

162170
constructor(protected _element: ElementRef<HTMLElement>,
163-
@Inject(DOCUMENT) protected _document: any) {
171+
@Inject(DOCUMENT) protected _document: any) {
172+
super();
173+
this._isNonInteractive = false;
164174
this._foundation = new MDCListFoundation(this._adapter);
165175
}
166176

167177
ngAfterViewInit() {
168178
this._foundation.init();
179+
const first = this._items.toArray()[0]?._element;
180+
if (first) {
181+
first.tabIndex = 0;
182+
}
169183
this._foundation.layout();
170184
}
171185

@@ -181,3 +195,4 @@ export abstract class MatListBase implements AfterViewInit, OnDestroy {
181195
return element ? this._items.toArray().findIndex(i => i._element.contains(element)) : -1;
182196
}
183197
}
198+

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ export class MatListSubheaderCssMatStyler {}
6666
{provide: MatListBase, useExisting: MatList},
6767
]
6868
})
69-
export class MatList extends MatListBase {
70-
_isNonInteractive = true;
71-
}
69+
export class MatList extends MatListBase {}
7270

7371
@Component({
7472
selector: 'mat-list-item, a[mat-list-item], button[mat-list-item]',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
1010
import {MatList} from './list';
11-
import {MatListBase} from './list-base';
11+
import {MatInteractiveListBase, MatListBase} from './list-base';
1212

1313
@Component({
1414
selector: 'mat-nav-list',
@@ -33,4 +33,4 @@ import {MatListBase} from './list-base';
3333
{provide: MatList, useExisting: MatNavList},
3434
]
3535
})
36-
export class MatNavList extends MatListBase {}
36+
export class MatNavList extends MatInteractiveListBase {}

0 commit comments

Comments
 (0)