diff options
author | Olli Tietäväinen <ollit@vaadin.com> | 2018-02-12 15:14:49 +0200 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2018-02-12 15:14:49 +0200 |
commit | c641c7f56fcccada9268cbe0979fc66726f637d3 (patch) | |
tree | 4d177cfefb3c33f917518efb9e060a8ca9c6017f /client | |
parent | 201c4f1d77c96b2284773309a6f8c836e159a2d4 (diff) | |
download | vaadin-framework-c641c7f56fcccada9268cbe0979fc66726f637d3.tar.gz vaadin-framework-c641c7f56fcccada9268cbe0979fc66726f637d3.zip |
Implement focus handing in RadioButtonGroup and CheckboxGroup (#10440)
Fixes #10429
* implement focus handing in RadioButtonGroup, fixes #10429
* Merge branch 'master' of https://github.com/vaadin/framework into radiobuttongroup-focus
* merge
* Merge branch 'master' of https://github.com/vaadin/framework into radiobuttongroup-focus
* fix initial focus handling also on CheckBoxGroup and add tests
* add license headers
* Merge branch 'master' of https://github.com/vaadin/framework into radiobuttongroup-focus
* changed client to use lambdas and refactored focus testing to parent class
* made FocusTest abstract
* Merge branch 'master' of https://github.com/vaadin/framework into radiobuttongroup-focus
* don't allow focusing on disabled items & refactor focusing first item
Diffstat (limited to 'client')
3 files changed, 56 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VCheckBoxGroup.java b/client/src/main/java/com/vaadin/client/ui/VCheckBoxGroup.java index 93975d2926..f78c98ee27 100644 --- a/client/src/main/java/com/vaadin/client/ui/VCheckBoxGroup.java +++ b/client/src/main/java/com/vaadin/client/ui/VCheckBoxGroup.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.function.BiConsumer; import com.google.gwt.aria.client.Roles; +import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; @@ -235,4 +236,14 @@ public class VCheckBoxGroup extends FocusableFlowPanelComposite .remove(selectionChanged); } + /** + * Set focus to the first check box. + */ + @Override + public void focus() { + // If focus is set on creation, need to wait until options are populated + Scheduler.get().scheduleDeferred(() -> { + getWidget().focusFirstEnabledChild(); + }); + } } diff --git a/client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java b/client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java index 91ea471543..479e41a9b5 100644 --- a/client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java +++ b/client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java @@ -24,6 +24,7 @@ import java.util.Optional; import java.util.function.Consumer; import com.google.gwt.aria.client.Roles; +import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; @@ -40,6 +41,7 @@ import com.vaadin.client.widgets.FocusableFlowPanelComposite; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.ui.ListingJsonConstants; + import elemental.json.JsonObject; /** @@ -272,8 +274,29 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite } /** + * Set focus to the selected radio button (or first radio button if there is + * no selection). + */ + @Override + public void focus() { + // If focus is set on creation, need to wait until options are populated + Scheduler.get().scheduleDeferred(() -> { + // if there's a selected radio button, focus it + for (String key : keyToOptions.keySet()) { + RadioButton radioButton = keyToOptions.get(key); + if (radioButton != null && radioButton.getValue()) { + radioButton.setFocus(true); + return; + } + } + // otherwise focus the first enabled child + getWidget().focusFirstEnabledChild(); + }); + } + + /** * Updates the selected state of a radio button. - * + * * @param radioButton * the radio button to update * @param value @@ -282,5 +305,6 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite protected void updateItemSelection(RadioButton radioButton, boolean value) { radioButton.setValue(value); radioButton.setStyleName(CLASSNAME_OPTION_SELECTED, value); + } } diff --git a/client/src/main/java/com/vaadin/client/widgets/ChildFocusAwareFlowPanel.java b/client/src/main/java/com/vaadin/client/widgets/ChildFocusAwareFlowPanel.java index d1f7882dff..a0fc6a1499 100644 --- a/client/src/main/java/com/vaadin/client/widgets/ChildFocusAwareFlowPanel.java +++ b/client/src/main/java/com/vaadin/client/widgets/ChildFocusAwareFlowPanel.java @@ -153,6 +153,26 @@ public class ChildFocusAwareFlowPanel extends FocusableFlowPanel } } + /** + * Put focus in the first child Widget that can be focused and is not + * disabled. + */ + public void focusFirstEnabledChild() { + for (int i = 0; i < getWidgetCount(); i++) { + Widget widget = getWidget(i); + if (!(widget instanceof FocusWidget)) { + continue; + } + FocusWidget focusableChild = (FocusWidget) widget; + if (focusableChild.isEnabled()) { + focusableChild.setFocus(true); + break; + } + + } + + } + private void addHandlers(Widget widget) { if (focusRegistrations.containsKey(widget)) { assert blurRegistrations.containsKey(widget); |