Skip to content

Commit 104c403

Browse files
committed
fix(material/tree): Fix misc PR comments.
Fix miscellaneous PR comments. Take a first pass to fix comments that are easiest to fix. This commit message will be squashed away when merging. * move type definitions in tree-key-manager.ts to a new file, tree-key-manager-strategy.ts * remove unnecessary comment * fix JSDoc comment syntax * remove isNotNullish * consolidate event handling code in CdkTreeNodeToggle * remove unnecessary type coerscion * simply logic for detecting TreeControlMissingError and MultipleTreeControlsError * adopt Input transform * remove unnecessary nesting in _updateActiveItemIndex
1 parent 9310d0f commit 104c403

File tree

11 files changed

+252
-237
lines changed

11 files changed

+252
-237
lines changed

src/cdk/a11y/key-manager/legacy-tree-key-manager.ts renamed to src/cdk/a11y/key-manager/noop-tree-key-manager.ts

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

99
import {Subject} from 'rxjs';
10+
import {TREE_KEY_MANAGER} from './tree-key-manager';
1011
import {
11-
TREE_KEY_MANAGER,
1212
TreeKeyManagerFactory,
1313
TreeKeyManagerItem,
1414
TreeKeyManagerStrategy,
15-
} from './tree-key-manager';
15+
} from './tree-key-manager-strategy';
1616

17+
// NoopTreeKeyManager is a "noop" implementation of TreeKeyMangerStrategy. Methods are noops. Does
18+
// not emit to streams.
19+
//
20+
// Used for applications built before TreeKeyManager to opt-out of TreeKeyManager and revert to
21+
// legacy behavior.
1722
/**
1823
* @docs-private
1924
*
20-
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
25+
* @deprecated NoopTreeKeyManager deprecated. Use TreeKeyManager or inject a
2126
* TreeKeyManagerStrategy instead. To be removed in a future version.
2227
*
2328
* @breaking-change 19.0.0
2429
*/
25-
// LegacyTreeKeyManager is a "noop" implementation of TreeKeyMangerStrategy. Methods are noops. Does
26-
// not emit to streams.
27-
//
28-
// Used for applications built before TreeKeyManager to opt-out of TreeKeyManager and revert to
29-
// legacy behavior.
30-
export class LegacyTreeKeyManager<T extends TreeKeyManagerItem>
31-
implements TreeKeyManagerStrategy<T>
32-
{
33-
readonly _isLegacyTreeKeyManager = true;
30+
export class NoopTreeKeyManager<T extends TreeKeyManagerItem> implements TreeKeyManagerStrategy<T> {
31+
readonly _isNoopTreeKeyManager = true;
3432

35-
// Provide change as required by TreeKeyManagerStrategy. LegacyTreeKeyManager is a "noop"
33+
// Provide change as required by TreeKeyManagerStrategy. NoopTreeKeyManager is a "noop"
3634
// implementation that does not emit to streams.
3735
readonly change = new Subject<T | null>();
3836

@@ -41,13 +39,13 @@ export class LegacyTreeKeyManager<T extends TreeKeyManagerItem>
4139
}
4240

4341
getActiveItemIndex() {
44-
// Always return null. LegacyTreeKeyManager is a "noop" implementation that does not maintain
42+
// Always return null. NoopTreeKeyManager is a "noop" implementation that does not maintain
4543
// the active item.
4644
return null;
4745
}
4846

4947
getActiveItem() {
50-
// Always return null. LegacyTreeKeyManager is a "noop" implementation that does not maintain
48+
// Always return null. NoopTreeKeyManager is a "noop" implementation that does not maintain
5149
// the active item.
5250
return null;
5351
}
@@ -64,26 +62,26 @@ export class LegacyTreeKeyManager<T extends TreeKeyManagerItem>
6462
/**
6563
* @docs-private
6664
*
67-
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
65+
* @deprecated NoopTreeKeyManager deprecated. Use TreeKeyManager or inject a
6866
* TreeKeyManagerStrategy instead. To be removed in a future version.
6967
*
7068
* @breaking-change 19.0.0
7169
*/
72-
export function LEGACY_TREE_KEY_MANAGER_FACTORY<
70+
export function NOOP_TREE_KEY_MANAGER_FACTORY<
7371
T extends TreeKeyManagerItem,
7472
>(): TreeKeyManagerFactory<T> {
75-
return () => new LegacyTreeKeyManager<T>();
73+
return () => new NoopTreeKeyManager<T>();
7674
}
7775

7876
/**
7977
* @docs-private
8078
*
81-
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
79+
* @deprecated NoopTreeKeyManager deprecated. Use TreeKeyManager or inject a
8280
* TreeKeyManagerStrategy instead. To be removed in a future version.
8381
*
8482
* @breaking-change 19.0.0
8583
*/
86-
export const LEGACY_TREE_KEY_MANAGER_FACTORY_PROVIDER = {
84+
export const NOOP_TREE_KEY_MANAGER_FACTORY_PROVIDER = {
8785
provide: TREE_KEY_MANAGER,
88-
useFactory: LEGACY_TREE_KEY_MANAGER_FACTORY,
86+
useFactory: NOOP_TREE_KEY_MANAGER_FACTORY,
8987
};
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {QueryList} from '@angular/core';
10+
import {Observable, Subject} from 'rxjs';
11+
12+
/** Represents an item within a tree that can be passed to a TreeKeyManager. */
13+
export interface TreeKeyManagerItem {
14+
/** Whether the item is disabled. */
15+
isDisabled?: (() => boolean) | boolean;
16+
17+
/** The user-facing label for this item. */
18+
getLabel?(): string;
19+
20+
/** Perform the main action (i.e. selection) for this item. */
21+
activate(): void;
22+
23+
/** Retrieves the parent for this item. This is `null` if there is no parent. */
24+
getParent(): TreeKeyManagerItem | null;
25+
26+
/** Retrieves the children for this item. */
27+
getChildren(): TreeKeyManagerItem[] | Observable<TreeKeyManagerItem[]>;
28+
29+
/** Determines if the item is currently expanded. */
30+
isExpanded: (() => boolean) | boolean;
31+
32+
/** Collapses the item, hiding its children. */
33+
collapse(): void;
34+
35+
/** Expands the item, showing its children. */
36+
expand(): void;
37+
38+
/**
39+
* Focuses the item. This should provide some indication to the user that this item is focused.
40+
*/
41+
focus(): void;
42+
}
43+
44+
/**
45+
* Configuration for the TreeKeyManager.
46+
*/
47+
export interface TreeKeyManagerOptions<T extends TreeKeyManagerItem> {
48+
/**
49+
* If true, then the key manager will call `activate` in addition to calling `focus` when a
50+
* particular item is focused. By default, this is false.
51+
*/
52+
activationFollowsFocus?: boolean;
53+
54+
/**
55+
* The direction in which the tree items are laid out horizontally. This influences which key
56+
* will be interpreted as expand or collapse. Defaults to 'ltr'.
57+
*/
58+
horizontalOrientation?: 'rtl' | 'ltr';
59+
60+
/**
61+
* Sets the predicate function that determines which items should be skipped by the tree key
62+
* manager. By default, disabled items are skipped.
63+
*
64+
* If the item is to be skipped, this function should return false.
65+
*/
66+
skipPredicate?: (item: T) => boolean;
67+
68+
/**
69+
* If provided, determines how the key manager determines if two items are equivalent.
70+
*
71+
* It should provide a unique key for each unique tree item. If two tree items are equivalent,
72+
* then this function should return the same value.
73+
*/
74+
trackBy?: (treeItem: T) => unknown;
75+
76+
/**
77+
* If a value is provided, enables typeahead mode, which allows users to set the active item
78+
* by typing the visible label of the item.
79+
*
80+
* If a number is provided, this will be the time to wait after the last keystroke before
81+
* setting the active item. If `true` is provided, the default interval of 200ms will be used.
82+
*/
83+
typeAheadDebounceInterval?: true | number;
84+
}
85+
86+
export interface TreeKeyManagerStrategy<T extends TreeKeyManagerItem> {
87+
/** Stream that emits any time the focused item changes. */
88+
readonly change: Subject<T | null>;
89+
90+
/**
91+
* Handles a keyboard event on the tree.
92+
*
93+
* @param event Keyboard event that represents the user interaction with the tree.
94+
*/
95+
onKeydown(event: KeyboardEvent): void;
96+
97+
/** Index of the currently active item. */
98+
getActiveItemIndex(): number | null;
99+
100+
/** The currently active item. */
101+
getActiveItem(): T | null;
102+
103+
/**
104+
* Called the first time the Tree component is focused. This method will only be called once over
105+
* the lifetime of the Tree component.
106+
*
107+
* Intended to be used to focus the first item in the tree.
108+
*/
109+
onInitialFocus(): void;
110+
111+
/**
112+
* Focus the provided item by index.
113+
*
114+
* Updates the state of the currently active item. Emits to `change` stream if active item
115+
* Changes.
116+
* @param index The index of the item to focus.
117+
* @param options Additional focusing options.
118+
*/
119+
focusItem(index: number, options?: {emitChangeEvent?: boolean}): void;
120+
/**
121+
* Focus the provided item.
122+
*
123+
* Updates the state of the currently active item. Emits to `change` stream if active item
124+
* Changes.
125+
* @param item The item to focus. Equality is determined via the trackBy function.
126+
* @param options Additional focusing options.
127+
*/
128+
focusItem(item: T, options?: {emitChangeEvent?: boolean}): void;
129+
focusItem(itemOrIndex: number | T, options?: {emitChangeEvent?: boolean}): void;
130+
}
131+
132+
export type TreeKeyManagerFactory<T extends TreeKeyManagerItem> = (
133+
items: Observable<T[]> | QueryList<T> | T[],
134+
options: TreeKeyManagerOptions<T>,
135+
) => TreeKeyManagerStrategy<T>;

src/cdk/a11y/key-manager/tree-key-manager.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
} from '@angular/cdk/keycodes';
1313
import {createKeyboardEvent} from '../../testing/private';
1414
import {QueryList} from '@angular/core';
15-
import {TreeKeyManager, TreeKeyManagerItem} from './tree-key-manager';
15+
import {TreeKeyManager} from './tree-key-manager';
16+
import {TreeKeyManagerItem} from './tree-key-manager-strategy';
1617
import {Observable, of as observableOf, Subscription} from 'rxjs';
1718
import {fakeAsync, tick} from '@angular/core/testing';
1819

0 commit comments

Comments
 (0)