Skip to content

Commit cc6f39e

Browse files
authored
fix(sort): throw error on invalid direction (#7378)
1 parent 1823b2f commit cc6f39e

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/lib/sort/sort-errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ export function getSortHeaderNotContainedWithinSortError(): Error {
2020
export function getSortHeaderMissingIdError(): Error {
2121
return Error(`MatSortHeader must be provided with a unique id.`);
2222
}
23+
24+
/** @docs-private */
25+
export function getSortInvalidDirectionError(direction: string): Error {
26+
return Error(`${direction} is not a valid sort direction ('asc' or 'desc').`);
27+
}

src/lib/sort/sort-header.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class MatSortHeader implements MatSortable {
126126

127127
/** Whether this MatSortHeader is currently sorted in either ascending or descending order. */
128128
_isSorted() {
129-
return this._sort.active == this.id && this._sort.direction;
129+
return this._sort.active == this.id &&
130+
this._sort.direction === 'asc' || this._sort.direction === 'desc';
130131
}
131132
}

src/lib/sort/sort.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getSortDuplicateSortableIdError,
2121
getSortHeaderMissingIdError,
2222
getSortHeaderNotContainedWithinSortError,
23+
getSortInvalidDirectionError,
2324
} from './sort-errors';
2425

2526

@@ -37,7 +38,8 @@ describe('MatSort', () => {
3738
MatTableMatSortApp,
3839
MatSortHeaderMissingMatSortApp,
3940
MatSortDuplicateMatSortableIdsApp,
40-
MatSortableMissingIdApp
41+
MatSortableMissingIdApp,
42+
MatSortableInvalidDirection
4143
],
4244
}).compileComponents();
4345
}));
@@ -136,6 +138,11 @@ describe('MatSort', () => {
136138
.toThrowError(wrappedErrorMessage(getSortHeaderMissingIdError()));
137139
});
138140

141+
it('should throw an error if the provided direction is invalid', () => {
142+
expect(() => TestBed.createComponent(MatSortableInvalidDirection).detectChanges())
143+
.toThrowError(wrappedErrorMessage(getSortInvalidDirectionError('ascending')));
144+
});
145+
139146
it('should allow let MatSortable override the default sort parameters', () => {
140147
testSingleColumnSortDirectionSequence(
141148
fixture, ['asc', 'desc', '']);
@@ -333,3 +340,13 @@ class MatSortDuplicateMatSortableIdsApp { }
333340
`
334341
})
335342
class MatSortableMissingIdApp { }
343+
344+
345+
@Component({
346+
template: `
347+
<div matSort matSortDirection="ascending">
348+
<div mat-sort-header="a"> A </div>
349+
</div>
350+
`
351+
})
352+
class MatSortableInvalidDirection { }

src/lib/sort/sort.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, EventEmitter, Input, Output} from '@angular/core';
9+
import {Directive, EventEmitter, Input, isDevMode, Output} from '@angular/core';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {SortDirection} from './sort-direction';
12-
import {getSortDuplicateSortableIdError, getSortHeaderMissingIdError} from './sort-errors';
12+
import {
13+
getSortInvalidDirectionError,
14+
getSortDuplicateSortableIdError,
15+
getSortHeaderMissingIdError
16+
} from './sort-errors';
1317

1418
export interface MatSortable {
1519
id: string;
@@ -40,7 +44,15 @@ export class MatSort {
4044
@Input('matSortStart') start: 'asc' | 'desc' = 'asc';
4145

4246
/** The sort direction of the currently active MatSortable. */
43-
@Input('matSortDirection') direction: SortDirection = '';
47+
@Input('matSortDirection')
48+
set direction(direction: SortDirection) {
49+
if (isDevMode() && direction && direction !== 'asc' && direction !== 'desc') {
50+
throw getSortInvalidDirectionError(direction);
51+
}
52+
this._direction = direction;
53+
}
54+
get direction(): SortDirection { return this._direction; }
55+
private _direction: SortDirection = '';
4456

4557
/**
4658
* Whether to disable the user from clearing the sort by finishing the sort direction cycle.

0 commit comments

Comments
 (0)