papegaaij commented on code in PR #1847: URL: https://github.com/apache/wicket/pull/1847#discussion_r4527441040
########## wicket-user-guide/src/main/asciidoc/accessibility/accessibility_5.adoc: ########## @@ -0,0 +1,166 @@ +//// + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +//// + +When an AJAX response updates a part of the page, nothing tells the user so: the browser does not reload the page, the screen reader does not announce anything, and the keyboard focus stays where it was, if the element that had it still exists. In this paragraph we will see how to move the focus where the user needs it and how to announce what has changed. + +=== How Wicket replaces components + +Wicket updates a component by replacing its whole element in the DOM with the newly rendered markup. If the replaced element had the focus, Wicket gives the focus back to the new element with the same markup id. This works as long as the component keeps its markup id, which is the case for a component that stays in the component tree and has _setOutputMarkupId(true)_. + +The focus is lost when the focused element does not come back: a button that deletes its own row, a link that hides itself, or the items of a repeater that creates new components at every render, because new components get new markup ids. The focus then falls back to the start of the document, and keyboard and screen reader users have to find their way back from there (success criterion 2.4.3). + +=== Moving the focus with focusComponent + +In those cases we move the focus ourselves with _focusComponent_ of _AjaxRequestTarget_. Its argument must have _setOutputMarkupId(true)_, otherwise the method throws an _IllegalArgumentException_. Wicket sets the focus after the components of the response have been replaced, so the component may be one that is part of the same update. + +The next example is a page with a list of addresses, each with a button to delete it. After a deletion the button is gone, so we move the focus to the heading of the list: + +*AddressesPage.html:* + +[source,html] +---- +<wicket:extend> + <h1 wicket:id="heading" tabindex="-1">Your addresses</h1> + + <div role="status"> + <span wicket:id="status"></span> + </div> + + <ul wicket:id="list"> + <li wicket:id="addresses"> + <span wicket:id="address"></span> + <button wicket:id="delete">Delete<span class="visually-hidden"> <span wicket:id="deleteWhat"></span></span></button> + </li> + </ul> +</wicket:extend> +---- + +*AddressesPage.java:* + +[source,java] +---- +public class AddressesPage extends BasePage { + + private final List<String> addresses = new ArrayList<>(List.of("Main Street 1", "Station Road 2")); + + public AddressesPage() { + IModel<String> statusMessage = Model.of(""); + Label status = new Label("status", statusMessage); + status.setOutputMarkupId(true); + add(status); + + WebMarkupContainer heading = new WebMarkupContainer("heading"); + heading.setOutputMarkupId(true); + add(heading); + + WebMarkupContainer list = new WebMarkupContainer("list"); + list.setOutputMarkupId(true); + add(list); + + list.add(new ListView<>("addresses", () -> addresses) { + @Override + protected void populateItem(ListItem<String> item) { + item.add(new Label("address", item.getModel())); + AjaxLink<String> delete = new AjaxLink<>("delete", item.getModel()) { + @Override + public void onClick(AjaxRequestTarget target) { + addresses.remove(getModelObject()); + statusMessage.setObject("Address " + getModelObject() + " removed."); Review Comment: The item model of a `ListView` is a `ListItemModel`, which reads `list.get(index)` on every `getObject()`. After the `remove`, the same index points to the next address: deleting "Main Street 1" announces "Address Station Road 2 removed.", and deleting the last remaining address throws an `IndexOutOfBoundsException`. Taking the address before removing it fixes both: ```suggestion String address = getModelObject(); addresses.remove(address); statusMessage.setObject("Address " + address + " removed."); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
