Skip to content

fix(material/chips): initial value from forms not reflected in the view #26052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/material/chips/chip-listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,30 +737,35 @@ describe('MDC-based MatChipListbox', () => {
});

describe('multiple selection', () => {
beforeEach(() => {
fixture = createComponent(MultiSelectionChipListbox);
chips = fixture.componentInstance.chips;
});

it('should take an initial view value with reactive forms', () => {
fixture.componentInstance.control = new FormControl(['pizza-1']);
it('should take an initial view value with reactive forms', fakeAsync(() => {
fixture = createComponent(MultiSelectionChipListbox, undefined, initFixture => {
initFixture.componentInstance.control = new FormControl(['pizza-1', 'pasta-6']);
initFixture.componentInstance.selectable = true;
});
fixture.detectChanges();
flush();

const array = chips.toArray();
const array = fixture.componentInstance.chips.toArray();

expect(array[1].selected).withContext('Expect pizza-1 chip to be selected').toBeTruthy();
expect(array[1].selected).withContext('Expect pizza-1 chip to be selected').toBe(true);
expect(array[6].selected).withContext('Expect pasta-6 chip to be selected').toBe(true);

dispatchKeyboardEvent(primaryActions[1], 'keydown', SPACE);
fixture.detectChanges();
flush();

expect(array[1].selected)
.withContext('Expect chip to be not selected after toggle selected')
.toBeFalsy();
});
.withContext('Expect pizza-1 chip to no longer be selected')
.toBe(false);
expect(array[6].selected)
.withContext('Expect pasta-6 chip to remain selected')
.toBe(true);
}));

it('should set the view value from the form', () => {
fixture = createComponent(MultiSelectionChipListbox);
const chipListbox = fixture.componentInstance.chipListbox;
const array = chips.toArray();
const array = fixture.componentInstance.chips.toArray();

expect(chipListbox.value)
.withContext('Expect chip listbox to have no initial value')
Expand All @@ -773,6 +778,8 @@ describe('MDC-based MatChipListbox', () => {
});

it('should update the form value when the view changes', () => {
fixture = createComponent(MultiSelectionChipListbox);

expect(fixture.componentInstance.control.value)
.withContext(`Expected the control's value to be empty initially.`)
.toEqual(null);
Expand All @@ -786,8 +793,10 @@ describe('MDC-based MatChipListbox', () => {
});

it('should clear the selection when a nonexistent option value is selected', () => {
const array = chips.toArray();
fixture = createComponent(MultiSelectionChipListbox);
chips = fixture.componentInstance.chips;

const array = fixture.componentInstance.chips.toArray();
fixture.componentInstance.control.setValue(['pizza-1']);
fixture.detectChanges();

Expand All @@ -805,7 +814,8 @@ describe('MDC-based MatChipListbox', () => {
});

it('should clear the selection when the control is reset', () => {
const array = chips.toArray();
fixture = createComponent(MultiSelectionChipListbox);
const array = fixture.componentInstance.chips.toArray();

fixture.componentInstance.control.setValue(['pizza-1']);
fixture.detectChanges();
Expand All @@ -824,6 +834,7 @@ describe('MDC-based MatChipListbox', () => {
function createComponent<T>(
component: Type<T>,
direction: Direction = 'ltr',
beforeInitialChangeDetection?: (fixture: ComponentFixture<T>) => void,
): ComponentFixture<T> {
directionality = {
value: direction,
Expand All @@ -840,6 +851,7 @@ describe('MDC-based MatChipListbox', () => {
}).compileComponents();

fixture = TestBed.createComponent<T>(component);
beforeInitialChangeDetection?.(fixture);
fixture.detectChanges();

chipListboxDebugElement = fixture.debugElement.query(By.directive(MatChipListbox))!;
Expand Down Expand Up @@ -926,7 +938,7 @@ class MultiSelectionChipListbox {
{value: 'pasta-6', viewValue: 'Pasta'},
{value: 'sushi-7', viewValue: 'Sushi'},
];
control = new FormControl<string | null>(null);
control = new FormControl<string[] | null>(null);
isRequired: boolean;
tabIndexOverride: number;
selectable: boolean;
Expand Down
12 changes: 12 additions & 0 deletions src/material/chips/chip-listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class MatChipListbox
// TODO: MDC uses `grid` here
protected override _defaultRole = 'listbox';

/** Value that was assigned before the listbox was initialized. */
private _pendingInitialValue: any;

/** Whether the user should be allowed to select multiple chips. */
@Input()
get multiple(): boolean {
Expand Down Expand Up @@ -188,6 +191,13 @@ export class MatChipListbox
override _chips: QueryList<MatChipOption>;

ngAfterContentInit() {
if (this._pendingInitialValue !== undefined) {
Promise.resolve().then(() => {
this._setSelectionByValue(this._pendingInitialValue, false);
this._pendingInitialValue = undefined;
});
}

this._chips.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
// Update listbox selectable/multiple properties on chips
this._syncListboxProperties();
Expand Down Expand Up @@ -236,6 +246,8 @@ export class MatChipListbox
writeValue(value: any): void {
if (this._chips) {
this._setSelectionByValue(value, false);
} else if (value != null) {
this._pendingInitialValue = value;
}
}

Expand Down