Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/sort/sort-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export function getSortHeaderNotContainedWithinSortError(): Error {
export function getSortHeaderMissingIdError(): Error {
return Error(`MatSortHeader must be provided with a unique id.`);
}

/** @docs-private */
export function getSortInvalidDirectionError(direction: string): Error {
return Error(`${direction} is not a valid sort direction ('asc' or 'desc').`);
}
3 changes: 2 additions & 1 deletion src/lib/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class MatSortHeader implements MatSortable {

/** Whether this MatSortHeader is currently sorted in either ascending or descending order. */
_isSorted() {
return this._sort.active == this.id && this._sort.direction;
return this._sort.active == this.id &&
this._sort.direction === 'asc' || this._sort.direction === 'desc';
}
}
19 changes: 18 additions & 1 deletion src/lib/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getSortDuplicateSortableIdError,
getSortHeaderMissingIdError,
getSortHeaderNotContainedWithinSortError,
getSortInvalidDirectionError,
} from './sort-errors';


Expand All @@ -37,7 +38,8 @@ describe('MatSort', () => {
MatTableMatSortApp,
MatSortHeaderMissingMatSortApp,
MatSortDuplicateMatSortableIdsApp,
MatSortableMissingIdApp
MatSortableMissingIdApp,
MatSortableInvalidDirection
],
}).compileComponents();
}));
Expand Down Expand Up @@ -136,6 +138,11 @@ describe('MatSort', () => {
.toThrowError(wrappedErrorMessage(getSortHeaderMissingIdError()));
});

it('should throw an error if the provided direction is invalid', () => {
expect(() => TestBed.createComponent(MatSortableInvalidDirection).detectChanges())
.toThrowError(wrappedErrorMessage(getSortInvalidDirectionError('ascending')));
});

it('should allow let MatSortable override the default sort parameters', () => {
testSingleColumnSortDirectionSequence(
fixture, ['asc', 'desc', '']);
Expand Down Expand Up @@ -333,3 +340,13 @@ class MatSortDuplicateMatSortableIdsApp { }
`
})
class MatSortableMissingIdApp { }


@Component({
template: `
<div matSort matSortDirection="ascending">
<div mat-sort-header="a"> A </div>
</div>
`
})
class MatSortableInvalidDirection { }
18 changes: 15 additions & 3 deletions src/lib/sort/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, EventEmitter, Input, Output} from '@angular/core';
import {Directive, EventEmitter, Input, isDevMode, Output} from '@angular/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {SortDirection} from './sort-direction';
import {getSortDuplicateSortableIdError, getSortHeaderMissingIdError} from './sort-errors';
import {
getSortInvalidDirectionError,
getSortDuplicateSortableIdError,
getSortHeaderMissingIdError
} from './sort-errors';

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

/** The sort direction of the currently active MatSortable. */
@Input('matSortDirection') direction: SortDirection = '';
@Input('matSortDirection')
set direction(direction: SortDirection) {
if (isDevMode() && direction && direction !== 'asc' && direction !== 'desc') {
throw getSortInvalidDirectionError(direction);
}
this._direction = direction;
}
get direction(): SortDirection { return this._direction; }
private _direction: SortDirection = '';

/**
* Whether to disable the user from clearing the sort by finishing the sort direction cycle.
Expand Down