Skip to content

Commit 63f713f

Browse files
andrewseguinjosephperrott
authored andcommitted
fix(sort): add aria-sort to host when sorted (#6891)
1 parent 8c3ba34 commit 63f713f

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-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, SkipSelf, Optional} from '@angular/core';
1010
import {Subject} from 'rxjs';
11-
import {SortDirection} from './sort-direction';
1211

1312
/**
1413
* To modify the labels and text displayed, create a new instance of MatSortHeaderIntl and
@@ -26,11 +25,6 @@ export class MatSortHeaderIntl {
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
}
3529
/** @docs-private */
3630
export function MAT_SORT_HEADER_INTL_PROVIDER_FACTORY(parentIntl: MatSortHeaderIntl) {

src/lib/sort/sort-header.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,3 @@
2424
</div>
2525
</div>
2626
</div>
27-
28-
<span class="cdk-visually-hidden" *ngIf="_isSorted()">
29-
&nbsp;{{_intl.sortDescriptionLabel(id, _sort.direction)}}
30-
</span>

src/lib/sort/sort-header.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface ArrowViewStateTransition {
7070
'(mouseenter)': '_setIndicatorHintVisible(true)',
7171
'(longpress)': '_setIndicatorHintVisible(true)',
7272
'(mouseleave)': '_setIndicatorHintVisible(false)',
73+
'[attr.aria-sort]': '_getAriaSortAttribute()',
7374
'[class.mat-sort-header-disabled]': '_isDisabled()',
7475
},
7576
encapsulation: ViewEncapsulation.None,
@@ -263,4 +264,16 @@ export class MatSortHeader extends _MatSortHeaderMixinBase implements MatSortabl
263264
_isDisabled() {
264265
return this._sort.disabled || this.disabled;
265266
}
267+
268+
/**
269+
* Gets the aria-sort attribute that should be applied to this sort header. If this header
270+
* is not sorted, returns null so that the attribute is removed from the host element. Aria spec
271+
* says that the aria-sort property should only be present on one header at a time, so removing
272+
* ensures this is true.
273+
*/
274+
_getAriaSortAttribute() {
275+
if (!this._isSorted()) { return null; }
276+
277+
return this._sort.direction == 'asc' ? 'ascending' : 'descending';
278+
}
266279
}

src/lib/sort/sort.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ describe('MatSort', () => {
347347
expect(header._showIndicatorHint).toBeFalsy();
348348
});
349349

350+
it('should apply the aria-sort label to the header when sorted', () => {
351+
const sortHeaderElement = fixture.nativeElement.querySelector('#defaultA');
352+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe(null);
353+
354+
component.sort('defaultA');
355+
fixture.detectChanges();
356+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('ascending');
357+
358+
component.sort('defaultA');
359+
fixture.detectChanges();
360+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('descending');
361+
362+
component.sort('defaultA');
363+
fixture.detectChanges();
364+
expect(sortHeaderElement.getAttribute('aria-sort')).toBe(null);
365+
});
366+
350367
it('should re-render when the i18n labels have changed',
351368
inject([MatSortHeaderIntl], (intl: MatSortHeaderIntl) => {
352369
const header = fixture.debugElement.query(By.directive(MatSortHeader)).nativeElement;

src/lib/table/table.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('MatTable', () => {
161161
component.sort.sort(component.sortHeader);
162162
fixture.detectChanges();
163163
expectTableToMatchContent(tableElement, [
164-
['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'],
164+
['Column A', 'Column B', 'Column C'],
165165
['a_1', 'b_1', 'c_1'],
166166
['a_2', 'b_2', 'c_2'],
167167
['a_3', 'b_3', 'c_3'],
@@ -171,7 +171,7 @@ describe('MatTable', () => {
171171
component.sort.sort(component.sortHeader);
172172
fixture.detectChanges();
173173
expectTableToMatchContent(tableElement, [
174-
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
174+
['Column A', 'Column B', 'Column C'],
175175
['a_3', 'b_3', 'c_3'],
176176
['a_2', 'b_2', 'c_2'],
177177
['a_1', 'b_1', 'c_1'],
@@ -189,7 +189,7 @@ describe('MatTable', () => {
189189
component.sort.direction = '';
190190
component.sort.sort(component.sortHeader);
191191
expectTableToMatchContent(tableElement, [
192-
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
192+
['Column A', 'Column B', 'Column C'],
193193
['a_1', 'b_1', 'c_1'],
194194
['a_3', 'b_3', 'c_3'],
195195
['a_2', 'b_2', 'c_2'],
@@ -204,7 +204,7 @@ describe('MatTable', () => {
204204

205205
// Expect that empty string row comes before the other values
206206
expectTableToMatchContent(tableElement, [
207-
['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'],
207+
['Column A', 'Column B', 'Column C'],
208208
['', 'b_1', 'c_1'],
209209
['a_2', 'b_2', 'c_2'],
210210
['a_3', 'b_3', 'c_3'],
@@ -214,7 +214,7 @@ describe('MatTable', () => {
214214
component.sort.sort(component.sortHeader);
215215
fixture.detectChanges();
216216
expectTableToMatchContent(tableElement, [
217-
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
217+
['Column A', 'Column B', 'Column C'],
218218
['a_3', 'b_3', 'c_3'],
219219
['a_2', 'b_2', 'c_2'],
220220
['', 'b_1', 'c_1'],
@@ -229,7 +229,7 @@ describe('MatTable', () => {
229229
component.sort.sort(component.sortHeader);
230230
fixture.detectChanges();
231231
expectTableToMatchContent(tableElement, [
232-
['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'],
232+
['Column A', 'Column B', 'Column C'],
233233
['', 'b_1', 'c_1'],
234234
['a_2', 'b_2', 'c_2'],
235235
['a_3', 'b_3', 'c_3'],
@@ -240,7 +240,7 @@ describe('MatTable', () => {
240240
component.sort.sort(component.sortHeader);
241241
fixture.detectChanges();
242242
expectTableToMatchContent(tableElement, [
243-
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
243+
['Column A', 'Column B', 'Column C'],
244244
['a_3', 'b_3', 'c_3'],
245245
['a_2', 'b_2', 'c_2'],
246246
['', 'b_1', 'c_1'],

0 commit comments

Comments
 (0)