Skip to content

Commit 3d12a73

Browse files
ok7saiwagnermaciel
authored andcommitted
fix: enable overwriting preventDefault to allow triggering hyperlinks using enter key (angular#32123)
1 parent f336967 commit 3d12a73

File tree

3 files changed

+97
-33
lines changed

3 files changed

+97
-33
lines changed

src/aria/private/behaviors/event-manager/keyboard-event-manager.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,41 @@ export class KeyboardEventManager<T extends KeyboardEvent> extends EventManager<
3535
};
3636

3737
/** Configures this event manager to handle events with a specific key and no modifiers. */
38-
on(key: KeyCode, handler: EventHandler<T>): this;
38+
on(key: KeyCode, handler: EventHandler<T>, options?: Partial<EventHandlerOptions>): this;
3939

4040
/** Configures this event manager to handle events with a specific modifer and key combination. */
41-
on(modifiers: ModifierInputs, key: KeyCode, handler: EventHandler<T>): this;
41+
on(
42+
modifiers: ModifierInputs,
43+
key: KeyCode,
44+
handler: EventHandler<T>,
45+
options?: Partial<EventHandlerOptions>,
46+
): this;
4247

4348
on(...args: any[]) {
44-
const {modifiers, key, handler} = this._normalizeInputs(...args);
49+
const {modifiers, key, handler, options} = this._normalizeInputs(...args);
4550

4651
this.configs.push({
4752
handler: handler,
4853
matcher: event => this._isMatch(event, key, modifiers),
4954
...this.options,
55+
...options,
5056
});
5157

5258
return this;
5359
}
5460

5561
private _normalizeInputs(...args: any[]) {
56-
const key = args.length === 3 ? args[1] : args[0];
57-
const handler = args.length === 3 ? args[2] : args[1];
58-
const modifiers = args.length === 3 ? args[0] : Modifier.None;
62+
const withModifiers = Array.isArray(args[0]) || args[0] in Modifier;
63+
const modifiers = withModifiers ? args[0] : Modifier.None;
64+
const key = withModifiers ? args[1] : args[0];
65+
const handler = withModifiers ? args[2] : args[1];
66+
const options = withModifiers ? args[3] : args[2];
5967

6068
return {
6169
key: key as KeyCode,
6270
handler: handler as EventHandler<T>,
6371
modifiers: modifiers as ModifierInputs,
72+
options: (options ?? {}) as Partial<EventHandlerOptions>,
6473
};
6574
}
6675

src/aria/private/tree/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ export class TreePattern<V> {
275275
if (!this.followFocus() && this.inputs.multi()) {
276276
manager
277277
.on(this.dynamicSpaceKey, () => list.toggle())
278-
.on('Enter', () => list.toggle())
278+
.on('Enter', () => list.toggle(), {preventDefault: !this.nav()})
279279
.on([Modifier.Ctrl, Modifier.Meta], 'A', () => list.toggleAll());
280280
}
281281

282282
if (!this.followFocus() && !this.inputs.multi()) {
283283
manager.on(this.dynamicSpaceKey, () => list.selectOne());
284-
manager.on('Enter', () => list.selectOne());
284+
manager.on('Enter', () => list.selectOne(), {preventDefault: !this.nav()});
285285
}
286286

287287
if (this.inputs.multi() && this.followFocus()) {

src/components-examples/aria/tree/tree-configurable/tree-configurable-example.html

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,86 @@
4747

4848
<ng-template #treeNodes let-nodes="nodes" let-parent="parent">
4949
@for (node of nodes; track node.value) {
50-
<li
51-
ngTreeItem
52-
[parent]="parent"
53-
[value]="node.value"
54-
[label]="node.name"
55-
[disabled]="node.disabled"
56-
[selectable]="!nav.value || !node.children"
57-
#treeItem="ngTreeItem"
58-
class="example-tree-item example-selectable example-stateful"
59-
>
60-
<span aria-hidden="true" class="material-symbols-outlined example-parent-icon example-icon">{{node.children ? 'chevron_right' : ''}}</span>
61-
<span aria-hidden="true" class="material-symbols-outlined example-icon">{{node.children ? 'folder' : 'docs'}}</span>
62-
{{ node.name }}
63-
<span aria-hidden="true" class="material-symbols-outlined example-selected-icon example-icon">check</span>
64-
</li>
50+
@if (nav.value) {
51+
<li role="none" style="list-style: none">
52+
<a
53+
ngTreeItem
54+
[parent]="parent"
55+
[value]="node.value"
56+
[label]="node.name"
57+
[disabled]="node.disabled"
58+
[selectable]="!node.children"
59+
#treeItem="ngTreeItem"
60+
class="example-tree-item example-selectable example-stateful"
61+
[attr.href]="!!node.children ? null : '/aria-tree#' + node.name"
62+
>
63+
<span
64+
aria-hidden="true"
65+
class="material-symbols-outlined example-parent-icon example-icon"
66+
>{{node.children ? 'chevron_right' : ''}}</span
67+
>
68+
<span
69+
aria-hidden="true"
70+
class="material-symbols-outlined example-icon"
71+
>{{node.children ? 'folder' : 'docs'}}</span
72+
>
73+
{{ node.name }}
74+
<span
75+
aria-hidden="true"
76+
class="material-symbols-outlined example-selected-icon example-icon"
77+
>check</span
78+
>
79+
</a>
80+
</li>
6581

66-
@if (node.children) {
67-
<ul role="group">
68-
<ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup">
69-
<ng-template
70-
[ngTemplateOutlet]="treeNodes"
71-
[ngTemplateOutletContext]="{nodes: node.children, parent: group}"
72-
/>
73-
</ng-template>
74-
</ul>
75-
}
82+
@if (node.children) {
83+
<ul role="group">
84+
<ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup">
85+
<ng-template
86+
[ngTemplateOutlet]="treeNodes"
87+
[ngTemplateOutletContext]="{nodes: node.children, parent: group}"
88+
/>
89+
</ng-template>
90+
</ul>
91+
}
92+
} @else {
93+
<li
94+
ngTreeItem
95+
[parent]="parent"
96+
[value]="node.value"
97+
[label]="node.name"
98+
[disabled]="node.disabled"
99+
#treeItem="ngTreeItem"
100+
class="example-tree-item example-selectable example-stateful"
101+
>
102+
<span
103+
aria-hidden="true"
104+
class="material-symbols-outlined example-parent-icon example-icon"
105+
>{{node.children ? 'chevron_right' : ''}}</span
106+
>
107+
<span
108+
aria-hidden="true"
109+
class="material-symbols-outlined example-icon"
110+
>{{node.children ? 'folder' : 'docs'}}</span
111+
>
112+
{{ node.name }}
113+
<span
114+
aria-hidden="true"
115+
class="material-symbols-outlined example-selected-icon example-icon"
116+
>check</span
117+
>
118+
</li>
119+
120+
@if (node.children) {
121+
<ul role="group">
122+
<ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup">
123+
<ng-template
124+
[ngTemplateOutlet]="treeNodes"
125+
[ngTemplateOutletContext]="{nodes: node.children, parent: group}"
126+
/>
127+
</ng-template>
128+
</ul>
129+
}
130+
}
76131
}
77132
</ng-template>

0 commit comments

Comments
 (0)