From 78da2f6470461a8a1c2666ff3ee8dfb5830b38cf Mon Sep 17 00:00:00 2001 From: Diego Cardoso Date: Wed, 4 Dec 2024 15:50:48 +0200 Subject: [PATCH] feat: add `setAriaLabel` to `Grid` (#6878) Co-authored-by: Serhii Kulykov --- .../com/vaadin/flow/component/grid/Grid.java | 24 +++++++++++++++++++ .../vaadin/flow/component/grid/GridTest.java | 14 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/Grid.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/Grid.java index 259b0e511cf..025d541b4b0 100755 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/Grid.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/Grid.java @@ -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 getAriaLabel() { + return Optional.ofNullable(getElement().getProperty("accessibleName")); + } + /** * Adds a new context-menu for this grid. * diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/GridTest.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/GridTest.java index 771267b6aaa..95e4e4fbc7b 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/GridTest.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/GridTest.java @@ -131,6 +131,20 @@ public void setAllRowsVisible_setSmallPageSize_callSetRequestedRangeWithLengthLa callSetRequestedRange(grid, 0, 600); } + @Test + public void setAriaLabel() { + final Grid 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 grid, int start, int length) { try {