Skip to content

Commit 8c9ce52

Browse files
committed
fix(selection-list): incorrect cursor if disabled
* Currently if a list option is disabled through the `[disabled]` binding, the disabled option still has the `pointer` cursor. * Fixes that list options are still focusable if disabled. * Fixes that the selection list is still focusable if disabled. Fixes #9952
1 parent d84612e commit 8c9ce52

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

src/demo-app/list/list-demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ <h2>Selection list</h2>
109109

110110
<mat-selection-list #groceries [ngModel]="selectedOptions"
111111
(ngModelChange)="onSelectedOptionsChange($event)"
112-
(change)="changeEventCount = changeEventCount + 1">
112+
(change)="changeEventCount = changeEventCount + 1"
113+
[disabled]="selectionListDisabled">
113114
<h3 mat-subheader>Groceries</h3>
114115

115116
<mat-list-option value="bananas" checkboxPosition="before">Bananas</mat-list-option>
@@ -121,6 +122,7 @@ <h3 mat-subheader>Groceries</h3>
121122
<p>Selected: {{selectedOptions | json}}</p>
122123
<p>Change Event Count {{changeEventCount}}</p>
123124
<p>Model Change Event Count {{modelChangeEventCount}}</p>
125+
<mat-checkbox [(ngModel)]="selectionListDisabled">Disable Selection List</mat-checkbox>
124126

125127
<p>
126128
<button mat-raised-button (click)="groceries.selectAll()">Select all</button>

src/demo-app/list/list-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class ListDemo {
5959

6060
thirdLine: boolean = false;
6161
infoClicked: boolean = false;
62+
selectionListDisabled: boolean = false;
6263

6364
selectedOptions: string[] = ['apples'];
6465
changeEventCount: number = 0;

src/lib/list/list.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ $mat-list-item-inset-divider-offset: 72px;
254254
}
255255

256256
.mat-list-option {
257-
&:not([disabled]) {
257+
&:not(.mat-list-item-disabled) {
258258
cursor: pointer;
259259
}
260260
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ describe('MatSelectionList without forms', () => {
522522

523523
expect(listOptionEl.classList).toContain('mat-list-item-disabled');
524524
});
525+
526+
it('should remove the tabindex attribute', () => {
527+
listOption.disabled = true;
528+
fixture.detectChanges();
529+
530+
expect(listOptionEl.hasAttribute('tabindex')).toBe(false,
531+
'Expected tabindex attribute to be removed if option is disabled.');
532+
});
525533
});
526534

527535
describe('with list disabled', () => {
@@ -564,6 +572,11 @@ describe('MatSelectionList without forms', () => {
564572

565573
expect(selectList.selected.length).toBe(0);
566574
});
575+
576+
it('should remove the tabindex attribute', () => {
577+
expect(selectionList.nativeElement.hasAttribute('tabindex')).toBe(false,
578+
'Expected tabindex attribute to be removed if selection list is disabled.');
579+
});
567580
});
568581

569582
describe('with checkbox position after', () => {

src/lib/list/selection-list.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ export class MatSelectionListChange {
9797
'(focus)': '_handleFocus()',
9898
'(blur)': '_handleBlur()',
9999
'(click)': '_handleClick()',
100-
'tabindex': '-1',
101100
'[class.mat-list-item-disabled]': 'disabled',
102101
'[class.mat-list-item-focus]': '_hasFocus',
103102
'[attr.aria-selected]': 'selected.toString()',
104103
'[attr.aria-disabled]': 'disabled.toString()',
104+
'[attr.tabindex]': 'disabled ? null : -1'
105105
},
106106
templateUrl: 'list-option.html',
107107
encapsulation: ViewEncapsulation.None,
@@ -278,12 +278,13 @@ export class MatListOption extends _MatListOptionMixinBase
278278
inputs: ['disabled', 'disableRipple', 'tabIndex'],
279279
host: {
280280
'role': 'listbox',
281-
'[tabIndex]': 'tabIndex',
282281
'class': 'mat-selection-list',
283282
'(focus)': 'focus()',
284283
'(blur)': '_onTouched()',
285284
'(keydown)': '_keydown($event)',
286-
'[attr.aria-disabled]': 'disabled.toString()'},
285+
'[attr.aria-disabled]': 'disabled.toString()',
286+
'[attr.tabindex]': 'disabled ? null : tabIndex',
287+
},
287288
template: '<ng-content></ng-content>',
288289
styleUrls: ['list.css'],
289290
encapsulation: ViewEncapsulation.None,

0 commit comments

Comments
 (0)