Skip to content

Commit

Permalink
feat: add setAriaLabel to Grid (#6878)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
DiegoCardoso and web-padawan authored Dec 4, 2024
1 parent b0698f2 commit 78da2f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3671,6 +3671,30 @@ private void updateContextMenuTargetItem(String key, String colId) {
getElement().setProperty("_contextMenuTargetColumnId", colId);
}

/**
* Set the aria-label of the component to the given text.
*
* @param ariaLabel
* the aria-label text to set or {@code null} to clear
*/
public void setAriaLabel(String ariaLabel) {
if (ariaLabel == null) {
getElement().removeProperty("accessibleName");
} else {
getElement().setProperty("accessibleName", ariaLabel);
}
}

/**
* Gets the aria-label of the component.
*
* @return an optional aria-label of the component if no aria-label has been
* set
*/
public Optional<String> getAriaLabel() {
return Optional.ofNullable(getElement().getProperty("accessibleName"));
}

/**
* Adds a new context-menu for this grid.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public void setAllRowsVisible_setSmallPageSize_callSetRequestedRangeWithLengthLa
callSetRequestedRange(grid, 0, 600);
}

@Test
public void setAriaLabel() {
final Grid<String> grid = new Grid<>();
grid.setAriaLabel("test");
Assert.assertTrue(grid.getAriaLabel().isPresent());
Assert.assertEquals("test", grid.getAriaLabel().get());
Assert.assertEquals("test",
grid.getElement().getProperty("accessibleName"));

grid.setAriaLabel(null);
Assert.assertFalse(grid.getAriaLabel().isPresent());
Assert.assertFalse(grid.getElement().hasProperty("accessibleName"));
}

private void callSetRequestedRange(Grid<String> grid, int start,
int length) {
try {
Expand Down

0 comments on commit 78da2f6

Please sign in to comment.