Skip to content

Commit

Permalink
fix(material/list): option views not change detected when control is …
Browse files Browse the repository at this point in the history
…disabled (angular#30532)

Fixes that we weren't change detecting the view of the selection list options when the form control for the entire list is disabled. The visual difference is subtle for unchecked options, but it's noticeable for checked ones.

Fixes angular#30522.
  • Loading branch information
crisbeto authored Feb 21, 2025
1 parent 7ef405c commit 8b2b944
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1540,20 +1540,24 @@ describe('MatSelectionList with forms', () => {
});

it('should be able to disable options from the control', () => {
const optionElements = listOptions.map(option => option._elementRef.nativeElement);
const inputs = optionElements.map(element => element.querySelector('input')!);
selectionList.focus();
expect(selectionList.disabled)
.withContext('Expected the selection list to be enabled.')
.toBe(false);
expect(listOptions.every(option => !option.disabled))
.withContext('Expected every list option to be enabled.')
.toBe(true);
expect(
listOptions.some(
option => option._elementRef.nativeElement.getAttribute('tabindex') === '0',
),
)
expect(optionElements.some(el => el.getAttribute('tabindex') === '0'))
.withContext('Expected one list item to be in the tab order')
.toBe(true);
// Note: assert the disabled of the inner inputs, because they're placed inside of the
// view of the individual options which is detected separately from the host. The tabindex
// check above isn't enough, because it doesn't go through change detection.
expect(inputs.every(input => !input.disabled))
.withContext('Expected all options to be enabled')
.toBe(true);

fixture.componentInstance.formControl.disable();
fixture.detectChanges();
Expand All @@ -1564,13 +1568,12 @@ describe('MatSelectionList with forms', () => {
expect(listOptions.every(option => option.disabled))
.withContext('Expected every list option to be disabled.')
.toBe(true);
expect(
listOptions.every(
option => option._elementRef.nativeElement.getAttribute('tabindex') === '-1',
),
)
expect(optionElements.every(el => el.getAttribute('tabindex') === '-1'))
.withContext('Expected every list option to be removed from the tab order')
.toBe(true);
expect(inputs.every(input => input.disabled))
.withContext('Expected all options to be disabled')
.toBe(true);
});

it('should be able to update the disabled property after form control disabling', () => {
Expand Down
1 change: 1 addition & 0 deletions src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export class MatSelectionList
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
this._changeDetectorRef.markForCheck();
this._markOptionsForCheck();
}

/**
Expand Down

0 comments on commit 8b2b944

Please sign in to comment.