Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setAriaLabel to Grid #6878

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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