Skip to content

Commit 1923192

Browse files
committed
fix(sort): add aria-sort to host when sorted
1 parent 835014a commit 1923192

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

src/lib/sort/sort-header-intl.ts

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

99
import {Injectable} from '@angular/core';
1010
import {Subject} from 'rxjs/Subject';
11-
import {SortDirection} from './sort-direction';
1211

1312
/**
1413
* To modify the labels and text displayed, create a new instance of MdSortHeaderIntl and
@@ -26,9 +25,4 @@ export class MdSortHeaderIntl {
2625
sortButtonLabel = (id: string) => {
2726
return `Change sorting for ${id}`;
2827
}
29-
30-
/** A label to describe the current sort (visible only to screenreaders). */
31-
sortDescriptionLabel = (id: string, direction: SortDirection) => {
32-
return `Sorted by ${id} ${direction == 'asc' ? 'ascending' : 'descending'}`;
33-
}
3428
}

src/lib/sort/sort-header.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@
1313
<div class="mat-sort-header-pointer-right"></div>
1414
</div>
1515
</div>
16-
17-
<span class="cdk-visually-hidden" *ngIf="_isSorted()">
18-
{{_intl.sortDescriptionLabel(id, _sort.direction)}}
19-
</span>

src/lib/sort/sort-header.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ import {
1515
ViewEncapsulation
1616
} from '@angular/core';
1717
import {coerceBooleanProperty} from '@angular/cdk/coercion';
18-
import {
19-
trigger,
20-
state,
21-
style,
22-
animate,
23-
transition
24-
} from '@angular/animations';
18+
import {animate, state, style, transition, trigger} from '@angular/animations';
2519
import {CdkColumnDef} from '@angular/cdk/table';
2620
import {Subscription} from 'rxjs/Subscription';
2721
import {merge} from 'rxjs/observable/merge';
@@ -46,6 +40,7 @@ import {getMdSortHeaderNotContainedWithinMdSortError} from './sort-errors';
4640
styleUrls: ['sort-header.css'],
4741
host: {
4842
'(click)': '_sort.sort(this)',
43+
'[attr.aria-sort]': '_getAriaSortAttribute()',
4944
'[class.mat-sort-header-sorted]': '_isSorted()',
5045
},
5146
encapsulation: ViewEncapsulation.None,
@@ -113,4 +108,16 @@ export class MdSortHeader implements MdSortable {
113108
_isSorted() {
114109
return this._sort.active == this.id && this._sort.direction;
115110
}
111+
112+
/**
113+
* Gets the aria-sort attribute that should be applied to this sort header. If this header
114+
* is not sorted, returns null so that the attribute is removed from the host element. Aria spec
115+
* says that the aria-sort property should only be present on one header at a time, so removing
116+
* ensures this is true.
117+
*/
118+
_getAriaSortAttribute() {
119+
if (!this._isSorted()) { return null; }
120+
121+
return this._sort.direction == 'asc' ? 'ascending' : 'descending';
122+
}
116123
}

src/lib/sort/sort.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ describe('MdSort', () => {
144144
expect(button.getAttribute('aria-label')).toBe('Change sorting for defaultSortHeaderA');
145145
});
146146

147+
it('should apply the aria-sort label to the header when sorted', () => {
148+
const sortHeaderElement = fixture.nativeElement.querySelector('#defaultSortHeaderA');
149+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe(null);
150+
151+
component.sort('defaultSortHeaderA');
152+
fixture.detectChanges();
153+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('ascending');
154+
155+
component.sort('defaultSortHeaderA');
156+
fixture.detectChanges();
157+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('descending');
158+
159+
component.sort('defaultSortHeaderA');
160+
fixture.detectChanges();
161+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe(null);
162+
});
163+
147164
it('should re-render when the i18n labels have changed',
148165
inject([MdSortHeaderIntl], (intl: MdSortHeaderIntl) => {
149166
const header = fixture.debugElement.query(By.directive(MdSortHeader)).nativeElement;

0 commit comments

Comments
 (0)