Skip to content

Commit 229645f

Browse files
authored
fix: generate sorter aria-label using text content (#2741)
1 parent b89541a commit 229645f

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

  • vaadin-grid-flow-parent
    • vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it
    • vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid

‎vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/SortingIT.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public void setInitialSortOrder_changeOrderFromServer_sortIndicatorsUpdated() {
7777
assertAscendingSorter("Age");
7878
}
7979

80+
@Test
81+
public void setInitialSortOrder_sorterAriaLabels() {
82+
findElement(By.id("sort-by-age")).click();
83+
List<TestBenchElement> sorters = grid.$("vaadin-grid-sorter").all();
84+
// The first sorter uses a ComponentRenderer so an aria-label can't be
85+
// generated
86+
Assert.assertEquals(false, sorters.get(0).hasAttribute("aria-label"));
87+
Assert.assertEquals("Sort by Age",
88+
sorters.get(1).getAttribute("aria-label"));
89+
}
90+
8091
@Test
8192
public void setInitialSortOrder_updateHeaderText_sortIndicatorsRemain() {
8293
findElement(By.id("sort-by-age")).click();

‎vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/AbstractColumn.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,14 @@ protected void setBaseHeaderTemplate(String headerTemplate) {
240240
protected String addGridSorter(String templateInnerHtml) {
241241
String escapedColumnId = HtmlUtils
242242
.escape(getBottomLevelColumn().getInternalId());
243+
244+
String textContent = org.jsoup.Jsoup.parse(templateInnerHtml).text();
245+
String sortBy = textContent.isBlank() ? ""
246+
: "aria-label='Sort by " + textContent + "'";
247+
243248
return String.format(
244-
"<vaadin-grid-sorter path='%s' aria-label='Sort by %s'>%s</vaadin-grid-sorter>",
245-
escapedColumnId, templateInnerHtml, templateInnerHtml);
249+
"<vaadin-grid-sorter path='%s' %s>%s</vaadin-grid-sorter>",
250+
escapedColumnId, sortBy, templateInnerHtml);
246251
}
247252

248253
/**

0 commit comments

Comments
 (0)