Skip to content

Commit 1bfa13e

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

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import {FocusableOption} from '@angular/cdk/a11y';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {
12-
ContentChild,
1312
Directive,
1413
ElementRef,
1514
EventEmitter,
16-
forwardRef,
1715
Input,
1816
OnDestroy,
1917
Output,
@@ -77,7 +75,7 @@ export class MdBasicChip { }
7775
'(click)': '_handleClick($event)',
7876
'(keydown)': '_handleKeydown($event)',
7977
'(focus)': '_hasFocus = true',
80-
'(blur)': '_hasFocus = false',
78+
'(blur)': '_blur()',
8179
},
8280

8381
})
@@ -130,6 +128,9 @@ export class MdChip extends _MdChipMixinBase implements FocusableOption, OnDestr
130128
/** Emits when the chip is focused. */
131129
_onFocus = new Subject<MdChipEvent>();
132130

131+
/** Emits when the chip is blured. */
132+
_onBlur = new Subject<MdChipEvent>();
133+
133134
/** Emitted when the chip is selected or deselected. */
134135
@Output() onSelectionChange = new EventEmitter<MdChipSelectionChange>();
135136

@@ -233,6 +234,11 @@ export class MdChip extends _MdChipMixinBase implements FocusableOption, OnDestr
233234
break;
234235
}
235236
}
237+
238+
_blur() {
239+
this._hasFocus = false;
240+
this._onBlur.next({chip: this});
241+
}
236242
}
237243

238244

0 commit comments

Comments
 (0)