Skip to content

Commit fcc8875

Browse files
devversionjelbourn
authored andcommitted
fix(selection-list): form control disable locks disabled property (#12113)
Fixes that the `setDisabledState` control value accessor hook updates the disabled state of each option individually. This causes the `MatSelectionList#disabled` property to no longer have any effect. Fixes #12107
1 parent a248c18 commit fcc8875

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/lib/list/selection-list.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,26 +840,53 @@ describe('MatSelectionList with forms', () => {
840840
describe('and formControl', () => {
841841
let fixture: ComponentFixture<SelectionListWithFormControl>;
842842
let listOptions: MatListOption[];
843+
let selectionList: MatSelectionList;
843844

844845
beforeEach(() => {
845846
fixture = TestBed.createComponent(SelectionListWithFormControl);
846847
fixture.detectChanges();
847848

849+
selectionList = fixture.debugElement.query(By.directive(MatSelectionList)).componentInstance;
848850
listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
849851
.map(optionDebugEl => optionDebugEl.componentInstance);
850852
});
851853

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

856860
fixture.componentInstance.formControl.disable();
857861
fixture.detectChanges();
858862

863+
expect(selectionList.disabled)
864+
.toBe(true, 'Expected the selection list to be disabled.');
859865
expect(listOptions.every(option => option.disabled))
860866
.toBe(true, 'Expected every list option to be disabled.');
861867
});
862868

869+
it('should be able to update the disabled property after form control disabling', () => {
870+
expect(listOptions.every(option => !option.disabled))
871+
.toBe(true, 'Expected every list option to be enabled.');
872+
873+
fixture.componentInstance.formControl.disable();
874+
fixture.detectChanges();
875+
876+
expect(listOptions.every(option => option.disabled))
877+
.toBe(true, 'Expected every list option to be disabled.');
878+
879+
// Previously the selection list has been disabled through FormControl#disable. Now we
880+
// want to verify that we can still change the disabled state through updating the disabled
881+
// property. Calling FormControl#disable should not lock the disabled property.
882+
// See: https://github.com/angular/material2/issues/12107
883+
selectionList.disabled = false;
884+
fixture.detectChanges();
885+
886+
expect(listOptions.every(option => !option.disabled))
887+
.toBe(true, 'Expected every list option to be enabled.');
888+
});
889+
863890
it('should be able to set the value through the form control', () => {
864891
expect(listOptions.every(option => !option.selected))
865892
.toBe(true, 'Expected every list option to be unselected.');

src/lib/list/selection-list.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
461461

462462
/** Implemented as a part of ControlValueAccessor. */
463463
setDisabledState(isDisabled: boolean): void {
464-
if (this.options) {
465-
this.options.forEach(option => option.disabled = isDisabled);
466-
}
464+
this.disabled = isDisabled;
467465
}
468466

469467
/** Implemented as part of ControlValueAccessor. */

0 commit comments

Comments
 (0)