Skip to content

Commit

Permalink
fix(material/paginator): ignore clicks on disabled buttons (#30138)
Browse files Browse the repository at this point in the history
The changes in #29379 made the paginator interactive while they're disabled in order to improve accessibility, but as a result it also allows for the buttons to navigate while they're disabled.

These changes add internal checks to ensure that the buttons don't navigate while disabled. I've also cleaned up the logic a bit so we don't have four different places that deal with navigations.

Fixes #30124.

(cherry picked from commit 8d3bca5)
  • Loading branch information
crisbeto committed Dec 9, 2024
1 parent 4b69162 commit 3ea8cf5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 44 deletions.
16 changes: 8 additions & 8 deletions src/material/paginator/paginator.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
@if (showFirstLastButtons) {
<button mat-icon-button type="button"
class="mat-mdc-paginator-navigation-first"
(click)="firstPage()"
(click)="_buttonClicked(0, _previousButtonsDisabled())"
[attr.aria-label]="_intl.firstPageLabel"
[matTooltip]="_intl.firstPageLabel"
[matTooltipDisabled]="_previousButtonsDisabled()"
[matTooltipPosition]="'above'"
matTooltipPosition="above"
[disabled]="_previousButtonsDisabled()"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
Expand All @@ -61,11 +61,11 @@
}
<button mat-icon-button type="button"
class="mat-mdc-paginator-navigation-previous"
(click)="previousPage()"
(click)="_buttonClicked(pageIndex - 1, _previousButtonsDisabled())"
[attr.aria-label]="_intl.previousPageLabel"
[matTooltip]="_intl.previousPageLabel"
[matTooltipDisabled]="_previousButtonsDisabled()"
[matTooltipPosition]="'above'"
matTooltipPosition="above"
[disabled]="_previousButtonsDisabled()"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
Expand All @@ -77,11 +77,11 @@
</button>
<button mat-icon-button type="button"
class="mat-mdc-paginator-navigation-next"
(click)="nextPage()"
(click)="_buttonClicked(pageIndex + 1, _nextButtonsDisabled())"
[attr.aria-label]="_intl.nextPageLabel"
[matTooltip]="_intl.nextPageLabel"
[matTooltipDisabled]="_nextButtonsDisabled()"
[matTooltipPosition]="'above'"
matTooltipPosition="above"
[disabled]="_nextButtonsDisabled()"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
Expand All @@ -94,11 +94,11 @@
@if (showFirstLastButtons) {
<button mat-icon-button type="button"
class="mat-mdc-paginator-navigation-last"
(click)="lastPage()"
(click)="_buttonClicked(getNumberOfPages() - 1, _nextButtonsDisabled())"
[attr.aria-label]="_intl.lastPageLabel"
[matTooltip]="_intl.lastPageLabel"
[matTooltipDisabled]="_nextButtonsDisabled()"
[matTooltipPosition]="'above'"
matTooltipPosition="above"
[disabled]="_nextButtonsDisabled()"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
Expand Down
72 changes: 60 additions & 12 deletions src/material/paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {dispatchMouseEvent} from '@angular/cdk/testing/private';
import {ChangeDetectorRef, Component, Provider, Type, ViewChild, inject} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
import {ThemePalette} from '@angular/material/core';
Expand Down Expand Up @@ -135,7 +134,7 @@ describe('MatPaginator', () => {
const paginator = component.paginator;
expect(paginator.pageIndex).toBe(0);

dispatchMouseEvent(getNextButton(fixture), 'click');
getNextButton(fixture).click();

expect(paginator.pageIndex).toBe(1);
expect(component.pageEvent).toHaveBeenCalledWith(
Expand All @@ -154,7 +153,7 @@ describe('MatPaginator', () => {
fixture.detectChanges();
expect(paginator.pageIndex).toBe(1);

dispatchMouseEvent(getPreviousButton(fixture), 'click');
getPreviousButton(fixture).click();

expect(paginator.pageIndex).toBe(0);
expect(component.pageEvent).toHaveBeenCalledWith(
Expand All @@ -164,12 +163,37 @@ describe('MatPaginator', () => {
}),
);
});

it('should not navigate to the next page when the paginator is disabled', () => {
const fixture = createComponent(MatPaginatorApp);
expect(fixture.componentInstance.paginator.pageIndex).toBe(0);

fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
getNextButton(fixture).click();
expect(fixture.componentInstance.paginator.pageIndex).toBe(0);
});

it('should not navigate to the previous page when the paginator is disabled', () => {
const fixture = createComponent(MatPaginatorApp);
fixture.componentInstance.pageIndex = 1;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expect(fixture.componentInstance.paginator.pageIndex).toBe(1);

fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
getPreviousButton(fixture).click();
expect(fixture.componentInstance.paginator.pageIndex).toBe(1);
});
});

it('should be able to show the first/last buttons', () => {
const fixture = createComponent(MatPaginatorApp);
expect(getFirstButton(fixture)).withContext('Expected first button to not exist.').toBeNull();

expect(getLastButton(fixture)).withContext('Expected last button to not exist.').toBeNull();

fixture.componentInstance.showFirstLastButtons = true;
Expand Down Expand Up @@ -271,7 +295,7 @@ describe('MatPaginator', () => {
it('should be able to go to the last page via the last page button', () => {
expect(paginator.pageIndex).toBe(0);

dispatchMouseEvent(getLastButton(fixture), 'click');
getLastButton(fixture).click();

expect(paginator.pageIndex).toBe(9);
expect(component.pageEvent).toHaveBeenCalledWith(
Expand All @@ -287,7 +311,7 @@ describe('MatPaginator', () => {
fixture.detectChanges();
expect(paginator.pageIndex).toBe(3);

dispatchMouseEvent(getFirstButton(fixture), 'click');
getFirstButton(fixture).click();

expect(paginator.pageIndex).toBe(0);
expect(component.pageEvent).toHaveBeenCalledWith(
Expand All @@ -305,7 +329,7 @@ describe('MatPaginator', () => {
expect(paginator.hasNextPage()).toBe(false);

component.pageEvent.calls.reset();
dispatchMouseEvent(getNextButton(fixture), 'click');
getNextButton(fixture).click();

expect(component.pageEvent).not.toHaveBeenCalled();
expect(paginator.pageIndex).toBe(9);
Expand All @@ -316,11 +340,35 @@ describe('MatPaginator', () => {
expect(paginator.hasPreviousPage()).toBe(false);

component.pageEvent.calls.reset();
dispatchMouseEvent(getPreviousButton(fixture), 'click');
getPreviousButton(fixture).click();

expect(component.pageEvent).not.toHaveBeenCalled();
expect(paginator.pageIndex).toBe(0);
});

it('should not navigate to the last page when the paginator is disabled', () => {
expect(fixture.componentInstance.paginator.pageIndex).toBe(0);

fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
getLastButton(fixture).click();
expect(fixture.componentInstance.paginator.pageIndex).toBe(0);
});

it('should not navigate to the first page when the paginator is disabled', () => {
fixture.componentInstance.pageIndex = 1;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expect(fixture.componentInstance.paginator.pageIndex).toBe(1);

fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
getFirstButton(fixture).click();
expect(fixture.componentInstance.paginator.pageIndex).toBe(1);
});
});

it('should mark for check when inputs are changed directly', () => {
Expand Down Expand Up @@ -569,19 +617,19 @@ describe('MatPaginator', () => {
});
});

function getPreviousButton(fixture: ComponentFixture<any>) {
function getPreviousButton(fixture: ComponentFixture<any>): HTMLButtonElement {
return fixture.nativeElement.querySelector('.mat-mdc-paginator-navigation-previous');
}

function getNextButton(fixture: ComponentFixture<any>) {
function getNextButton(fixture: ComponentFixture<any>): HTMLButtonElement {
return fixture.nativeElement.querySelector('.mat-mdc-paginator-navigation-next');
}

function getFirstButton(fixture: ComponentFixture<any>) {
function getFirstButton(fixture: ComponentFixture<any>): HTMLButtonElement {
return fixture.nativeElement.querySelector('.mat-mdc-paginator-navigation-first');
}

function getLastButton(fixture: ComponentFixture<any>) {
function getLastButton(fixture: ComponentFixture<any>): HTMLButtonElement {
return fixture.nativeElement.querySelector('.mat-mdc-paginator-navigation-last');
}

Expand Down
56 changes: 32 additions & 24 deletions src/material/paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,48 +241,32 @@ export class MatPaginator implements OnInit, OnDestroy {

/** Advances to the next page if it exists. */
nextPage(): void {
if (!this.hasNextPage()) {
return;
if (this.hasNextPage()) {
this._navigate(this.pageIndex + 1);
}

const previousPageIndex = this.pageIndex;
this.pageIndex = this.pageIndex + 1;
this._emitPageEvent(previousPageIndex);
}

/** Move back to the previous page if it exists. */
previousPage(): void {
if (!this.hasPreviousPage()) {
return;
if (this.hasPreviousPage()) {
this._navigate(this.pageIndex - 1);
}

const previousPageIndex = this.pageIndex;
this.pageIndex = this.pageIndex - 1;
this._emitPageEvent(previousPageIndex);
}

/** Move to the first page if not already there. */
firstPage(): void {
// hasPreviousPage being false implies at the start
if (!this.hasPreviousPage()) {
return;
if (this.hasPreviousPage()) {
this._navigate(0);
}

const previousPageIndex = this.pageIndex;
this.pageIndex = 0;
this._emitPageEvent(previousPageIndex);
}

/** Move to the last page if not already there. */
lastPage(): void {
// hasNextPage being false implies at the end
if (!this.hasNextPage()) {
return;
if (this.hasNextPage()) {
this._navigate(this.getNumberOfPages() - 1);
}

const previousPageIndex = this.pageIndex;
this.pageIndex = this.getNumberOfPages() - 1;
this._emitPageEvent(previousPageIndex);
}

/** Whether there is a previous page. */
Expand Down Expand Up @@ -369,4 +353,28 @@ export class MatPaginator implements OnInit, OnDestroy {
length: this.length,
});
}

/** Navigates to a specific page index. */
private _navigate(index: number) {
const previousIndex = this.pageIndex;

if (index !== previousIndex) {
this.pageIndex = index;
this._emitPageEvent(previousIndex);
}
}

/**
* Callback invoked when one of the navigation buttons is called.
* @param targetIndex Index to which the paginator should navigate.
* @param isDisabled Whether the button is disabled.
*/
protected _buttonClicked(targetIndex: number, isDisabled: boolean) {
// Note that normally disabled buttons won't dispatch the click event, but the paginator ones
// do, because we're using `disabledInteractive` to allow them to be focusable. We need to
// check here to avoid the navigation.
if (!isDisabled) {
this._navigate(targetIndex);
}
}
}
1 change: 1 addition & 0 deletions tools/public_api_guard/material/paginator.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function MAT_PAGINATOR_INTL_PROVIDER_FACTORY(parentIntl: MatPaginatorIntl
// @public
export class MatPaginator implements OnInit, OnDestroy {
constructor(_intl: MatPaginatorIntl, _changeDetectorRef: ChangeDetectorRef, defaults?: MatPaginatorDefaultOptions);
protected _buttonClicked(targetIndex: number, isDisabled: boolean): void;
_changePageSize(pageSize: number): void;
color: ThemePalette;
disabled: boolean;
Expand Down

0 comments on commit 3ea8cf5

Please sign in to comment.