Skip to content
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

Fixed #16152 - ListBox component accessibility issues #16183

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
40 changes: 18 additions & 22 deletions src/app/components/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,7 @@ export const LISTBOX_VALUE_ACCESSOR: any = {
selector: 'p-listbox',
template: `
<div [attr.id]="id" [ngClass]="containerClass" [ngStyle]="style" [class]="styleClass" (focusout)="onFocusout($event)">
<span
#firstHiddenFocusableElement
role="presentation"
[attr.aria-hidden]="true"
class="p-hidden-accessible p-hidden-focusable"
[tabindex]="!disabled ? tabindex : -1"
(focus)="onFirstHiddenFocus($event)"
[attr.data-p-hidden-focusable]="true"
>
</span>
<span #firstHiddenFocusableElement role="presentation" class="p-hidden-accessible p-hidden-focusable" [tabindex]="!disabled ? tabindex : -1" (focus)="onFirstHiddenFocus($event)" [attr.data-p-hidden-focusable]="true"> </span>
<div class="p-listbox-header" *ngIf="headerFacet || headerTemplate">
<ng-content select="p-header"></ng-content>
<ng-container *ngTemplateOutlet="headerTemplate; context: { $implicit: modelValue(), options: visibleOptions() }"></ng-container>
Expand Down Expand Up @@ -105,6 +96,7 @@ export const LISTBOX_VALUE_ACCESSOR: any = {
[tabindex]="!disabled && !focused ? tabindex : -1"
(input)="onFilterChange($event)"
(keydown)="onFilterKeyDown($event)"
(focus)="onFilterFocus($event)"
(blur)="onFilterBlur($event)"
/>
<SearchIcon *ngIf="!filterIconTemplate" [styleClass]="'p-listbox-filter-icon'" [attr.aria-hidden]="true" />
Expand All @@ -117,18 +109,18 @@ export const LISTBOX_VALUE_ACCESSOR: any = {
</span>
</ng-template>
</div>
<div [ngClass]="'p-listbox-list-wrapper'" [ngStyle]="listStyle" [class]="listStyleClass" [style.max-height]="virtualScroll ? 'auto' : scrollHeight || 'auto'">
<div #wrapper [ngClass]="'p-listbox-list-wrapper'" [ngStyle]="listStyle" [class]="listStyleClass" [style.max-height]="virtualScroll ? 'auto' : scrollHeight || 'auto'" [attr.tabindex]="!disabled && '0'">
<p-scroller
#scroller
*ngIf="virtualScroll"
[items]="visibleOptions()"
[style]="{ height: scrollHeight }"
[itemSize]="virtualScrollItemSize"
[autoSize]="true"
[tabindex]="-1"
[lazy]="lazy"
[options]="virtualScrollOptions"
(onLazyLoad)="onLazyLoad.emit($event)"
[tabindex]="scrollerTabIndex"
>
<ng-template pTemplate="content" let-items let-scrollerOptions="options">
<ng-container *ngTemplateOutlet="buildInItems; context: { $implicit: items, options: scrollerOptions }"></ng-container>
Expand Down Expand Up @@ -226,16 +218,7 @@ export const LISTBOX_VALUE_ACCESSOR: any = {
<span role="status" aria-live="polite" class="p-hidden-accessible">
{{ selectedMessageText }}
</span>
<span
#lastHiddenFocusableElement
role="presentation"
[attr.aria-hidden]="true"
class="p-hidden-accessible p-hidden-focusable"
[tabindex]="!disabled ? tabindex : -1"
(focus)="onLastHiddenFocus($event)"
[attr.data-p-hidden-focusable]="true"
>
</span>
<span #lastHiddenFocusableElement role="presentation" class="p-hidden-accessible p-hidden-focusable" [tabindex]="!disabled ? tabindex : -1" (focus)="onLastHiddenFocus($event)" [attr.data-p-hidden-focusable]="true"> </span>
</div>
`,
providers: [LISTBOX_VALUE_ACCESSOR],
Expand Down Expand Up @@ -545,6 +528,8 @@ export class Listbox implements AfterContentInit, OnInit, ControlValueAccessor,

@ViewChild('list') listViewChild: Nullable<ElementRef>;

@ViewChild('wrapper') wrapperViewChild: Nullable<ElementRef>;

@ContentChild(Header) headerFacet: Nullable<TemplateRef<any>>;

@ContentChild(Footer) footerFacet: Nullable<TemplateRef<any>>;
Expand Down Expand Up @@ -593,6 +578,8 @@ export class Listbox implements AfterContentInit, OnInit, ControlValueAccessor,

focused: boolean | undefined;

scrollerTabIndex: string = '0';

get containerClass() {
return {
'p-listbox p-component': true,
Expand Down Expand Up @@ -949,6 +936,8 @@ export class Listbox implements AfterContentInit, OnInit, ControlValueAccessor,
onFocusout(event: FocusEvent) {
if (!this.el.nativeElement.contains(event.relatedTarget) && this.lastHiddenFocusableElement && this.firstHiddenFocusableElement) {
this.firstHiddenFocusableElement.nativeElement.tabIndex = this.lastHiddenFocusableElement.nativeElement.tabIndex = undefined;
this.wrapperViewChild.nativeElement.tabIndex = '0';
this.scrollerTabIndex = '0';
}
}

Expand All @@ -957,6 +946,9 @@ export class Listbox implements AfterContentInit, OnInit, ControlValueAccessor,
const focusedOptionIndex = this.focusedOptionIndex() !== -1 ? this.focusedOptionIndex() : this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
this.focusedOptionIndex.set(focusedOptionIndex);
this.onFocus.emit(event);

this.wrapperViewChild.nativeElement.tabIndex = '-1';
this.scrollerTabIndex = '-1';
}

onListBlur(event: FocusEvent) {
Expand Down Expand Up @@ -1010,6 +1002,10 @@ export class Listbox implements AfterContentInit, OnInit, ControlValueAccessor,

!this.virtualScrollerDisabled && this.scroller.scrollToIndex(0);
}

onFilterFocus(event: FocusEvent){
this.wrapperViewChild.nativeElement.tabIndex = '-1';
}

onFilterBlur(event: FocusEvent) {
this.focusedOptionIndex.set(-1);
Expand Down
Loading