* The following WAI-ARIA attributes are added through this class:
*
* <ul>
- * <li>aria-rowcount (since 8.2)</li>
+ * <li>aria-rowcount (since 8.2)</li>
+ * <li>roles provided by {@link AriaGridRole} (since 8.2)</li>
* </ul>
*
* @since 8.2
getTable().setAttribute("aria-rowcount", String.valueOf(allRows));
}
+
+ /**
+ * Sets the {@code role} attribute to the given element.
+ *
+ * @param element element that should get the role attribute
+ * @param role role to be added
+ *
+ * @since
+ */
+ public void updateRole(final Element element, AriaGridRole role) {
+ element.setAttribute("role", role.getName());
+ }
+ }
+
+ /**
+ * Holds the currently used aria roles within the grid for rows and cells.
+ *
+ * @since
+ */
+ public enum AriaGridRole {
+
+ ROW("row"),
+ ROWHEADER("rowheader"),
+ ROWGROUP("rowgroup"),
+ GRIDCELL("gridcell"),
+ COLUMNHEADER("columnheader");
+
+ private final String name;
+
+
+ AriaGridRole(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Return the name of the {@link AriaGridRole}.
+ *
+ * @return String name to be used as role attribute
+ */
+ public String getName() {
+ return name;
+ }
}
public abstract class AbstractRowContainer implements RowContainer {
public AbstractRowContainer(
final TableSectionElement rowContainerElement) {
root = rowContainerElement;
+ ariaGridHelper.updateRole(root, AriaGridRole.ROWGROUP);
}
@Override
*/
protected abstract String getCellElementTagName();
+ /**
+ * Gets the role attribute of an element to represent a cell in a row.
+ * <p>
+ * Usually {@link AriaGridRole#GRIDCELL} except for a cell in
+ * the header.
+ *
+ * @return the role attribute for the element to represent cells
+ *
+ * @since
+ */
+ protected AriaGridRole getCellElementRole() {
+ return AriaGridRole.GRIDCELL;
+ }
+
+ /**
+ * Gets the role attribute of an element to represent a row in a grid.
+ * <p>
+ * Usually {@link AriaGridRole#ROW} except for a row in
+ * the header.
+ *
+ * @return the role attribute for the element to represent rows
+ *
+ * @since
+ */
+ protected AriaGridRole getRowElementRole() {
+ return AriaGridRole.ROW;
+ }
+
@Override
public EscalatorUpdater getEscalatorUpdater() {
return updater;
final TableRowElement tr = TableRowElement.as(DOM.createTR());
addedRows.add(tr);
tr.addClassName(getStylePrimaryName() + "-row");
+ ariaGridHelper.updateRole(tr, getRowElementRole());
for (int col = 0; col < columnConfiguration
.getColumnCount(); col++) {
final double colWidth = columnConfiguration
.getColumnWidthActual(col);
- final TableCellElement cellElem = createCellElement(
- colWidth);
+ final TableCellElement cellElem = createCellElement(colWidth);
tr.appendChild(cellElem);
-
// Set stylename and position if new cell is frozen
if (col < columnConfiguration.frozenColumns) {
cellElem.addClassName("frozen");
cellElem.getStyle().setWidth(width, Unit.PX);
}
cellElem.addClassName(getStylePrimaryName() + "-cell");
+ ariaGridHelper.updateRole(cellElem, getCellElementRole());
return cellElem;
}
return "th";
}
+ @Override
+ protected AriaGridRole getRowElementRole() {
+ return AriaGridRole.ROWHEADER;
+ }
+
+ @Override
+ protected AriaGridRole getCellElementRole() {
+ return AriaGridRole.COLUMNHEADER;
+ }
+
@Override
public void setStylePrimaryName(String primaryStyleName) {
super.setStylePrimaryName(primaryStyleName);
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest;
-import org.junit.Assert;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
grid = $(GridElement.class).first();
+ // default grid should contain at least one of each role
+ String gridHtml = grid.getHTML();
+ assertTrue("Grid should contain a role=\"rowheader\"", gridHtml.contains("role=\"rowheader\""));
+ assertTrue("Grid should contain a role=\"columnheader\"", gridHtml.contains("role=\"columnheader\""));
+ assertTrue("Grid should contain a role=\"row\"", gridHtml.contains("role=\"row\""));
+ assertTrue("Grid should contain a role=\"gridcell\"", gridHtml.contains("role=\"gridcell\""));
+ assertTrue("Grid should contain a role=\"rowgroup\"", gridHtml.contains("role=\"rowgroup\""));
+
// default with 1 header row and 2 body rows.
assertTrue("Grid should have 3 rows", containsRows(3));