Skip to content

Commit 1b98431

Browse files
committed
feat(paginator): add input for configuring the underlying select
Since we hide the underlying `MatSelect` inside the `MatPaginator`, the user doesn't have the ability to configure some of the inputs. These changes introduce an input that proxy some of the supported properties to the select. Fixes #13646.
1 parent 7c75d6e commit 1b98431

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

src/material/paginator/paginator.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
<mat-select
1313
[value]="pageSize"
1414
[disabled]="disabled"
15+
[panelClass]="selectConfig.panelClass"
16+
[disableOptionCentering]="selectConfig.disableOptionCentering"
1517
[aria-label]="_intl.itemsPerPageLabel"
1618
(selectionChange)="_changePageSize($event.value)">
17-
<mat-option *ngFor="let pageSizeOption of _displayedPageSizeOptions" [value]="pageSizeOption">
19+
<mat-option *ngFor="let pageSizeOption of _displayedPageSizeOptions"
20+
[value]="pageSizeOption">
1821
{{pageSizeOption}}
1922
</mat-option>
2023
</mat-select>

src/material/paginator/paginator.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ any associated data view.
1616
The paginator displays a dropdown of page sizes for the user to choose from. The options for this
1717
dropdown can be set via `pageSizeOptions`
1818

19-
The current pageSize will always appear in the dropdown, even if it is not included in pageSizeOptions.
19+
The current pageSize will always appear in the dropdown, even if it is not included in
20+
pageSizeOptions.
21+
22+
If you want to customize some of the optional of the `mat-select` inside the `mat-paginator`, you
23+
can use the `selectConfig` input.
2024

2125
### Internationalization
2226
The labels for the paginator can be customized by providing your own instance of `MatPaginatorIntl`.
@@ -26,4 +30,5 @@ This will allow you to change the following:
2630
3. The tooltip messages on the navigation buttons.
2731

2832
### Accessibility
29-
The `aria-label`s for next page, previous page, first page and last page buttons can be set in `MatPaginatorIntl`.
33+
The `aria-label`s for next page, previous page, first page and last page buttons can be set in
34+
`MatPaginatorIntl`.

src/material/paginator/paginator.spec.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import {dispatchMouseEvent} from '@angular/cdk/testing/private';
55
import {ThemePalette} from '@angular/material/core';
66
import {MatSelect} from '@angular/material/select';
77
import {By} from '@angular/platform-browser';
8-
import {MatPaginatorModule, MatPaginator, MatPaginatorIntl} from './index';
9-
import {MAT_PAGINATOR_DEFAULT_OPTIONS, MatPaginatorDefaultOptions} from './paginator';
8+
import {
9+
MatPaginatorModule,
10+
MatPaginator,
11+
MatPaginatorIntl,
12+
MatPaginatorSelectConfig,
13+
MAT_PAGINATOR_DEFAULT_OPTIONS,
14+
MatPaginatorDefaultOptions,
15+
} from './index';
1016

1117

1218
describe('MatPaginator', () => {
@@ -186,6 +192,24 @@ describe('MatPaginator', () => {
186192
expect(formField.classList).toContain('mat-accent');
187193
});
188194

195+
it('should be able to pass options to the underlying mat-select', () => {
196+
const fixture = createComponent(MatPaginatorApp);
197+
fixture.detectChanges();
198+
const select: MatSelect = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
199+
200+
expect(select.disableOptionCentering).toBe(false);
201+
expect(select.panelClass).toBeFalsy();
202+
203+
fixture.componentInstance.selectConfig = {
204+
disableOptionCentering: true,
205+
panelClass: 'custom-class'
206+
};
207+
fixture.detectChanges();
208+
209+
expect(select.disableOptionCentering).toBe(true);
210+
expect(select.panelClass).toBe('custom-class');
211+
});
212+
189213
describe('when showing the first and last button', () => {
190214
let fixture: ComponentFixture<MatPaginatorApp>;
191215
let component: MatPaginatorApp;
@@ -488,6 +512,7 @@ function getLastButton(fixture: ComponentFixture<any>) {
488512
[pageSize]="pageSize"
489513
[pageSizeOptions]="pageSizeOptions"
490514
[hidePageSize]="hidePageSize"
515+
[selectConfig]="selectConfig"
491516
[showFirstLastButtons]="showFirstLastButtons"
492517
[length]="length"
493518
[color]="color"
@@ -506,6 +531,7 @@ class MatPaginatorApp {
506531
disabled: boolean;
507532
pageEvent = jasmine.createSpy('page event');
508533
color: ThemePalette;
534+
selectConfig: MatPaginatorSelectConfig = {};
509535

510536
@ViewChild(MatPaginator) paginator: MatPaginator;
511537

src/material/paginator/paginator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ export interface MatPaginatorDefaultOptions {
8282
export const MAT_PAGINATOR_DEFAULT_OPTIONS =
8383
new InjectionToken<MatPaginatorDefaultOptions>('MAT_PAGINATOR_DEFAULT_OPTIONS');
8484

85+
86+
/** Object that can used to configure the underlying `MatSelect` inside a `MatPaginator`. */
87+
export interface MatPaginatorSelectConfig {
88+
/** Whether to center the active option over the trigger. */
89+
disableOptionCentering?: boolean;
90+
91+
/** Classes to be passed to the select panel. */
92+
panelClass?: string|string[]|Set<string>|{[key: string]: any};
93+
}
94+
8595
// Boilerplate for applying mixins to MatPaginator.
8696
/** @docs-private */
8797
class MatPaginatorBase {}
@@ -166,6 +176,9 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
166176
}
167177
private _showFirstLastButtons = false;
168178

179+
/** Used to configure the underlying `MatSelect` inside the paginator. */
180+
@Input() selectConfig: MatPaginatorSelectConfig = {};
181+
169182
/** Event emitted when the paginator changes the page size or page index. */
170183
@Output() readonly page: EventEmitter<PageEvent> = new EventEmitter<PageEvent>();
171184

tools/public_api_guard/material/paginator.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export declare class MatPaginator extends _MatPaginatorBase implements OnInit, O
2323
set pageSize(value: number);
2424
get pageSizeOptions(): number[];
2525
set pageSizeOptions(value: number[]);
26+
selectConfig: MatPaginatorSelectConfig;
2627
get showFirstLastButtons(): boolean;
2728
set showFirstLastButtons(value: boolean);
2829
constructor(_intl: MatPaginatorIntl, _changeDetectorRef: ChangeDetectorRef, defaults?: MatPaginatorDefaultOptions);
@@ -44,7 +45,7 @@ export declare class MatPaginator extends _MatPaginatorBase implements OnInit, O
4445
static ngAcceptInputType_pageIndex: NumberInput;
4546
static ngAcceptInputType_pageSize: NumberInput;
4647
static ngAcceptInputType_showFirstLastButtons: BooleanInput;
47-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatPaginator, "mat-paginator", ["matPaginator"], { "disabled": "disabled"; "color": "color"; "pageIndex": "pageIndex"; "length": "length"; "pageSize": "pageSize"; "pageSizeOptions": "pageSizeOptions"; "hidePageSize": "hidePageSize"; "showFirstLastButtons": "showFirstLastButtons"; }, { "page": "page"; }, never>;
48+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatPaginator, "mat-paginator", ["matPaginator"], { "disabled": "disabled"; "color": "color"; "pageIndex": "pageIndex"; "length": "length"; "pageSize": "pageSize"; "pageSizeOptions": "pageSizeOptions"; "hidePageSize": "hidePageSize"; "showFirstLastButtons": "showFirstLastButtons"; "selectConfig": "selectConfig"; }, { "page": "page"; }, never>;
4849
static ɵfac: i0.ɵɵFactoryDef<MatPaginator>;
4950
}
5051

@@ -72,6 +73,13 @@ export declare class MatPaginatorModule {
7273
static ɵmod: i0.ɵɵNgModuleDefWithMeta<MatPaginatorModule, [typeof i1.MatPaginator], [typeof i2.CommonModule, typeof i3.MatButtonModule, typeof i4.MatSelectModule, typeof i5.MatTooltipModule], [typeof i1.MatPaginator]>;
7374
}
7475

76+
export interface MatPaginatorSelectConfig {
77+
disableOptionCentering?: boolean;
78+
panelClass?: string | string[] | Set<string> | {
79+
[key: string]: any;
80+
};
81+
}
82+
7583
export declare class PageEvent {
7684
length: number;
7785
pageIndex: number;

0 commit comments

Comments
 (0)