Skip to content

Commit ade524f

Browse files
committed
refactor(cdk/listbox): switch to input transforms
Switches inputs in cdk/listbox to use transforms instead of getters/setters.
1 parent d8d350a commit ade524f

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/cdk/listbox/listbox.ts

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

99
import {
1010
AfterContentInit,
11+
booleanAttribute,
1112
ChangeDetectorRef,
1213
ContentChildren,
1314
Directive,
@@ -33,7 +34,7 @@ import {
3334
SPACE,
3435
UP_ARROW,
3536
} from '@angular/cdk/keycodes';
36-
import {BooleanInput, coerceArray, coerceBooleanProperty} from '@angular/cdk/coercion';
37+
import {coerceArray} from '@angular/cdk/coercion';
3738
import {SelectionModel} from '@angular/cdk/collections';
3839
import {defer, fromEvent, merge, Observable, Subject} from 'rxjs';
3940
import {filter, map, startWith, switchMap, takeUntil} from 'rxjs/operators';
@@ -115,12 +116,12 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
115116
@Input('cdkOptionTypeaheadLabel') typeaheadLabel: string;
116117

117118
/** Whether this option is disabled. */
118-
@Input('cdkOptionDisabled')
119+
@Input({alias: 'cdkOptionDisabled', transform: booleanAttribute})
119120
get disabled(): boolean {
120121
return this.listbox.disabled || this._disabled;
121122
}
122-
set disabled(value: BooleanInput) {
123-
this._disabled = coerceBooleanProperty(value);
123+
set disabled(value: boolean) {
124+
this._disabled = value;
124125
}
125126
private _disabled: boolean = false;
126127

@@ -281,37 +282,25 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
281282
* Whether the listbox allows multiple options to be selected. If the value switches from `true`
282283
* to `false`, and more than one option is selected, all options are deselected.
283284
*/
284-
@Input('cdkListboxMultiple')
285+
@Input({alias: 'cdkListboxMultiple', transform: booleanAttribute})
285286
get multiple(): boolean {
286287
return this.selectionModel.multiple;
287288
}
288-
set multiple(value: BooleanInput) {
289-
this.selectionModel.multiple = coerceBooleanProperty(value);
289+
set multiple(value: boolean) {
290+
this.selectionModel.multiple = value;
290291

291292
if (this.options) {
292293
this._updateInternalValue();
293294
}
294295
}
295296

296297
/** Whether the listbox is disabled. */
297-
@Input('cdkListboxDisabled')
298-
get disabled(): boolean {
299-
return this._disabled;
300-
}
301-
set disabled(value: BooleanInput) {
302-
this._disabled = coerceBooleanProperty(value);
303-
}
304-
private _disabled: boolean = false;
298+
@Input({alias: 'cdkListboxDisabled', transform: booleanAttribute})
299+
disabled: boolean = false;
305300

306301
/** Whether the listbox will use active descendant or will move focus onto the options. */
307-
@Input('cdkListboxUseActiveDescendant')
308-
get useActiveDescendant(): boolean {
309-
return this._useActiveDescendant;
310-
}
311-
set useActiveDescendant(shouldUseActiveDescendant: BooleanInput) {
312-
this._useActiveDescendant = coerceBooleanProperty(shouldUseActiveDescendant);
313-
}
314-
private _useActiveDescendant: boolean = false;
302+
@Input({alias: 'cdkListboxUseActiveDescendant', transform: booleanAttribute})
303+
useActiveDescendant: boolean = false;
315304

316305
/** The orientation of the listbox. Only affects keyboard interaction, not visual layout. */
317306
@Input('cdkListboxOrientation')
@@ -341,23 +330,23 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
341330
* Whether the keyboard navigation should wrap when the user presses arrow down on the last item
342331
* or arrow up on the first item.
343332
*/
344-
@Input('cdkListboxNavigationWrapDisabled')
333+
@Input({alias: 'cdkListboxNavigationWrapDisabled', transform: booleanAttribute})
345334
get navigationWrapDisabled() {
346335
return this._navigationWrapDisabled;
347336
}
348-
set navigationWrapDisabled(wrap: BooleanInput) {
349-
this._navigationWrapDisabled = coerceBooleanProperty(wrap);
337+
set navigationWrapDisabled(wrap: boolean) {
338+
this._navigationWrapDisabled = wrap;
350339
this.listKeyManager?.withWrap(!this._navigationWrapDisabled);
351340
}
352341
private _navigationWrapDisabled = false;
353342

354343
/** Whether keyboard navigation should skip over disabled items. */
355-
@Input('cdkListboxNavigatesDisabledOptions')
344+
@Input({alias: 'cdkListboxNavigatesDisabledOptions', transform: booleanAttribute})
356345
get navigateDisabledOptions() {
357346
return this._navigateDisabledOptions;
358347
}
359-
set navigateDisabledOptions(skip: BooleanInput) {
360-
this._navigateDisabledOptions = coerceBooleanProperty(skip);
348+
set navigateDisabledOptions(skip: boolean) {
349+
this._navigateDisabledOptions = skip;
361350
this.listKeyManager?.skipPredicate(
362351
this._navigateDisabledOptions ? this._skipNonePredicate : this._skipDisabledPredicate,
363352
);
@@ -683,7 +672,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
683672

684673
/** Called when the user presses keydown on the listbox. */
685674
protected _handleKeydown(event: KeyboardEvent) {
686-
if (this._disabled) {
675+
if (this.disabled) {
687676
return;
688677
}
689678

@@ -809,7 +798,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
809798

810799
/** Get the id of the active option if active descendant is being used. */
811800
protected _getAriaActiveDescendant(): string | null | undefined {
812-
return this._useActiveDescendant ? this.listKeyManager?.activeItem?.id : null;
801+
return this.useActiveDescendant ? this.listKeyManager?.activeItem?.id : null;
813802
}
814803

815804
/** Get the tabindex for the listbox. */

tools/public_api_guard/cdk/listbox.md

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

77
import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
88
import { AfterContentInit } from '@angular/core';
9-
import { BooleanInput } from '@angular/cdk/coercion';
109
import { ChangeDetectorRef } from '@angular/core';
1110
import { ControlValueAccessor } from '@angular/forms';
1211
import { Highlightable } from '@angular/cdk/a11y';
@@ -27,8 +26,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
2726
deselect(option: CdkOption<T>): void;
2827
deselectValue(value: T): void;
2928
protected readonly destroyed: Subject<void>;
30-
get disabled(): boolean;
31-
set disabled(value: BooleanInput);
29+
disabled: boolean;
3230
protected readonly element: HTMLElement;
3331
get enabledTabIndex(): number | null;
3432
set enabledTabIndex(value: number | null);
@@ -46,11 +44,21 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
4644
isValueSelected(value: T): boolean;
4745
protected listKeyManager: ActiveDescendantKeyManager<CdkOption<T>>;
4846
get multiple(): boolean;
49-
set multiple(value: BooleanInput);
50-
get navigateDisabledOptions(): BooleanInput;
51-
set navigateDisabledOptions(skip: BooleanInput);
52-
get navigationWrapDisabled(): BooleanInput;
53-
set navigationWrapDisabled(wrap: BooleanInput);
47+
set multiple(value: boolean);
48+
get navigateDisabledOptions(): boolean;
49+
set navigateDisabledOptions(skip: boolean);
50+
get navigationWrapDisabled(): boolean;
51+
set navigationWrapDisabled(wrap: boolean);
52+
// (undocumented)
53+
static ngAcceptInputType_disabled: unknown;
54+
// (undocumented)
55+
static ngAcceptInputType_multiple: unknown;
56+
// (undocumented)
57+
static ngAcceptInputType_navigateDisabledOptions: unknown;
58+
// (undocumented)
59+
static ngAcceptInputType_navigationWrapDisabled: unknown;
60+
// (undocumented)
61+
static ngAcceptInputType_useActiveDescendant: unknown;
5462
// (undocumented)
5563
ngAfterContentInit(): void;
5664
// (undocumented)
@@ -71,8 +79,7 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
7179
toggleValue(value: T): void;
7280
protected triggerOption(option: CdkOption<T> | null): void;
7381
protected triggerRange(trigger: CdkOption<T> | null, from: number, to: number, on: boolean): void;
74-
get useActiveDescendant(): boolean;
75-
set useActiveDescendant(shouldUseActiveDescendant: BooleanInput);
82+
useActiveDescendant: boolean;
7683
get value(): readonly T[];
7784
set value(value: readonly T[]);
7885
readonly valueChange: Subject<ListboxValueChangeEvent<T>>;
@@ -99,7 +106,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
99106
deselect(): void;
100107
protected destroyed: Subject<void>;
101108
get disabled(): boolean;
102-
set disabled(value: BooleanInput);
109+
set disabled(value: boolean);
103110
readonly element: HTMLElement;
104111
get enabledTabIndex(): number | null;
105112
set enabledTabIndex(value: number | null);
@@ -113,6 +120,8 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
113120
isSelected(): boolean;
114121
protected readonly listbox: CdkListbox<T>;
115122
// (undocumented)
123+
static ngAcceptInputType_disabled: unknown;
124+
// (undocumented)
116125
ngOnDestroy(): void;
117126
select(): void;
118127
setActiveStyles(): void;

0 commit comments

Comments
 (0)