Skip to content

Commit 1ffa1fc

Browse files
crisbetoandrewseguin
authored andcommitted
fix(chips): not updating keyboard controls if direction changes (#14080)
Fixes the chip list not updating the direction of its keyboard controls, if the direction changes after the component has been initialized.
1 parent ed857c9 commit 1ffa1fc

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/lib/chips/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ng_test_library(
4545
name = "chips_test_sources",
4646
srcs = glob(["**/*.spec.ts"]),
4747
deps = [
48+
"@rxjs",
4849
"@angular//packages/animations",
4950
"@angular//packages/forms",
5051
"@angular//packages/platform-browser",

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {async, ComponentFixture, TestBed} from '@angular/core/testing';
77
import {By} from '@angular/platform-browser';
88
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
99
import {MatFormFieldModule} from '@angular/material/form-field';
10+
import {Subject} from 'rxjs';
1011
import {MatChipInput, MatChipInputEvent} from './chip-input';
1112
import {MatChipsModule} from './index';
1213
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
@@ -27,7 +28,10 @@ describe('MatChipInput', () => {
2728
declarations: [TestChipInput],
2829
providers: [{
2930
provide: Directionality, useFactory: () => {
30-
return {value: dir.toLowerCase()};
31+
return {
32+
value: dir.toLowerCase(),
33+
change: new Subject()
34+
};
3135
}
3236
}]
3337
});

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {FormControl, FormsModule, NgForm, ReactiveFormsModule, Validators} from
3535
import {MatFormFieldModule} from '@angular/material/form-field';
3636
import {By} from '@angular/platform-browser';
3737
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
38+
import {Subject} from 'rxjs';
3839
import {MatInputModule} from '../input/index';
3940
import {MatChip} from './chip';
4041
import {MatChipInputEvent} from './chip-input';
@@ -50,6 +51,7 @@ describe('MatChipList', () => {
5051
let chips: QueryList<MatChip>;
5152
let manager: FocusKeyManager<MatChip>;
5253
let zone: MockNgZone;
54+
let dirChange: Subject<Direction>;
5355

5456
describe('StandardChipList', () => {
5557
describe('basic behaviors', () => {
@@ -422,6 +424,38 @@ describe('MatChipList', () => {
422424
expect(chipListInstance._tabIndex).toBe(4, 'Expected tabIndex to be reset back to 4');
423425
}));
424426
});
427+
428+
it('should account for the direction changing', () => {
429+
setupStandardList();
430+
manager = chipListInstance._keyManager;
431+
432+
let nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
433+
let firstNativeChip = nativeChips[0] as HTMLElement;
434+
435+
let RIGHT_EVENT: KeyboardEvent =
436+
createKeyboardEvent('keydown', RIGHT_ARROW, firstNativeChip);
437+
let array = chips.toArray();
438+
let firstItem = array[0];
439+
440+
firstItem.focus();
441+
expect(manager.activeItemIndex).toBe(0);
442+
443+
chipListInstance._keydown(RIGHT_EVENT);
444+
chipListInstance._blur();
445+
fixture.detectChanges();
446+
447+
expect(manager.activeItemIndex).toBe(1);
448+
449+
dirChange.next('rtl');
450+
fixture.detectChanges();
451+
452+
chipListInstance._keydown(RIGHT_EVENT);
453+
chipListInstance._blur();
454+
fixture.detectChanges();
455+
456+
expect(manager.activeItemIndex).toBe(0);
457+
});
458+
425459
});
426460
});
427461

@@ -1239,8 +1273,12 @@ describe('MatChipList', () => {
12391273
}
12401274

12411275
function setupStandardList(direction: Direction = 'ltr') {
1276+
dirChange = new Subject();
12421277
fixture = createComponent(StandardChipList, [{
1243-
provide: Directionality, useFactory: () => ({value: direction.toLowerCase()})
1278+
provide: Directionality, useFactory: () => ({
1279+
value: direction.toLowerCase(),
1280+
change: dirChange
1281+
})
12441282
}]);
12451283
fixture.detectChanges();
12461284

src/lib/chips/chip-list.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
352352
.withVerticalOrientation()
353353
.withHorizontalOrientation(this._dir ? this._dir.value : 'ltr');
354354

355+
if (this._dir) {
356+
this._dir.change
357+
.pipe(takeUntil(this._destroyed))
358+
.subscribe(dir => this._keyManager.withHorizontalOrientation(dir));
359+
}
360+
355361
// Prevents the chip list from capturing focus and redirecting
356362
// it back to the first chip when the user tabs out.
357363
this._keyManager.tabOut.pipe(takeUntil(this._destroyed)).subscribe(() => {

src/lib/chips/chip.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
44
import {Component, DebugElement} from '@angular/core';
55
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
66
import {By} from '@angular/platform-browser';
7+
import {Subject} from 'rxjs';
78
import {MatChip, MatChipEvent, MatChipSelectionChange, MatChipsModule} from './index';
89

910

@@ -20,7 +21,10 @@ describe('Chips', () => {
2021
imports: [MatChipsModule],
2122
declarations: [BasicChip, SingleChip],
2223
providers: [{
23-
provide: Directionality, useFactory: () => ({value: dir})
24+
provide: Directionality, useFactory: () => ({
25+
value: dir,
26+
change: new Subject()
27+
})
2428
}]
2529
});
2630

0 commit comments

Comments
 (0)