Skip to content

Commit 86a4208

Browse files
crisbetojelbourn
authored andcommitted
refactor: remove QueryList workaround for type checking (#17789)
In earlier 9.0 RCs `QueryList` wasn't inferred as iterable and we were getting compiler errors. The way we worked around it was to convert it into an array. Now that the compiler handles `QueryList` correctly we don't need the workarounds anymore.
1 parent 81294f7 commit 86a4208

File tree

10 files changed

+11
-28
lines changed

10 files changed

+11
-28
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,6 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
261261
*/
262262
@ContentChildren(CdkStep, {descendants: true}) _steps: QueryList<CdkStep>;
263263

264-
/**
265-
* We need to store the steps in an Iterable due to strict template type checking with *ngFor and
266-
* https://github.com/angular/angular/issues/29842.
267-
*/
268-
_stepsArray: CdkStep[] = [];
269-
270264
/** The list of step components that the stepper is holding. */
271265
get steps(): QueryList<CdkStep> {
272266
return this._steps;

src/components-examples/cdk/stepper/cdk-custom-stepper-without-form/example-custom-stepper.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h2>Step {{ selectedIndex + 1 }}/{{ steps.length }}</h2>
1111
<button class="example-nav-button" cdkStepperPrevious>&larr;</button>
1212
<button
1313
class="example-step"
14-
*ngFor="let step of _stepsArray; let i = index"
14+
*ngFor="let step of steps; let i = index"
1515
[ngClass]="{ 'example-active': selectedIndex === i }"
1616
(click)="onClick(i)"
1717
>

src/material-experimental/mdc-tabs/tab-group.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
role="tab"
1010
matTabLabelWrapper
1111
cdkMonitorElementFocus
12-
*ngFor="let tab of _tabsArray; let i = index"
12+
*ngFor="let tab of _tabs; let i = index"
1313
[id]="_getTabLabelId(i)"
1414
[attr.tabIndex]="_getTabIndex(tab, i)"
1515
[attr.aria-posinset]="i + 1"
@@ -51,7 +51,7 @@
5151
[class._mat-animation-noopable]="_animationMode === 'NoopAnimations'"
5252
#tabBodyWrapper>
5353
<mat-tab-body role="tabpanel"
54-
*ngFor="let tab of _tabsArray; let i = index"
54+
*ngFor="let tab of _tabs; let i = index"
5555
[id]="_getTabContentId(i)"
5656
[attr.aria-labelledby]="_getTabLabelId(i)"
5757
[class.mat-mdc-tab-body-active]="selectedIndex == i"

src/material/stepper/stepper-horizontal.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="mat-horizontal-stepper-header-container">
2-
<ng-container *ngFor="let step of _stepsArray; let i = index; let isLast = last">
2+
<ng-container *ngFor="let step of steps; let i = index; let isLast = last">
33
<mat-step-header class="mat-horizontal-stepper-header"
44
(click)="step.select()"
55
(keydown)="_onKeydown($event)"
@@ -26,7 +26,7 @@
2626
</div>
2727

2828
<div class="mat-horizontal-content-container">
29-
<div *ngFor="let step of _stepsArray; let i = index"
29+
<div *ngFor="let step of steps; let i = index"
3030
[attr.tabindex]="selectedIndex === i ? 0 : null"
3131
class="mat-horizontal-stepper-content" role="tabpanel"
3232
[@stepTransition]="_getAnimationDirection(i)"

src/material/stepper/stepper-vertical.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="mat-step" *ngFor="let step of _stepsArray; let i = index; let isLast = last">
1+
<div class="mat-step" *ngFor="let step of steps; let i = index; let isLast = last">
22
<mat-step-header class="mat-vertical-stepper-header"
33
(click)="step.select()"
44
(keydown)="_onKeydown($event)"

src/material/stepper/stepper.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ export class MatStepper extends CdkStepper implements AfterContentInit {
112112
_animationDone = new Subject<AnimationEvent>();
113113

114114
ngAfterContentInit() {
115-
this._stepsArray = this.steps.toArray();
116115
this._icons.forEach(({name, templateRef}) => this._iconOverrides[name] = templateRef);
117116

118117
// Mark the component for change detection whenever the content children query changes
119118
this._steps.changes.pipe(takeUntil(this._destroyed)).subscribe(() => {
120-
this._stepsArray = this.steps.toArray();
121119
this._stateChanged();
122120
});
123121

src/material/tabs/tab-group.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(indexFocused)="_focusChanged($event)"
66
(selectFocusedIndex)="selectedIndex = $event">
77
<div class="mat-tab-label" role="tab" matTabLabelWrapper mat-ripple cdkMonitorElementFocus
8-
*ngFor="let tab of _tabsArray; let i = index"
8+
*ngFor="let tab of _tabs; let i = index"
99
[id]="_getTabLabelId(i)"
1010
[attr.tabIndex]="_getTabIndex(tab, i)"
1111
[attr.aria-posinset]="i + 1"
@@ -37,7 +37,7 @@
3737
[class._mat-animation-noopable]="_animationMode === 'NoopAnimations'"
3838
#tabBodyWrapper>
3939
<mat-tab-body role="tabpanel"
40-
*ngFor="let tab of _tabsArray; let i = index"
40+
*ngFor="let tab of _tabs; let i = index"
4141
[id]="_getTabContentId(i)"
4242
[attr.aria-labelledby]="_getTabLabelId(i)"
4343
[class.mat-tab-body-active]="selectedIndex == i"

src/material/tabs/tab-group.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ export abstract class _MatTabGroupBase extends _MatTabGroupMixinBase implements
8989
/** All of the tabs that belong to the group. */
9090
_tabs: QueryList<MatTab> = new QueryList<MatTab>();
9191

92-
/**
93-
* We need to store the tabs in an Iterable due to strict template type checking with *ngFor and
94-
* https://github.com/angular/angular/issues/29842.
95-
*/
96-
_tabsArray: MatTab[] = [];
97-
9892
/** The tab index that should be selected after the content has been checked. */
9993
private _indexToSelect: number | null = 0;
10094

@@ -234,20 +228,19 @@ export abstract class _MatTabGroupBase extends _MatTabGroupMixinBase implements
234228
ngAfterContentInit() {
235229
this._subscribeToAllTabChanges();
236230
this._subscribeToTabLabels();
237-
this._tabsArray = this._tabs.toArray();
238231

239232
// Subscribe to changes in the amount of tabs, in order to be
240233
// able to re-render the content as new tabs are added or removed.
241234
this._tabsSubscription = this._tabs.changes.subscribe(() => {
242235
const indexToSelect = this._clampTabIndex(this._indexToSelect);
243-
this._tabsArray = this._tabs.toArray();
244236

245237
// Maintain the previously-selected tab if a new tab is added or removed and there is no
246238
// explicit change that selects a different tab.
247239
if (indexToSelect === this._selectedIndex) {
240+
const tabs = this._tabs.toArray();
248241

249-
for (let i = 0; i < this._tabsArray.length; i++) {
250-
if (this._tabsArray[i].isActive) {
242+
for (let i = 0; i < tabs.length; i++) {
243+
if (tabs[i].isActive) {
251244
// Assign both to the `_indexToSelect` and `_selectedIndex` so we don't fire a changed
252245
// event, otherwise the consumer may end up in an infinite loop in some edge cases like
253246
// adding a tab within the `selectedIndexChange` event.

tools/public_api_guard/cdk/stepper.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export declare class CdkStepper implements AfterViewInit, OnDestroy {
4848
protected _orientation: StepperOrientation;
4949
_stepHeader: QueryList<FocusableOption>;
5050
_steps: QueryList<CdkStep>;
51-
_stepsArray: CdkStep[];
5251
linear: boolean;
5352
selected: CdkStep;
5453
selectedIndex: number;

tools/public_api_guard/material/tabs.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export declare abstract class _MatTabGroupBase extends _MatTabGroupMixinBase imp
3636
abstract _tabBodyWrapper: ElementRef;
3737
abstract _tabHeader: MatTabGroupBaseHeader;
3838
_tabs: QueryList<MatTab>;
39-
_tabsArray: MatTab[];
4039
readonly animationDone: EventEmitter<void>;
4140
animationDuration: string;
4241
backgroundColor: ThemePalette;

0 commit comments

Comments
 (0)