Skip to content

Commit 5fc655b

Browse files
authored
feat(material/paginator): add input for configuring the underlying select (#13705)
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 7d0bca7 commit 5fc655b

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
lines changed

src/material-experimental/mdc-paginator/paginator.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
[value]="pageSize"
1515
[disabled]="disabled"
1616
[aria-labelledby]="_pageSizeLabelId"
17+
[panelClass]="selectConfig.panelClass || ''"
18+
[disableOptionCentering]="selectConfig.disableOptionCentering"
1719
(selectionChange)="_changePageSize($event.value)">
1820
<mat-option *ngFor="let pageSizeOption of _displayedPageSizeOptions" [value]="pageSizeOption">
1921
{{pageSizeOption}}

src/material-experimental/mdc-paginator/paginator.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {dispatchMouseEvent} from '../../cdk/testing/private';
55
import {ThemePalette} from '@angular/material/core';
66
import {MatSelect} from '@angular/material-experimental/mdc-select';
77
import {By} from '@angular/platform-browser';
8-
import {MatPaginatorModule, MatPaginator, MatPaginatorIntl} from './index';
8+
import {
9+
MatPaginatorModule,
10+
MatPaginator,
11+
MatPaginatorIntl,
12+
MatPaginatorSelectConfig,
13+
} from './index';
914
import {MAT_PAGINATOR_DEFAULT_OPTIONS, MatPaginatorDefaultOptions} from './paginator';
1015

1116
describe('MDC-based MatPaginator', () => {
@@ -215,6 +220,24 @@ describe('MDC-based MatPaginator', () => {
215220
expect(formField.classList).not.toContain('mat-accent');
216221
});
217222

223+
it('should be able to pass options to the underlying mat-select', () => {
224+
const fixture = createComponent(MatPaginatorApp);
225+
fixture.detectChanges();
226+
const select: MatSelect = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
227+
228+
expect(select.disableOptionCentering).toBe(false);
229+
expect(select.panelClass).toBeFalsy();
230+
231+
fixture.componentInstance.selectConfig = {
232+
disableOptionCentering: true,
233+
panelClass: 'custom-class',
234+
};
235+
fixture.detectChanges();
236+
237+
expect(select.disableOptionCentering).toBe(true);
238+
expect(select.panelClass).toBe('custom-class');
239+
});
240+
218241
describe('when showing the first and last button', () => {
219242
let fixture: ComponentFixture<MatPaginatorApp>;
220243
let component: MatPaginatorApp;
@@ -542,6 +565,7 @@ function getLastButton(fixture: ComponentFixture<any>) {
542565
[pageSize]="pageSize"
543566
[pageSizeOptions]="pageSizeOptions"
544567
[hidePageSize]="hidePageSize"
568+
[selectConfig]="selectConfig"
545569
[showFirstLastButtons]="showFirstLastButtons"
546570
[length]="length"
547571
[color]="color"
@@ -560,6 +584,7 @@ class MatPaginatorApp {
560584
disabled: boolean;
561585
pageEvent = jasmine.createSpy('page event');
562586
color: ThemePalette;
587+
selectConfig: MatPaginatorSelectConfig = {};
563588

564589
@ViewChild(MatPaginator) paginator: MatPaginator;
565590

src/material-experimental/mdc-paginator/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export {
1313
MAT_PAGINATOR_INTL_PROVIDER_FACTORY,
1414
MAT_PAGINATOR_INTL_PROVIDER,
1515
PageEvent,
16+
MatPaginatorSelectConfig,
1617
} from '@angular/material/paginator';

src/material/paginator/paginator.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<mat-select
1414
[value]="pageSize"
1515
[disabled]="disabled"
16+
[panelClass]="selectConfig.panelClass || ''"
17+
[disableOptionCentering]="selectConfig.disableOptionCentering"
1618
[aria-label]="_intl.itemsPerPageLabel"
1719
(selectionChange)="_changePageSize($event.value)">
1820
<mat-option *ngFor="let pageSizeOption of _displayedPageSizeOptions" [value]="pageSizeOption">

src/material/paginator/paginator.md

Lines changed: 6 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`.
@@ -30,5 +34,5 @@ The paginator uses `role="group"` to semantically group its child controls. You
3034
`aria-label` or `aria-labelledby` attribute to `<mat-paginator>` with a label that describes
3135
the content controlled by the pagination control.
3236

33-
You can set the `aria-label` attributes for the button and select controls within the paginator in
37+
You can set the `aria-label` attributes for the button and select controls within the paginator in
3438
`MatPaginatorIntl`.

src/material/paginator/paginator.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {ThemePalette} from '@angular/material/core';
66
import {MatSelect} from '@angular/material/select';
77
import {By} from '@angular/platform-browser';
88
import {MatPaginator, MatPaginatorIntl, MatPaginatorModule} from './index';
9-
import {MAT_PAGINATOR_DEFAULT_OPTIONS, MatPaginatorDefaultOptions} from './paginator';
9+
import {
10+
MAT_PAGINATOR_DEFAULT_OPTIONS,
11+
MatPaginatorDefaultOptions,
12+
MatPaginatorSelectConfig,
13+
} from './paginator';
1014

1115
describe('MatPaginator', () => {
1216
function createComponent<T>(type: Type<T>, providers: Provider[] = []): ComponentFixture<T> {
@@ -210,6 +214,24 @@ describe('MatPaginator', () => {
210214
expect(formField.classList).not.toContain('mat-accent');
211215
});
212216

217+
it('should be able to pass options to the underlying mat-select', () => {
218+
const fixture = createComponent(MatPaginatorApp);
219+
fixture.detectChanges();
220+
const select: MatSelect = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
221+
222+
expect(select.disableOptionCentering).toBe(false);
223+
expect(select.panelClass).toBeFalsy();
224+
225+
fixture.componentInstance.selectConfig = {
226+
disableOptionCentering: true,
227+
panelClass: 'custom-class',
228+
};
229+
fixture.detectChanges();
230+
231+
expect(select.disableOptionCentering).toBe(true);
232+
expect(select.panelClass).toBe('custom-class');
233+
});
234+
213235
describe('when showing the first and last button', () => {
214236
let fixture: ComponentFixture<MatPaginatorApp>;
215237
let component: MatPaginatorApp;
@@ -537,6 +559,7 @@ function getLastButton(fixture: ComponentFixture<any>) {
537559
[pageSize]="pageSize"
538560
[pageSizeOptions]="pageSizeOptions"
539561
[hidePageSize]="hidePageSize"
562+
[selectConfig]="selectConfig"
540563
[showFirstLastButtons]="showFirstLastButtons"
541564
[length]="length"
542565
[color]="color"
@@ -555,6 +578,7 @@ class MatPaginatorApp {
555578
disabled: boolean;
556579
pageEvent = jasmine.createSpy('page event');
557580
color: ThemePalette;
581+
selectConfig: MatPaginatorSelectConfig = {};
558582

559583
@ViewChild(MatPaginator) paginator: MatPaginator;
560584

src/material/paginator/paginator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ export const MAT_PAGINATOR_DEFAULT_OPTIONS = new InjectionToken<MatPaginatorDefa
8989
/** @docs-private */
9090
const _MatPaginatorMixinBase = mixinDisabled(mixinInitialized(class {}));
9191

92+
/** Object that can used to configure the underlying `MatSelect` inside a `MatPaginator`. */
93+
export interface MatPaginatorSelectConfig {
94+
/** Whether to center the active option over the trigger. */
95+
disableOptionCentering?: boolean;
96+
97+
/** Classes to be passed to the select panel. */
98+
panelClass?: string | string[] | Set<string> | {[key: string]: any};
99+
}
100+
92101
/**
93102
* Base class with all of the `MatPaginator` functionality.
94103
* @docs-private
@@ -175,6 +184,9 @@ export abstract class _MatPaginatorBase<
175184
}
176185
private _showFirstLastButtons = false;
177186

187+
/** Used to configure the underlying `MatSelect` inside the paginator. */
188+
@Input() selectConfig: MatPaginatorSelectConfig = {};
189+
178190
/** Event emitted when the paginator changes the page size or page index. */
179191
@Output() readonly page: EventEmitter<PageEvent> = new EventEmitter<PageEvent>();
180192

tools/public_api_guard/material/paginator.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ export abstract class _MatPaginatorBase<O extends {
8686
set pageSizeOptions(value: number[] | readonly number[]);
8787
_previousButtonsDisabled(): boolean;
8888
previousPage(): void;
89+
selectConfig: MatPaginatorSelectConfig;
8990
get showFirstLastButtons(): boolean;
9091
set showFirstLastButtons(value: BooleanInput);
9192
// (undocumented)
92-
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatPaginatorBase<any>, never, never, { "color": "color"; "pageIndex": "pageIndex"; "length": "length"; "pageSize": "pageSize"; "pageSizeOptions": "pageSizeOptions"; "hidePageSize": "hidePageSize"; "showFirstLastButtons": "showFirstLastButtons"; }, { "page": "page"; }, never>;
93+
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatPaginatorBase<any>, never, never, { "color": "color"; "pageIndex": "pageIndex"; "length": "length"; "pageSize": "pageSize"; "pageSizeOptions": "pageSizeOptions"; "hidePageSize": "hidePageSize"; "showFirstLastButtons": "showFirstLastButtons"; "selectConfig": "selectConfig"; }, { "page": "page"; }, never>;
9394
// (undocumented)
9495
static ɵfac: i0.ɵɵFactoryDeclaration<_MatPaginatorBase<any>, never>;
9596
}
@@ -128,6 +129,14 @@ export class MatPaginatorModule {
128129
static ɵmod: i0.ɵɵNgModuleDeclaration<MatPaginatorModule, [typeof i1.MatPaginator], [typeof i2.CommonModule, typeof i3.MatButtonModule, typeof i4.MatSelectModule, typeof i5.MatTooltipModule, typeof i6.MatCommonModule], [typeof i1.MatPaginator]>;
129130
}
130131

132+
// @public
133+
export interface MatPaginatorSelectConfig {
134+
disableOptionCentering?: boolean;
135+
panelClass?: string | string[] | Set<string> | {
136+
[key: string]: any;
137+
};
138+
}
139+
131140
// @public
132141
export class PageEvent {
133142
length: number;

0 commit comments

Comments
 (0)