Skip to content

fix(material-experimental/mdc-paginator): target page size label with aria-labelledby #23172

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
Jul 23, 2021
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
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-paginator/paginator.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="mat-mdc-paginator-outer-container">
<div class="mat-mdc-paginator-container">
<div class="mat-mdc-paginator-page-size" *ngIf="!hidePageSize">
<div class="mat-mdc-paginator-page-size-label">
<div class="mat-mdc-paginator-page-size-label" id="{{_pageSizeLabelId}}">
{{_intl.itemsPerPageLabel}}
</div>

Expand All @@ -13,7 +13,7 @@
<mat-select
[value]="pageSize"
[disabled]="disabled"
[aria-label]="_intl.itemsPerPageLabel"
[aria-labelledby]="_pageSizeLabelId"
(selectionChange)="_changePageSize($event.value)">
<mat-option *ngFor="let pageSizeOption of _displayedPageSizeOptions" [value]="pageSizeOption">
{{pageSizeOption}}
Expand Down
9 changes: 7 additions & 2 deletions src/material-experimental/mdc-paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ describe('MDC-based MatPaginator', () => {

it('should show right aria-labels for select and buttons', () => {
const fixture = createComponent(MatPaginatorApp);
const select = fixture.nativeElement.querySelector('.mat-mdc-select');
expect(select.getAttribute('aria-label')).toBe('Items per page:');

expect(getPreviousButton(fixture).getAttribute('aria-label')).toBe('Previous page');
expect(getNextButton(fixture).getAttribute('aria-label')).toBe('Next page');

const select = fixture.nativeElement.querySelector('.mat-mdc-select');
const selectLabelIds = select.getAttribute('aria-labelledby')?.split(/\s/g) as string[];
const selectLabelTexts = selectLabelIds?.map(labelId => {
return fixture.nativeElement.querySelector(`#${labelId}`)?.textContent?.trim();
});
expect(selectLabelTexts).toContain('Items per page:');
});

it('should re-render when the i18n labels change', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/material-experimental/mdc-paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface MatPaginatorDefaultOptions {
export const MAT_PAGINATOR_DEFAULT_OPTIONS =
new InjectionToken<MatPaginatorDefaultOptions>('MAT_PAGINATOR_DEFAULT_OPTIONS');

let nextUniqueId = 0;

/**
* Component to provide navigation between paged information. Displays the size of the current
* page, user-selectable options to change that size, what items are being shown, and
Expand All @@ -66,6 +68,10 @@ export class MatPaginator extends _MatPaginatorBase<MatPaginatorDefaultOptions>
/** If set, styles the "page size" form field with the designated style. */
_formFieldAppearance?: MatFormFieldAppearance;

/** ID for the DOM node containing the pagiators's items per page label. */
readonly _pageSizeLabelId = `mat-paginator-page-size-label-${nextUniqueId++}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might shorten this to something like mat-pgntr-x just to shave off a few bytes



constructor(intl: MatPaginatorIntl,
changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(MAT_PAGINATOR_DEFAULT_OPTIONS) defaults?: MatPaginatorDefaultOptions) {
Expand Down