diff options
author | Knoobie <Knoobie@gmx.de> | 2017-10-04 07:51:31 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-10-04 08:51:31 +0300 |
commit | 80336d30ed12d53f24c01de5c4b6274ccc094b3b (patch) | |
tree | dd3994504c85feade1810478faa372f5229ffefc | |
parent | bf690d31eb986ef1803324ff2bd628104a7cb0fe (diff) | |
download | vaadin-framework-80336d30ed12d53f24c01de5c4b6274ccc094b3b.tar.gz vaadin-framework-80336d30ed12d53f24c01de5c4b6274ccc094b3b.zip |
Add role="grid" and aria-multiselectable to grid (#10009)
Also adds aria-selected for grid rows.
9 files changed, 169 insertions, 3 deletions
diff --git a/all/src/main/templates/release-notes.html b/all/src/main/templates/release-notes.html index 8db3ef8670..be04a7247a 100644 --- a/all/src/main/templates/release-notes.html +++ b/all/src/main/templates/release-notes.html @@ -120,6 +120,7 @@ <li>Error indicators are now <tt><span class="v-errorindicator"></span></tt> elements.</li> <li><tt>Embedded</tt> is not a <tt>LegacyComponent</tt> anymore.</li> <li><tt>Notification</tt> method <tt>show</tt> returns <tt>Notification</tt>, instead of <tt>void</tt>.</li> + <li>The client side <tt>SelectionModel</tt> interface has a new method <tt>isMultiSelectionAllowed</tt>.</li> <h2>For incompatible or behavior-altering changes in 8.1, please see <a href="https://vaadin.com/download/release/8.1/8.1.0/release-notes.html#incompatible">8.1 release notes</a></h2> diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java index 92b858cb97..79a3cac92b 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java @@ -121,6 +121,10 @@ public class MultiSelectionModelConnector return isSelectionAllowed; } + @Override + public boolean isMultiSelectionAllowed() { + return true; + } } @Override diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java index 560ccb2d85..2320edc8e0 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java @@ -82,6 +82,11 @@ public class SingleSelectionModelConnector return isSelectionAllowed; } + @Override + public boolean isMultiSelectionAllowed() { + return false; + } + /** * Sets whether it's allowed to deselect the selected row through the * UI. Deselection is allowed by default. diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModel.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModel.java index 17763b0a94..513ee4949a 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModel.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModel.java @@ -58,6 +58,11 @@ public interface SelectionModel<T> { public boolean isSelectionAllowed() { return false; } + + @Override + public boolean isMultiSelectionAllowed() { + return false; + } } /** @@ -116,6 +121,16 @@ public interface SelectionModel<T> { boolean isSelectionAllowed(); /** + * Checks if the user is allowed to have more than on item selected. + * <p> + * + * @return <code>true</code> if the user is allowed to select multiple items, + * <code>false</code> otherwise + * @since 8.2 + */ + boolean isMultiSelectionAllowed(); + + /** * Gets the selected state from a given grid row json object. This is a * helper method for grid selection models. * diff --git a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java index 016c701deb..cf9d7ea833 100644 --- a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java +++ b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java @@ -45,6 +45,13 @@ public class TreeGrid extends Grid<JsonObject> { private String depthStyleNamePrefix; /** + * Creates a new instance. + */ + public TreeGrid() { + setAriaRole("treegrid"); + } + + /** * Body updater that adds additional style to each row containing depth * information inside the hierarchy. */ diff --git a/client/src/main/java/com/vaadin/client/widgets/Escalator.java b/client/src/main/java/com/vaadin/client/widgets/Escalator.java index a46450fe07..640304e828 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Escalator.java +++ b/client/src/main/java/com/vaadin/client/widgets/Escalator.java @@ -6792,6 +6792,16 @@ public class Escalator extends Widget return tableWrapper; } + /** + * Returns the {@code <table />} element of the grid. + * + * @return the table element + * @since 8.2 + */ + public Element getTable() { + return getTableWrapper().getFirstChildElement(); + } + private Element getSubPartElementTableStructure(SubPartArguments args) { String type = args.getType(); diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index e4ceac93f8..81f6756930 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -5597,10 +5597,15 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, rowReference.set(rowIndex, rowData, rowElement); - if (hasData) { - setStyleName(rowElement, rowSelectedStyleName, - isSelected(rowData)); + boolean isSelected = hasData && isSelected(rowData); + if (Grid.this.selectionModel.isSelectionAllowed()) { + rowElement.setAttribute("aria-selected", String.valueOf(isSelected)); + } else { + rowElement.removeAttribute("aria-selected"); + } + if (hasData) { + setStyleName(rowElement, rowSelectedStyleName, isSelected); if (rowStyleGenerator != null) { try { String rowStylename = rowStyleGenerator @@ -6148,6 +6153,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, cellFocusHandler = new CellFocusHandler(); setStylePrimaryName(STYLE_NAME); + setAriaRole("grid"); escalator.getHeader().setEscalatorUpdater(createHeaderUpdater()); escalator.getBody().setEscalatorUpdater(createBodyUpdater()); @@ -6309,6 +6315,17 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, } /** + * Adds the given role as 'role="$param"' to the {@code <table />} element + * of the grid. + * + * @param role the role param + * @since 8.2 + */ + protected void setAriaRole(String role){ + escalator.getTable().setAttribute("role", role); + } + + /** * Creates the escalator updater used to update the header rows in this * grid. The updater is invoked when header rows or columns are added or * removed, or the content of existing header cells is changed. @@ -7971,6 +7988,13 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, setSelectColumnRenderer(null); } + if (this.selectionModel.isMultiSelectionAllowed()) { + escalator.getTable().setAttribute("aria-multiselectable", "true"); + } else if (this.selectionModel.isSelectionAllowed()) { + escalator.getTable().setAttribute("aria-multiselectable", "false"); + } else { + escalator.getTable().removeAttribute("aria-multiselectable"); + } // Refresh rendered rows to update selection, if it has changed requestRefreshBody(); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridAriaMultiselectable.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridAriaMultiselectable.java new file mode 100644 index 0000000000..144e518c43 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridAriaMultiselectable.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +import com.vaadin.data.ValueProvider; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; + +/** + * @author Vaadin Ltd + * + */ +public class GridAriaMultiselectable extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + Grid<String> grid = new Grid<>(); + grid.addColumn(ValueProvider.identity()); + grid.setItems("a", "b"); + grid.setSelectionMode(SelectionMode.NONE); + + addComponent(grid); + + Button singleSelectBtn = new Button("SingleSelect", event -> { + grid.setSelectionMode(SelectionMode.SINGLE); + }); + addComponent(singleSelectBtn); + + Button multiSelectBtn = new Button("MultiSelect", event -> { + grid.setSelectionMode(SelectionMode.MULTI); + }); + addComponent(multiSelectBtn); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridAriaMultiselectableTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridAriaMultiselectableTest.java new file mode 100644 index 0000000000..eb6cd44f50 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridAriaMultiselectableTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +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; + +/** + * @author Vaadin Ltd + */ +public class GridAriaMultiselectableTest extends SingleBrowserTest { + + @Test + public void checkAriaMultiselectable() { + openTestURL(); + + GridElement grid = $(GridElement.class).first(); + + Assert.assertTrue("Grid should have the role 'grid'", + grid.getHTML().contains("role=\"grid\"")); + Assert.assertFalse("Grid should not have aria-multiselectable", + grid.getHTML().contains("aria-multiselectable")); + + $(ButtonElement.class).caption("SingleSelect").first().click(); + + Assert.assertTrue("Grid should have aria-multiselectable 'false'", + grid.getHTML().contains("aria-multiselectable=\"false\"")); + + $(ButtonElement.class).caption("MultiSelect").first().click(); + + Assert.assertTrue("Grid should have aria-multiselectable 'true'", + grid.getHTML().contains("aria-multiselectable=\"true\"")); + } +} |