Skip to content

Commit 0d75c2a

Browse files
committed
Fix blur & focus behavior
1 parent 15106aa commit 0d75c2a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/lib/chips/chip-list.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
<<<<<<< HEAD
21
import {FocusKeyManager} from '@angular/cdk/a11y';
32
import {createKeyboardEvent, dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
43
import {Component, DebugElement, QueryList, ViewChild, ViewChildren} from '@angular/core';
54
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
6-
import {
7-
FormControl,
8-
FormsModule,
9-
ReactiveFormsModule,
10-
} from '@angular/forms';
5+
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
116
import {
127
BACKSPACE,
138
DELETE,

src/lib/chips/chip-list.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
104104
/** Subscription to focus changes in the chips. */
105105
private _chipFocusSubscription: Subscription|null;
106106

107+
/** Subscription to blur changes in the chips. */
108+
private _chipBlurSubscription: Subscription|null;
109+
107110
/** Subscription to selection changes in chips. */
108111
private _chipSelectionSubscription: Subscription|null;
109112

@@ -277,6 +280,11 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
277280
return merge(...this.chips.map(chip => chip._onFocus));
278281
}
279282

283+
/** Combined stream of all of the child chips' blur change events. */
284+
get chipBlurChanges(): Observable<MdChipEvent> {
285+
return merge(...this.chips.map(chip => chip._onBlur));
286+
}
287+
280288
/** Combined stream of all of the child chips' remove change events. */
281289
get chipRemoveChanges(): Observable<MdChipEvent> {
282290
return merge(...this.chips.map(chip => chip.destroy));
@@ -649,6 +657,11 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
649657
this._chipFocusSubscription = null;
650658
}
651659

660+
if (this._chipBlurSubscription) {
661+
this._chipBlurSubscription.unsubscribe();
662+
this._chipBlurSubscription = null;
663+
}
664+
652665
if (this._chipSelectionSubscription) {
653666
this._chipSelectionSubscription.unsubscribe();
654667
this._chipSelectionSubscription = null;
@@ -687,6 +700,11 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
687700
}
688701
this.stateChanges.next();
689702
});
703+
704+
this._chipBlurSubscription = this.chipBlurChanges.subscribe(_ => {
705+
this._blur();
706+
this.stateChanges.next();
707+
});
690708
}
691709

692710
private _listenToChipsRemoved(): void {

src/lib/chips/chip.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class MdBasicChip { }
7777
'(click)': '_handleClick($event)',
7878
'(keydown)': '_handleKeydown($event)',
7979
'(focus)': '_hasFocus = true',
80-
'(blur)': '_hasFocus = false',
80+
'(blur)': '_blur()',
8181
},
8282

8383
})
@@ -130,6 +130,9 @@ export class MdChip extends _MdChipMixinBase implements FocusableOption, OnDestr
130130
/** Emits when the chip is focused. */
131131
_onFocus = new Subject<MdChipEvent>();
132132

133+
/** Emits when the chip is blured. */
134+
_onBlur = new Subject<MdChipEvent>();
135+
133136
/** Emitted when the chip is selected or deselected. */
134137
@Output() onSelectionChange = new EventEmitter<MdChipSelectionChange>();
135138

@@ -233,6 +236,11 @@ export class MdChip extends _MdChipMixinBase implements FocusableOption, OnDestr
233236
break;
234237
}
235238
}
239+
240+
_blur() {
241+
this._hasFocus = false;
242+
this._onBlur.next({chip: this});
243+
}
236244
}
237245

238246

0 commit comments

Comments
 (0)