Skip to content

fix(selection-list): form control disable locks disabled property #12113

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
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
27 changes: 27 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,26 +840,53 @@ describe('MatSelectionList with forms', () => {
describe('and formControl', () => {
let fixture: ComponentFixture<SelectionListWithFormControl>;
let listOptions: MatListOption[];
let selectionList: MatSelectionList;

beforeEach(() => {
fixture = TestBed.createComponent(SelectionListWithFormControl);
fixture.detectChanges();

selectionList = fixture.debugElement.query(By.directive(MatSelectionList)).componentInstance;
listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.componentInstance);
});

it('should be able to disable options from the control', () => {
expect(selectionList.disabled)
.toBe(false, 'Expected the selection list to be enabled.');
expect(listOptions.every(option => !option.disabled))
.toBe(true, 'Expected every list option to be enabled.');

fixture.componentInstance.formControl.disable();
fixture.detectChanges();

expect(selectionList.disabled)
.toBe(true, 'Expected the selection list to be disabled.');
expect(listOptions.every(option => option.disabled))
.toBe(true, 'Expected every list option to be disabled.');
});

it('should be able to update the disabled property after form control disabling', () => {
expect(listOptions.every(option => !option.disabled))
.toBe(true, 'Expected every list option to be enabled.');

fixture.componentInstance.formControl.disable();
fixture.detectChanges();

expect(listOptions.every(option => option.disabled))
.toBe(true, 'Expected every list option to be disabled.');

// Previously the selection list has been disabled through FormControl#disable. Now we
// want to verify that we can still change the disabled state through updating the disabled
// property. Calling FormControl#disable should not lock the disabled property.
// See: https://github.com/angular/material2/issues/12107
selectionList.disabled = false;
fixture.detectChanges();

expect(listOptions.every(option => !option.disabled))
.toBe(true, 'Expected every list option to be enabled.');
});

it('should be able to set the value through the form control', () => {
expect(listOptions.every(option => !option.selected))
.toBe(true, 'Expected every list option to be unselected.');
Expand Down
4 changes: 1 addition & 3 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu

/** Implemented as a part of ControlValueAccessor. */
setDisabledState(isDisabled: boolean): void {
if (this.options) {
this.options.forEach(option => option.disabled = isDisabled);
}
this.disabled = isDisabled;
}

/** Implemented as part of ControlValueAccessor. */
Expand Down