Skip to content

Commit 8b2b944

Browse files
authored
fix(material/list): option views not change detected when control is 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.
1 parent 7ef405c commit 8b2b944

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,20 +1540,24 @@ describe('MatSelectionList with forms', () => {
15401540
});
15411541

15421542
it('should be able to disable options from the control', () => {
1543+
const optionElements = listOptions.map(option => option._elementRef.nativeElement);
1544+
const inputs = optionElements.map(element => element.querySelector('input')!);
15431545
selectionList.focus();
15441546
expect(selectionList.disabled)
15451547
.withContext('Expected the selection list to be enabled.')
15461548
.toBe(false);
15471549
expect(listOptions.every(option => !option.disabled))
15481550
.withContext('Expected every list option to be enabled.')
15491551
.toBe(true);
1550-
expect(
1551-
listOptions.some(
1552-
option => option._elementRef.nativeElement.getAttribute('tabindex') === '0',
1553-
),
1554-
)
1552+
expect(optionElements.some(el => el.getAttribute('tabindex') === '0'))
15551553
.withContext('Expected one list item to be in the tab order')
15561554
.toBe(true);
1555+
// Note: assert the disabled of the inner inputs, because they're placed inside of the
1556+
// view of the individual options which is detected separately from the host. The tabindex
1557+
// check above isn't enough, because it doesn't go through change detection.
1558+
expect(inputs.every(input => !input.disabled))
1559+
.withContext('Expected all options to be enabled')
1560+
.toBe(true);
15571561

15581562
fixture.componentInstance.formControl.disable();
15591563
fixture.detectChanges();
@@ -1564,13 +1568,12 @@ describe('MatSelectionList with forms', () => {
15641568
expect(listOptions.every(option => option.disabled))
15651569
.withContext('Expected every list option to be disabled.')
15661570
.toBe(true);
1567-
expect(
1568-
listOptions.every(
1569-
option => option._elementRef.nativeElement.getAttribute('tabindex') === '-1',
1570-
),
1571-
)
1571+
expect(optionElements.every(el => el.getAttribute('tabindex') === '-1'))
15721572
.withContext('Expected every list option to be removed from the tab order')
15731573
.toBe(true);
1574+
expect(inputs.every(input => input.disabled))
1575+
.withContext('Expected all options to be disabled')
1576+
.toBe(true);
15741577
});
15751578

15761579
it('should be able to update the disabled property after form control disabling', () => {

src/material/list/selection-list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export class MatSelectionList
256256
setDisabledState(isDisabled: boolean): void {
257257
this.disabled = isDisabled;
258258
this._changeDetectorRef.markForCheck();
259+
this._markOptionsForCheck();
259260
}
260261

261262
/**

0 commit comments

Comments
 (0)