Skip to content

Commit f861c42

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

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,4 @@ export class MdSortHeaderIntl {
2626
sortButtonLabel = (id: string) => {
2727
return `Change sorting for ${id}`;
2828
}
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-
}
3429
}

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {getMdSortHeaderNotContainedWithinMdSortError} from './sort-errors';
4646
styleUrls: ['sort-header.css'],
4747
host: {
4848
'(click)': '_sort.sort(this)',
49+
'[attr.aria-sort]': '_getAriaSortAttribute()',
4950
'[class.mat-sort-header-sorted]': '_isSorted()',
5051
},
5152
encapsulation: ViewEncapsulation.None,
@@ -113,4 +114,16 @@ export class MdSortHeader implements MdSortable {
113114
_isSorted() {
114115
return this._sort.active == this.id && this._sort.direction;
115116
}
117+
118+
/**
119+
* Returns the aria-sort attribute that should be applied to this sort header. If this header
120+
* is not sorted, returns null so that the attribute is removed from the host element. Aria spec
121+
* says that the aria-sort property should only be present on one header at a time, so removing
122+
* ensures this is true.
123+
*/
124+
_getAriaSortAttribute() {
125+
if (!this._isSorted()) { return null; }
126+
127+
return this._sort.direction == 'asc' ? 'ascending' : 'descending';
128+
}
116129
}

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)