Skip to content

Commit ecd2cbd

Browse files
committed
refactor(cdk/stepper): switch to input transforms
Switches inputs in cdk/stepper to use transforms instead of getters/setters.
1 parent 5cd2509 commit ecd2cbd

File tree

2 files changed

+38
-58
lines changed

2 files changed

+38
-58
lines changed

src/cdk/stepper/stepper.ts

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
1010
import {Direction, Directionality} from '@angular/cdk/bidi';
11-
import {
12-
BooleanInput,
13-
coerceBooleanProperty,
14-
coerceNumberProperty,
15-
NumberInput,
16-
} from '@angular/cdk/coercion';
1711
import {ENTER, hasModifierKey, SPACE} from '@angular/cdk/keycodes';
1812
import {
1913
AfterViewInit,
@@ -38,6 +32,8 @@ import {
3832
ViewChild,
3933
ViewEncapsulation,
4034
AfterContentInit,
35+
booleanAttribute,
36+
numberAttribute,
4137
} from '@angular/core';
4238
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
4339
import {Observable, of as observableOf, Subject} from 'rxjs';
@@ -149,32 +145,18 @@ export class CdkStep implements OnChanges {
149145
@Input() state: StepState;
150146

151147
/** Whether the user can return to this step once it has been marked as completed. */
152-
@Input()
153-
get editable(): boolean {
154-
return this._editable;
155-
}
156-
set editable(value: BooleanInput) {
157-
this._editable = coerceBooleanProperty(value);
158-
}
159-
private _editable = true;
148+
@Input({transform: booleanAttribute}) editable: boolean = true;
160149

161150
/** Whether the completion of step is optional. */
162-
@Input()
163-
get optional(): boolean {
164-
return this._optional;
165-
}
166-
set optional(value: BooleanInput) {
167-
this._optional = coerceBooleanProperty(value);
168-
}
169-
private _optional = false;
151+
@Input({transform: booleanAttribute}) optional: boolean = false;
170152

171153
/** Whether step is marked as completed. */
172-
@Input()
154+
@Input({transform: booleanAttribute})
173155
get completed(): boolean {
174156
return this._completedOverride == null ? this._getDefaultCompleted() : this._completedOverride;
175157
}
176-
set completed(value: BooleanInput) {
177-
this._completedOverride = coerceBooleanProperty(value);
158+
set completed(value: boolean) {
159+
this._completedOverride = value;
178160
}
179161
_completedOverride: boolean | null = null;
180162

@@ -183,12 +165,12 @@ export class CdkStep implements OnChanges {
183165
}
184166

185167
/** Whether step has an error. */
186-
@Input()
168+
@Input({transform: booleanAttribute})
187169
get hasError(): boolean {
188170
return this._customError == null ? this._getDefaultError() : this._customError;
189171
}
190-
set hasError(value: BooleanInput) {
191-
this._customError = coerceBooleanProperty(value);
172+
set hasError(value: boolean) {
173+
this._customError = value;
192174
}
193175
private _customError: boolean | null = null;
194176

@@ -271,40 +253,31 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
271253
private _sortedHeaders = new QueryList<CdkStepHeader>();
272254

273255
/** Whether the validity of previous steps should be checked or not. */
274-
@Input()
275-
get linear(): boolean {
276-
return this._linear;
277-
}
278-
set linear(value: BooleanInput) {
279-
this._linear = coerceBooleanProperty(value);
280-
}
281-
private _linear = false;
256+
@Input({transform: booleanAttribute}) linear: boolean = false;
282257

283258
/** The index of the selected step. */
284-
@Input()
259+
@Input({transform: numberAttribute})
285260
get selectedIndex(): number {
286261
return this._selectedIndex;
287262
}
288-
set selectedIndex(index: NumberInput) {
289-
const newIndex = coerceNumberProperty(index);
290-
263+
set selectedIndex(index: number) {
291264
if (this.steps && this._steps) {
292265
// Ensure that the index can't be out of bounds.
293-
if (!this._isValidIndex(newIndex) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
266+
if (!this._isValidIndex(index) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
294267
throw Error('cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.');
295268
}
296269

297270
this.selected?._markAsInteracted();
298271

299272
if (
300-
this._selectedIndex !== newIndex &&
301-
!this._anyControlsInvalidOrPending(newIndex) &&
302-
(newIndex >= this._selectedIndex || this.steps.toArray()[newIndex].editable)
273+
this._selectedIndex !== index &&
274+
!this._anyControlsInvalidOrPending(index) &&
275+
(index >= this._selectedIndex || this.steps.toArray()[index].editable)
303276
) {
304-
this._updateSelectedItemIndex(newIndex);
277+
this._updateSelectedItemIndex(index);
305278
}
306279
} else {
307-
this._selectedIndex = newIndex;
280+
this._selectedIndex = index;
308281
}
309282
}
310283
private _selectedIndex = 0;
@@ -551,7 +524,7 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
551524
}
552525

553526
private _anyControlsInvalidOrPending(index: number): boolean {
554-
if (this._linear && index >= 0) {
527+
if (this.linear && index >= 0) {
555528
return this.steps
556529
.toArray()
557530
.slice(0, index)

tools/public_api_guard/cdk/stepper.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import { AfterContentInit } from '@angular/core';
88
import { AfterViewInit } from '@angular/core';
9-
import { BooleanInput } from '@angular/cdk/coercion';
109
import { ChangeDetectorRef } from '@angular/core';
1110
import { Directionality } from '@angular/cdk/bidi';
1211
import { ElementRef } from '@angular/core';
@@ -15,7 +14,6 @@ import { FocusableOption } from '@angular/cdk/a11y';
1514
import * as i0 from '@angular/core';
1615
import * as i5 from '@angular/cdk/bidi';
1716
import { InjectionToken } from '@angular/core';
18-
import { NumberInput } from '@angular/cdk/coercion';
1917
import { Observable } from 'rxjs';
2018
import { OnChanges } from '@angular/core';
2119
import { OnDestroy } from '@angular/core';
@@ -29,26 +27,32 @@ export class CdkStep implements OnChanges {
2927
ariaLabel: string;
3028
ariaLabelledby: string;
3129
get completed(): boolean;
32-
set completed(value: BooleanInput);
30+
set completed(value: boolean);
3331
// (undocumented)
3432
_completedOverride: boolean | null;
3533
content: TemplateRef<any>;
3634
// (undocumented)
3735
_displayDefaultIndicatorType: boolean;
38-
get editable(): boolean;
39-
set editable(value: BooleanInput);
36+
editable: boolean;
4037
errorMessage: string;
4138
get hasError(): boolean;
42-
set hasError(value: BooleanInput);
39+
set hasError(value: boolean);
4340
interacted: boolean;
4441
readonly interactedStream: EventEmitter<CdkStep>;
4542
label: string;
4643
// (undocumented)
4744
_markAsInteracted(): void;
4845
// (undocumented)
46+
static ngAcceptInputType_completed: unknown;
47+
// (undocumented)
48+
static ngAcceptInputType_editable: unknown;
49+
// (undocumented)
50+
static ngAcceptInputType_hasError: unknown;
51+
// (undocumented)
52+
static ngAcceptInputType_optional: unknown;
53+
// (undocumented)
4954
ngOnChanges(): void;
50-
get optional(): boolean;
51-
set optional(value: BooleanInput);
55+
optional: boolean;
5256
reset(): void;
5357
select(): void;
5458
_showError(): boolean;
@@ -96,10 +100,13 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
96100
_getStepContentId(i: number): string;
97101
_getStepLabelId(i: number): string;
98102
_groupId: number;
99-
get linear(): boolean;
100-
set linear(value: BooleanInput);
103+
linear: boolean;
101104
next(): void;
102105
// (undocumented)
106+
static ngAcceptInputType_linear: unknown;
107+
// (undocumented)
108+
static ngAcceptInputType_selectedIndex: unknown;
109+
// (undocumented)
103110
ngAfterContentInit(): void;
104111
// (undocumented)
105112
ngAfterViewInit(): void;
@@ -114,7 +121,7 @@ export class CdkStepper implements AfterContentInit, AfterViewInit, OnDestroy {
114121
get selected(): CdkStep | undefined;
115122
set selected(step: CdkStep | undefined);
116123
get selectedIndex(): number;
117-
set selectedIndex(index: NumberInput);
124+
set selectedIndex(index: number);
118125
readonly selectedIndexChange: EventEmitter<number>;
119126
readonly selectionChange: EventEmitter<StepperSelectionEvent>;
120127
_stateChanged(): void;

0 commit comments

Comments
 (0)