Skip to content

fix(material/list): fix selection-list staying in tab order when disabled #25735

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 1 commit into from
Oct 12, 2022
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
5 changes: 4 additions & 1 deletion src/material/list/list-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export abstract class MatListBase {
}
private _disableRipple: boolean = false;

/** Whether all list items are disabled. */
/**
* Whether the entire list is disabled. When disabled, the list itself and each of its list items
* are disabled.
*/
@Input()
get disabled(): boolean {
return this._disabled;
Expand Down
38 changes: 38 additions & 0 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,29 @@ describe('MDC-based MatSelectionList without forms', () => {
.withContext('Expected ripples of list option to be enabled')
.toBe(false);
});

// when the entire list is disabled, its listitems should always have tabindex="-1"
it('should not put listitems in the tab order', () => {
fixture.componentInstance.disabled = false;
let testListItem = listOption[2].injector.get<MatListOption>(MatListOption);
testListItem.focus();
fixture.detectChanges();

expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') === '0').length,
)
.withContext('Expected at least one list option to be in the tab order')
.toBeGreaterThanOrEqual(1);

fixture.componentInstance.disabled = true;
fixture.detectChanges();

expect(
listOption.filter(option => option.nativeElement.getAttribute('tabindex') !== '-1').length,
)
.withContext('Expected all list options to be excluded from the tab order')
.toBe(0);
});
});

describe('with checkbox position after', () => {
Expand Down Expand Up @@ -1373,12 +1396,20 @@ describe('MDC-based MatSelectionList with forms', () => {
});

it('should be able to disable options from the control', () => {
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',
),
)
.withContext('Expected one list item to be in the tab order')
.toBe(true);

fixture.componentInstance.formControl.disable();
fixture.detectChanges();
Expand All @@ -1389,6 +1420,13 @@ describe('MDC-based 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',
),
)
.withContext('Expected every list option to be removed from the tab order')
.toBe(true);
});

it('should be able to update the disabled property after form control disabling', () => {
Expand Down
34 changes: 32 additions & 2 deletions src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ export class MatSelectionList
this.disabled = isDisabled;
}

/**
* Whether the *entire* selection list is disabled. When true, each list item is also disabled
* and each list item is removed from the tab order (has tabindex="-1").
*/
@Input()
override get disabled(): boolean {
return this._selectionListDisabled;
}
override set disabled(value: BooleanInput) {
// Update the disabled state of this list. Write to `this._selectionListDisabled` instead of
// `super.disabled`. That is to avoid closure compiler compatibility issues with assigning to
// a super property.
this._selectionListDisabled = coerceBooleanProperty(value);
if (this._selectionListDisabled) {
this._keyManager?.setActiveItem(-1);
}
}
private _selectionListDisabled = false;

/** Implemented as part of ControlValueAccessor. */
registerOnChange(fn: (value: any) => void): void {
this._onChange = fn;
Expand Down Expand Up @@ -365,13 +384,24 @@ export class MatSelectionList
}
};

/** Sets up the logic for maintaining the roving tabindex. */
/**
* Sets up the logic for maintaining the roving tabindex.
*
* `skipPredicate` determines if key manager should avoid putting a given list item in the tab
* index. Allow disabled list items to receive focus to align with WAI ARIA recommendation.
* Normally WAI ARIA's instructions are to exclude disabled items from the tab order, but it
* makes a few exceptions for compound widgets.
*
* From [Developing a Keyboard Interface](
* https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/):
* "For the following composite widget elements, keep them focusable when disabled: Options in a
* Listbox..."
*/
private _setupRovingTabindex() {
this._keyManager = new FocusKeyManager(this._items)
.withHomeAndEnd()
.withTypeAhead()
.withWrap()
// Allow navigation to disabled items.
.skipPredicate(() => false);

// Set the initial focus.
Expand Down
4 changes: 3 additions & 1 deletion tools/public_api_guard/material/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export class MatSelectionList extends MatListBase implements SelectionList, Cont
color: ThemePalette;
compareWith: (o1: any, o2: any) => boolean;
deselectAll(): MatListOption[];
get disabled(): boolean;
set disabled(value: BooleanInput);
// (undocumented)
_element: ElementRef<HTMLElement>;
_emitChangeEvent(options: MatListOption[]): void;
Expand Down Expand Up @@ -243,7 +245,7 @@ export class MatSelectionList extends MatListBase implements SelectionList, Cont
_value: string[] | null;
writeValue(values: string[]): void;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatSelectionList, "mat-selection-list", ["matSelectionList"], { "color": "color"; "compareWith": "compareWith"; "multiple": "multiple"; }, { "selectionChange": "selectionChange"; }, ["_items"], ["*"], false, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSelectionList, "mat-selection-list", ["matSelectionList"], { "color": "color"; "compareWith": "compareWith"; "multiple": "multiple"; "disabled": "disabled"; }, { "selectionChange": "selectionChange"; }, ["_items"], ["*"], false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSelectionList, never>;
}
Expand Down