Skip to content

Commit 7ddd73d

Browse files
committed
addressed comments
1 parent 4bc35ad commit 7ddd73d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/lib/core/style/focus-classes.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,28 @@ describe('FocusOriginMonitor', () => {
158158
expect(changeHandler).toHaveBeenCalledWith('program');
159159
}, 0);
160160
}));
161+
162+
it('should remove focus classes on blur', async(() => {
163+
if (platform.FIREFOX) { return; }
164+
165+
buttonElement.focus();
166+
fixture.detectChanges();
167+
168+
setTimeout(() => {
169+
fixture.detectChanges();
170+
171+
expect(buttonElement.classList.length)
172+
.toBe(2, 'button should have exactly 2 focus classes');
173+
expect(changeHandler).toHaveBeenCalledWith('program');
174+
175+
buttonElement.blur();
176+
fixture.detectChanges();
177+
178+
expect(buttonElement.classList.length)
179+
.toBe(0, 'button should not have any focus classes');
180+
expect(changeHandler).toHaveBeenCalledWith(null);
181+
}, 0);
182+
}));
161183
});
162184

163185

@@ -248,6 +270,28 @@ describe('cdkFocusClasses', () => {
248270
expect(changeHandler).toHaveBeenCalledWith('program');
249271
}, 0);
250272
}));
273+
274+
it('should remove focus classes on blur', async(() => {
275+
if (platform.FIREFOX) { return; }
276+
277+
buttonElement.focus();
278+
fixture.detectChanges();
279+
280+
setTimeout(() => {
281+
fixture.detectChanges();
282+
283+
expect(buttonElement.classList.length)
284+
.toBe(2, 'button should have exactly 2 focus classes');
285+
expect(changeHandler).toHaveBeenCalledWith('program');
286+
287+
buttonElement.blur();
288+
fixture.detectChanges();
289+
290+
expect(buttonElement.classList.length)
291+
.toBe(0, 'button should not have any focus classes');
292+
expect(changeHandler).toHaveBeenCalledWith(null);
293+
}, 0);
294+
}));
251295
});
252296

253297

src/lib/core/style/focus-classes.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class FocusOriginMonitor {
1212
/** The focus origin that the next focus event is a result of. */
1313
private _origin: FocusOrigin = null;
1414

15-
/** A WeakMap used to track the last element focused via the FocusOriginMonitor. */
16-
private _lastFocused = new WeakMap<Element, FocusOrigin>();
15+
/** The FocusOrigin of the last focus event tracked by the FocusOriginMonitor. */
16+
private _lastFocusOrigin: FocusOrigin;
1717

1818
/** Whether the window has just been focused. */
1919
private _windowFocused = false;
@@ -63,8 +63,8 @@ export class FocusOriginMonitor {
6363
// 2) The element was programmatically focused, in which case we should mark the origin as
6464
// 'program'.
6565
if (!this._origin) {
66-
if (this._windowFocused && this._lastFocused.has(element)) {
67-
this._origin = this._lastFocused.get(element);
66+
if (this._windowFocused && this._lastFocusOrigin) {
67+
this._origin = this._lastFocusOrigin;
6868
} else {
6969
this._origin = 'program';
7070
}
@@ -76,7 +76,7 @@ export class FocusOriginMonitor {
7676
renderer.setElementClass(element, 'cdk-program-focused', this._origin == 'program');
7777

7878
subject.next(this._origin);
79-
this._lastFocused = new WeakMap().set(element, this._origin);
79+
this._lastFocusOrigin = this._origin;
8080
this._origin = null;
8181
}
8282

0 commit comments

Comments
 (0)