Skip to content

Commit fdd87b9

Browse files
crisbetommalerba
authored andcommitted
fix(cdk/table): resolve breaking constructor changes (#20425)
In #19964 and #19750 some breaking constructor changes were made which eventually got released as a part of a patch branch which doesn't follow our breaking changes policy. These changes make the new constructor parameters optional and add fallbacks to the old behavior. Fixes #20422.
1 parent 7939bc0 commit fdd87b9

File tree

11 files changed

+89
-34
lines changed

11 files changed

+89
-34
lines changed

src/cdk-experimental/column-resize/resize-strategy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {Inject, Injectable, OnDestroy, Provider} from '@angular/core';
1010
import {DOCUMENT} from '@angular/common';
1111
import {coerceCssPixelValue} from '@angular/cdk/coercion';
12-
import {CdkTable, _CoalescedStyleScheduler} from '@angular/cdk/table';
12+
import {CdkTable, _CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER} from '@angular/cdk/table';
1313

1414
import {ColumnResize} from './column-resize';
1515

@@ -76,7 +76,8 @@ export abstract class ResizeStrategy {
7676
export class TableLayoutFixedResizeStrategy extends ResizeStrategy {
7777
constructor(
7878
protected readonly columnResize: ColumnResize,
79-
protected readonly styleScheduler: _CoalescedStyleScheduler,
79+
@Inject(_COALESCED_STYLE_SCHEDULER)
80+
protected readonly styleScheduler: _CoalescedStyleScheduler,
8081
protected readonly table: CdkTable<unknown>) {
8182
super();
8283
}
@@ -131,7 +132,8 @@ export class CdkFlexTableResizeStrategy extends ResizeStrategy implements OnDest
131132

132133
constructor(
133134
protected readonly columnResize: ColumnResize,
134-
protected readonly styleScheduler: _CoalescedStyleScheduler,
135+
@Inject(_COALESCED_STYLE_SCHEDULER)
136+
protected readonly styleScheduler: _CoalescedStyleScheduler,
135137
protected readonly table: CdkTable<unknown>,
136138
@Inject(DOCUMENT) document: any) {
137139
super();

src/cdk/table/coalesced-style-scheduler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, NgZone, OnDestroy} from '@angular/core';
9+
import {Injectable, NgZone, OnDestroy, InjectionToken} from '@angular/core';
1010
import {from, Subject} from 'rxjs';
1111
import {take, takeUntil} from 'rxjs/operators';
1212

@@ -18,6 +18,10 @@ export class _Schedule {
1818
endTasks: (() => unknown)[] = [];
1919
}
2020

21+
/** Injection token used to provide a coalesced style scheduler. */
22+
export const _COALESCED_STYLE_SCHEDULER =
23+
new InjectionToken<_CoalescedStyleScheduler>('_COALESCED_STYLE_SCHEDULER');
24+
2125
/**
2226
* Allows grouping up CSSDom mutations after the current execution context.
2327
* This can significantly improve performance when separate consecutive functions are

src/cdk/table/sticky-styler.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export class StickyStyler {
4242
constructor(private _isNativeHtmlTable: boolean,
4343
private _stickCellCss: string,
4444
public direction: Direction,
45-
private _coalescedStyleScheduler: _CoalescedStyleScheduler,
45+
/**
46+
* @deprecated `_coalescedStyleScheduler` parameter to become required.
47+
* @breaking-change 11.0.0
48+
*/
49+
private _coalescedStyleScheduler?: _CoalescedStyleScheduler,
4650
private _isBrowser = true,
4751
private readonly _needsPositionStickyOnElement = true) { }
4852

@@ -68,7 +72,7 @@ export class StickyStyler {
6872
}
6973

7074
// Coalesce with sticky row/column updates (and potentially other changes like column resize).
71-
this._coalescedStyleScheduler.schedule(() => {
75+
this._scheduleStyleChanges(() => {
7276
for (const element of elementsToClear) {
7377
this._removeStickyStyle(element, stickyDirections);
7478
}
@@ -99,7 +103,7 @@ export class StickyStyler {
99103
const endPositions = this._getStickyEndColumnPositions(cellWidths, stickyEndStates);
100104

101105
// Coalesce with sticky row updates (and potentially other changes like column resize).
102-
this._coalescedStyleScheduler.schedule(() => {
106+
this._scheduleStyleChanges(() => {
103107
const isRtl = this.direction === 'rtl';
104108
const start = isRtl ? 'right' : 'left';
105109
const end = isRtl ? 'left' : 'right';
@@ -163,7 +167,7 @@ export class StickyStyler {
163167

164168
// Coalesce with other sticky row updates (top/bottom), sticky columns updates
165169
// (and potentially other changes like column resize).
166-
this._coalescedStyleScheduler.schedule(() => {
170+
this._scheduleStyleChanges(() => {
167171
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
168172
if (!states[rowIndex]) {
169173
continue;
@@ -191,7 +195,7 @@ export class StickyStyler {
191195
const tfoot = tableElement.querySelector('tfoot')!;
192196

193197
// Coalesce with other sticky updates (and potentially other changes like column resize).
194-
this._coalescedStyleScheduler.schedule(() => {
198+
this._scheduleStyleChanges(() => {
195199
if (stickyStates.some(state => !state)) {
196200
this._removeStickyStyle(tfoot, ['bottom']);
197201
} else {
@@ -323,4 +327,17 @@ export class StickyStyler {
323327

324328
return positions;
325329
}
330+
331+
/**
332+
* Schedules styles to be applied when the style scheduler deems appropriate.
333+
* @breaking-change 11.0.0 This method can be removed in favor of calling
334+
* `CoalescedStyleScheduler.schedule` directly once the scheduler is a required parameter.
335+
*/
336+
private _scheduleStyleChanges(changes: () => void) {
337+
if (this._coalescedStyleScheduler) {
338+
this._coalescedStyleScheduler.schedule(changes);
339+
} else {
340+
changes();
341+
}
342+
}
326343
}

src/cdk/table/table.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
} from 'rxjs';
5858
import {takeUntil} from 'rxjs/operators';
5959
import {CdkColumnDef} from './cell';
60-
import {_CoalescedStyleScheduler} from './coalesced-style-scheduler';
60+
import {_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER} from './coalesced-style-scheduler';
6161
import {
6262
BaseRowDef,
6363
CdkCellOutlet,
@@ -199,7 +199,7 @@ export interface RenderRow<T> {
199199
providers: [
200200
{provide: CDK_TABLE, useExisting: CdkTable},
201201
{provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy},
202-
_CoalescedStyleScheduler,
202+
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
203203
]
204204
})
205205
export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
@@ -443,14 +443,19 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
443443
constructor(
444444
protected readonly _differs: IterableDiffers,
445445
protected readonly _changeDetectorRef: ChangeDetectorRef,
446-
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler,
447446
protected readonly _elementRef: ElementRef, @Attribute('role') role: string,
448447
@Optional() protected readonly _dir: Directionality, @Inject(DOCUMENT) _document: any,
449448
private _platform: Platform,
450-
// Optional for backwards compatibility, but a view repeater strategy will always
451-
// be provided.
449+
450+
/**
451+
* @deprecated `_coalescedStyleScheduler`, `_viewRepeater` and `_viewportRuler`
452+
* parameters to become required.
453+
* @breaking-change 11.0.0
454+
*/
452455
@Optional() @Inject(_VIEW_REPEATER_STRATEGY)
453-
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>) {
456+
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>>,
457+
@Optional() @Inject(_COALESCED_STYLE_SCHEDULER)
458+
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler) {
454459
if (!role) {
455460
this._elementRef.nativeElement.setAttribute('role', 'grid');
456461
}
@@ -549,11 +554,14 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
549554
return;
550555
}
551556
const viewContainer = this._rowOutlet.viewContainer;
552-
this._viewRepeater.applyChanges(
557+
558+
// @breaking-change 11.0.0 Remove null check for `_viewRepeater`
559+
// once it's a required parameter in the constructor.
560+
this._viewRepeater?.applyChanges(
553561
changes,
554562
viewContainer,
555563
(record: IterableChangeRecord<RenderRow<T>>,
556-
adjustedPreviousIndex: number|null,
564+
_adjustedPreviousIndex: number|null,
557565
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
558566
(record) => record.item.data,
559567
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {

src/material-experimental/column-resize/overlay-handle.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
ViewEncapsulation,
1616
} from '@angular/core';
1717
import {DOCUMENT} from '@angular/common';
18-
import {CdkColumnDef, _CoalescedStyleScheduler} from '@angular/cdk/table';
18+
import {
19+
CdkColumnDef,
20+
_CoalescedStyleScheduler,
21+
_COALESCED_STYLE_SCHEDULER,
22+
} from '@angular/cdk/table';
1923
import {Directionality} from '@angular/cdk/bidi';
2024
import {
2125
ColumnResize,
@@ -49,7 +53,8 @@ export class MatColumnResizeOverlayHandle extends ResizeOverlayHandle {
4953
protected readonly ngZone: NgZone,
5054
protected readonly resizeNotifier: ColumnResizeNotifierSource,
5155
protected readonly resizeRef: ResizeRef,
52-
protected readonly styleScheduler: _CoalescedStyleScheduler,
56+
@Inject(_COALESCED_STYLE_SCHEDULER)
57+
protected readonly styleScheduler: _CoalescedStyleScheduler,
5358
@Inject(DOCUMENT) document: any) {
5459
super();
5560
this.document = document;

src/material-experimental/column-resize/resizable-directives/default-enabled-resizable.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
import {DOCUMENT} from '@angular/common';
1919
import {Directionality} from '@angular/cdk/bidi';
2020
import {Overlay} from '@angular/cdk/overlay';
21-
import {CdkColumnDef, _CoalescedStyleScheduler} from '@angular/cdk/table';
21+
import {
22+
CdkColumnDef,
23+
_CoalescedStyleScheduler,
24+
_COALESCED_STYLE_SCHEDULER,
25+
} from '@angular/cdk/table';
2226
import {
2327
ColumnResize,
2428
ColumnResizeNotifierSource,
@@ -52,7 +56,8 @@ export class MatDefaultResizable extends AbstractMatResizable {
5256
protected readonly overlay: Overlay,
5357
protected readonly resizeNotifier: ColumnResizeNotifierSource,
5458
protected readonly resizeStrategy: ResizeStrategy,
55-
protected readonly styleScheduler: _CoalescedStyleScheduler,
59+
@Inject(_COALESCED_STYLE_SCHEDULER)
60+
protected readonly styleScheduler: _CoalescedStyleScheduler,
5661
protected readonly viewContainerRef: ViewContainerRef,
5762
protected readonly changeDetectorRef: ChangeDetectorRef) {
5863
super();

src/material-experimental/column-resize/resizable-directives/resizable.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
import {DOCUMENT} from '@angular/common';
1919
import {Directionality} from '@angular/cdk/bidi';
2020
import {Overlay} from '@angular/cdk/overlay';
21-
import {CdkColumnDef, _CoalescedStyleScheduler} from '@angular/cdk/table';
21+
import {
22+
CdkColumnDef,
23+
_CoalescedStyleScheduler,
24+
_COALESCED_STYLE_SCHEDULER,
25+
} from '@angular/cdk/table';
2226
import {
2327
ColumnResize,
2428
ColumnResizeNotifierSource,
@@ -51,7 +55,8 @@ export class MatResizable extends AbstractMatResizable {
5155
protected readonly overlay: Overlay,
5256
protected readonly resizeNotifier: ColumnResizeNotifierSource,
5357
protected readonly resizeStrategy: ResizeStrategy,
54-
protected readonly styleScheduler: _CoalescedStyleScheduler,
58+
@Inject(_COALESCED_STYLE_SCHEDULER)
59+
protected readonly styleScheduler: _CoalescedStyleScheduler,
5560
protected readonly viewContainerRef: ViewContainerRef,
5661
protected readonly changeDetectorRef: ChangeDetectorRef) {
5762
super();

src/material-experimental/column-resize/resize-strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Inject, Injectable, Provider} from '@angular/core';
1010
import {DOCUMENT} from '@angular/common';
11-
import {CdkTable, _CoalescedStyleScheduler} from '@angular/cdk/table';
11+
import {CdkTable, _CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER} from '@angular/cdk/table';
1212

1313
import {
1414
ColumnResize,
@@ -26,7 +26,7 @@ export {TABLE_LAYOUT_FIXED_RESIZE_STRATEGY_PROVIDER};
2626
export class MatFlexTableResizeStrategy extends CdkFlexTableResizeStrategy {
2727
constructor(
2828
columnResize: ColumnResize,
29-
styleScheduler: _CoalescedStyleScheduler,
29+
@Inject(_COALESCED_STYLE_SCHEDULER) styleScheduler: _CoalescedStyleScheduler,
3030
table: CdkTable<unknown>,
3131
@Inject(DOCUMENT) document: any) {
3232
super(columnResize, styleScheduler, table, document);

src/material-experimental/mdc-table/table.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
*/
88

99
import {ChangeDetectionStrategy, Component, OnInit, ViewEncapsulation} from '@angular/core';
10-
import {CDK_TABLE_TEMPLATE, CdkTable, _CoalescedStyleScheduler} from '@angular/cdk/table';
10+
import {
11+
CDK_TABLE_TEMPLATE,
12+
CdkTable,
13+
_CoalescedStyleScheduler,
14+
_COALESCED_STYLE_SCHEDULER,
15+
} from '@angular/cdk/table';
1116
import {_DisposeViewRepeaterStrategy, _VIEW_REPEATER_STRATEGY} from '@angular/cdk/collections';
1217

1318
@Component({
@@ -20,7 +25,7 @@ import {_DisposeViewRepeaterStrategy, _VIEW_REPEATER_STRATEGY} from '@angular/cd
2025
},
2126
providers: [
2227
{provide: CdkTable, useExisting: MatTable},
23-
_CoalescedStyleScheduler,
28+
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
2429
// TODO(michaeljamesparsons) Abstract the view repeater strategy to a directive API so this code
2530
// is only included in the build if used.
2631
{provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy},

src/material/table/table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
CDK_TABLE_TEMPLATE,
1111
CdkTable,
1212
CDK_TABLE,
13-
_CoalescedStyleScheduler
13+
_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER
1414
} from '@angular/cdk/table';
1515
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
1616
import {_DisposeViewRepeaterStrategy, _VIEW_REPEATER_STRATEGY} from '@angular/cdk/collections';
@@ -32,7 +32,7 @@ import {_DisposeViewRepeaterStrategy, _VIEW_REPEATER_STRATEGY} from '@angular/cd
3232
{provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy},
3333
{provide: CdkTable, useExisting: MatTable},
3434
{provide: CDK_TABLE, useExisting: MatTable},
35-
_CoalescedStyleScheduler,
35+
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
3636
],
3737
encapsulation: ViewEncapsulation.None,
3838
// See note on CdkTable for explanation on why this uses the default change detection strategy.

tools/public_api_guard/cdk/table.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export declare const _COALESCED_STYLE_SCHEDULER: InjectionToken<_CoalescedStyleScheduler>;
2+
13
export declare class _CoalescedStyleScheduler implements OnDestroy {
24
constructor(_ngZone: NgZone);
35
ngOnDestroy(): void;
@@ -186,7 +188,7 @@ export declare class CdkRowDef<T> extends BaseRowDef {
186188

187189
export declare class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
188190
protected readonly _changeDetectorRef: ChangeDetectorRef;
189-
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler;
191+
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined;
190192
_contentColumnDefs: QueryList<CdkColumnDef>;
191193
_contentFooterRowDefs: QueryList<CdkFooterRowDef>;
192194
_contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
@@ -201,7 +203,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
201203
_noDataRow: CdkNoDataRow;
202204
_noDataRowOutlet: NoDataRowOutlet;
203205
_rowOutlet: DataRowOutlet;
204-
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>;
206+
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined;
205207
get dataSource(): CdkTableDataSourceInput<T>;
206208
set dataSource(dataSource: CdkTableDataSourceInput<T>);
207209
get multiTemplateDataRows(): boolean;
@@ -214,7 +216,8 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
214216
start: number;
215217
end: number;
216218
}>;
217-
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _coalescedStyleScheduler: _CoalescedStyleScheduler, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform, _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>);
219+
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform,
220+
_viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined, _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined);
218221
_getRenderedRows(rowOutlet: RowOutlet): HTMLElement[];
219222
_getRowDefs(data: T, dataIndex: number): CdkRowDef<T>[];
220223
addColumnDef(columnDef: CdkColumnDef): void;
@@ -234,7 +237,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
234237
updateStickyHeaderRowStyles(): void;
235238
static ngAcceptInputType_multiTemplateDataRows: BooleanInput;
236239
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; }, {}, ["_noDataRow", "_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption", "colgroup, col"]>;
237-
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, null, { attribute: "role"; }, { optional: true; }, null, null, { optional: true; }]>;
240+
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, { optional: true; }, { optional: true; }]>;
238241
}
239242

240243
export declare class CdkTableModule {
@@ -319,7 +322,8 @@ export declare type StickyDirection = 'top' | 'bottom' | 'left' | 'right';
319322

320323
export declare class StickyStyler {
321324
direction: Direction;
322-
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction, _coalescedStyleScheduler: _CoalescedStyleScheduler, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean);
325+
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction,
326+
_coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean);
323327
_addStickyStyle(element: HTMLElement, dir: StickyDirection, dirValue: number): void;
324328
_getCalculatedZIndex(element: HTMLElement): string;
325329
_getCellWidths(row: HTMLElement): number[];

0 commit comments

Comments
 (0)