Skip to content

Commit f9155be

Browse files
committed
addressed comments
1 parent f5486a8 commit f9155be

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
@@ -180,6 +180,28 @@ describe('FocusOriginMonitor', () => {
180180
expect(changeHandler).toHaveBeenCalledWith('program');
181181
}, 0);
182182
}));
183+
184+
it('should remove focus classes on blur', async(() => {
185+
if (platform.FIREFOX) { return; }
186+
187+
buttonElement.focus();
188+
fixture.detectChanges();
189+
190+
setTimeout(() => {
191+
fixture.detectChanges();
192+
193+
expect(buttonElement.classList.length)
194+
.toBe(2, 'button should have exactly 2 focus classes');
195+
expect(changeHandler).toHaveBeenCalledWith('program');
196+
197+
buttonElement.blur();
198+
fixture.detectChanges();
199+
200+
expect(buttonElement.classList.length)
201+
.toBe(0, 'button should not have any focus classes');
202+
expect(changeHandler).toHaveBeenCalledWith(null);
203+
}, 0);
204+
}));
183205
});
184206

185207

@@ -275,6 +297,28 @@ describe('cdkFocusClasses', () => {
275297
expect(changeHandler).toHaveBeenCalledWith('program');
276298
}, 0);
277299
}));
300+
301+
it('should remove focus classes on blur', async(() => {
302+
if (platform.FIREFOX) { return; }
303+
304+
buttonElement.focus();
305+
fixture.detectChanges();
306+
307+
setTimeout(() => {
308+
fixture.detectChanges();
309+
310+
expect(buttonElement.classList.length)
311+
.toBe(2, 'button should have exactly 2 focus classes');
312+
expect(changeHandler).toHaveBeenCalledWith('program');
313+
314+
buttonElement.blur();
315+
fixture.detectChanges();
316+
317+
expect(buttonElement.classList.length)
318+
.toBe(0, 'button should not have any focus classes');
319+
expect(changeHandler).toHaveBeenCalledWith(null);
320+
}, 0);
321+
}));
278322
});
279323

280324

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)