Skip to content

Commit e7a197a

Browse files
crisbetojelbourn
authored andcommitted
fix(popover-edit): unable to close focus content using the keyboard (#18945)
The popover edit shows some content either when a cell is hovered or when it's focused, but the problem is that keyboard users don't have a way of closing the focus content without moving focus completely out of the table, because tabbing again usually lands on a different cell. These changes allow for the focus content to be removed by pressing the Escape key. (cherry picked from commit 17f8e94)
1 parent 897dee4 commit e7a197a

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,21 @@ describe('CDK Popover Edit', () => {
441441
expect(component.hoverContentStateForRow(4)).toBe(HoverContentState.FOCUSABLE);
442442
}));
443443

444+
it('should close the focus content when pressing escape', fakeAsync(() => {
445+
expect(component.hoverContentStateForRow(2)).toBe(HoverContentState.OFF);
446+
447+
component.focusEditCell(2);
448+
tick(1);
449+
450+
expect(component.hoverContentStateForRow(2)).toBe(HoverContentState.ON);
451+
452+
const event = new KeyboardEvent('keydown', {bubbles: true, key: 'Escape'});
453+
component.getEditCell(2).dispatchEvent(event);
454+
tick(1);
455+
456+
expect(component.hoverContentStateForRow(2)).toBe(HoverContentState.OFF);
457+
}));
458+
444459
it('shows hover content for the editing row and makes the rows above and below ' +
445460
'focusable unless focus is in a different table row in which case it takes priority',
446461
fakeAsync(() => {

src/cdk-experimental/popover-edit/table-directives.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,25 @@ export class CdkEditable implements AfterViewInit, OnDestroy {
104104

105105
// Track focus within the table to hide/show/make focusable hover content.
106106
fromEventPattern<FocusEvent>(
107-
(handler) => element.addEventListener('focus', handler, true),
108-
(handler) => element.removeEventListener('focus', handler, true)
107+
handler => element.addEventListener('focus', handler, true),
108+
handler => element.removeEventListener('focus', handler, true)
109109
).pipe(
110110
takeUntil(this.destroyed),
111111
toClosest(ROW_SELECTOR),
112112
share(),
113113
).subscribe(this.editEventDispatcher.focused);
114-
fromEventPattern<FocusEvent>(
115-
(handler) => element.addEventListener('blur', handler, true),
116-
(handler) => element.removeEventListener('blur', handler, true)
117-
).pipe(
118-
takeUntil(this.destroyed),
119-
mapTo(null),
120-
share(),
121-
).subscribe(this.editEventDispatcher.focused);
114+
115+
merge(
116+
fromEventPattern<FocusEvent>(
117+
handler => element.addEventListener('blur', handler, true),
118+
handler => element.removeEventListener('blur', handler, true)
119+
),
120+
fromEvent<KeyboardEvent>(element, 'keydown').pipe(filter(event => event.key === 'Escape'))
121+
).pipe(
122+
takeUntil(this.destroyed),
123+
mapTo(null),
124+
share(),
125+
).subscribe(this.editEventDispatcher.focused);
122126

123127
// Keep track of rows within the table. This is used to know which rows with hover content
124128
// are first or last in the table. They are kept focusable in case focus enters from above

0 commit comments

Comments
 (0)