Skip to content
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

fix(chips): chip list disabled state out of sync when swapping out form group with a disabled one #17993

Merged
merged 1 commit into from
Apr 20, 2020
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
55 changes: 54 additions & 1 deletion src/material/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ import {
ChangeDetectionStrategy,
} from '@angular/core';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {FormControl, FormsModule, NgForm, ReactiveFormsModule, Validators} from '@angular/forms';
import {
FormControl,
FormsModule,
NgForm,
ReactiveFormsModule,
Validators,
FormGroup,
FormBuilder,
} from '@angular/forms';
import {MatFormFieldModule} from '@angular/material/form-field';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -935,6 +943,23 @@ describe('MatChipList', () => {
.toBeFalsy(`Expected chip with the old value not to be selected.`);
});
});

it('should keep the disabled state in sync if the form group is swapped and ' +
'disabled at the same time', fakeAsync(() => {
fixture = createComponent(ChipListInsideDynamicFormGroup);
fixture.detectChanges();
const instance = fixture.componentInstance;
const list: MatChipList = instance.chipList;

expect(list.disabled).toBe(false);
expect(list.chips.toArray().every(chip => chip.disabled)).toBe(false);

instance.assignGroup(true);
fixture.detectChanges();

expect(list.disabled).toBe(true);
expect(list.chips.toArray().every(chip => chip.disabled)).toBe(true);
}));
});

describe('chip list with chip input', () => {
Expand Down Expand Up @@ -1642,3 +1667,31 @@ class ChipListWithRemove {
class PreselectedChipInsideOnPush {
control = new FormControl('Pizza');
}


@Component({
template: `
<form [formGroup]="form">
<mat-form-field>
<mat-chip-list formControlName="control">
<mat-chip>Pizza</mat-chip>
<mat-chip>Pasta</mat-chip>
</mat-chip-list>
</mat-form-field>
</form>
`
})
class ChipListInsideDynamicFormGroup {
@ViewChild(MatChipList) chipList: MatChipList;
form: FormGroup;

constructor(private _formBuilder: FormBuilder) {
this.assignGroup(false);
}

assignGroup(isDisabled: boolean) {
this.form = this._formBuilder.group({
control: {value: [], disabled: isDisabled}
});
}
}
4 changes: 4 additions & 0 deletions src/material/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
// that whatever logic is in here has to be super lean or we risk destroying the performance.
this.updateErrorState();

if (this.ngControl.disabled !== this._disabled) {
this.disabled = !!this.ngControl.disabled;
}
}
}

Expand Down