Skip to content

Commit 10e9243

Browse files
crisbetojosephperrott
authored andcommitted
refactor(sort): remove sort header dependency on table (#13507)
1 parent 7f5cbe3 commit 10e9243

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/cdk/table/cell.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ export const _CdkColumnDefBase: CanStickCtor & typeof CdkColumnDefBase =
5555
*/
5656
@Directive({
5757
selector: '[cdkColumnDef]',
58-
inputs: ['sticky']
58+
inputs: ['sticky'],
59+
providers: [{
60+
provide: 'MAT_SORT_HEADER_COLUMN_DEF',
61+
useExisting: CdkColumnDef
62+
}],
5963
})
6064
export class CdkColumnDef extends _CdkColumnDefBase implements CanStick {
6165
/** Unique name for this column. */

src/lib/sort/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ng_module(
1414
"@angular//packages/core",
1515
"@rxjs",
1616
"//src/cdk/coercion",
17-
"//src/cdk/table",
1817
"//src/lib/core",
1918
],
2019
)

src/lib/sort/sort-header.ts

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

99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
10-
import {CdkColumnDef} from '@angular/cdk/table';
1110
import {
1211
ChangeDetectionStrategy,
1312
ChangeDetectorRef,
@@ -17,6 +16,7 @@ import {
1716
OnInit,
1817
Optional,
1918
ViewEncapsulation,
19+
Inject,
2020
} from '@angular/core';
2121
import {CanDisable, CanDisableCtor, mixinDisabled} from '@angular/material/core';
2222
import {merge, Subscription} from 'rxjs';
@@ -53,6 +53,11 @@ export interface ArrowViewStateTransition {
5353
toState: ArrowViewState;
5454
}
5555

56+
/** Column definition associated with a `MatSortHeader`. */
57+
interface MatSortHeaderColumnDef {
58+
name: string;
59+
}
60+
5661
/**
5762
* Applies sorting behavior (click to change sort) and styles to an element, including an
5863
* arrow to display the current sort direction.
@@ -134,8 +139,12 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
134139
constructor(public _intl: MatSortHeaderIntl,
135140
changeDetectorRef: ChangeDetectorRef,
136141
@Optional() public _sort: MatSort,
137-
@Optional() public _cdkColumnDef: CdkColumnDef) {
138-
142+
@Inject('MAT_SORT_HEADER_COLUMN_DEF') @Optional()
143+
public _columnDef: MatSortHeaderColumnDef) {
144+
// Note that we use a string token for the `_columnDef`, because the value is provided both by
145+
// `material/table` and `cdk/table` and we can't have the CDK depending on Material,
146+
// and we want to avoid having the sort header depending on the CDK table because
147+
// of this single reference.
139148
super();
140149

141150
if (!_sort) {
@@ -159,8 +168,8 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
159168
}
160169

161170
ngOnInit() {
162-
if (!this.id && this._cdkColumnDef) {
163-
this.id = this._cdkColumnDef.name;
171+
if (!this.id && this._columnDef) {
172+
this.id = this._columnDef.name;
164173
}
165174

166175
// Initialize the direction of the arrow and set the view state to be immediately that state.

src/lib/table/cell.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export class MatFooterCellDef extends _CdkFooterCellDef {}
5656
*/
5757
@Directive({
5858
selector: '[matColumnDef]',
59-
providers: [{provide: CdkColumnDef, useExisting: MatColumnDef}],
59+
providers: [
60+
{provide: CdkColumnDef, useExisting: MatColumnDef},
61+
{provide: 'MAT_SORT_HEADER_COLUMN_DEF', useExisting: MatColumnDef}
62+
],
6063
})
6164
export class MatColumnDef extends CdkColumnDef {
6265
/** Unique name for this column. */

0 commit comments

Comments
 (0)