diff options
author | Anastasia Smirnova <anasmi@utu.fi> | 2018-12-14 13:06:00 +0200 |
---|---|---|
committer | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2018-12-14 13:06:00 +0200 |
commit | 7ecd44cfb6a2d08d67f72e38cadaba8fab04962d (patch) | |
tree | b524ed84a8d3af04e4500ff2053fa5b2a68a4853 | |
parent | 891bb534de30ed39e463af88f67e7f5bbf94e3f1 (diff) | |
download | vaadin-framework-7ecd44cfb6a2d08d67f72e38cadaba8fab04962d.tar.gz vaadin-framework-7ecd44cfb6a2d08d67f72e38cadaba8fab04962d.zip |
Move onClick logic to the Connector (#11367)
* Move onClick logic to the Connector
Move onClick event handling from 'VNativeButton' to NativeButtonConnector. Now works as for regular button.
Also, adding propagation of the `enabled` value to the state on disableOnClick being true
Fixes https://github.com/vaadin/framework/issues/11188
5 files changed, 80 insertions, 24 deletions
diff --git a/all/src/main/templates/release-notes.html b/all/src/main/templates/release-notes.html index d5b79557c5..cb453773c8 100644 --- a/all/src/main/templates/release-notes.html +++ b/all/src/main/templates/release-notes.html @@ -103,6 +103,7 @@ <ul> <li><tt>VaadinIcons.SEARCH_MINUS</tt> and <tt>VaadinIcons.SEARCH_PLUS</tt> codes were changed due to typo fix</li> + <li>Public <tt>disableOnClick</tt> variable in <tt>VNatiButton</tt> is removed due to the refactoring</li> </ul> <h2>For incompatible or behavior-altering changes in 8.6, please see <a href="https://vaadin.com/download/release/8.6/8.6.0/release-notes.html#incompatible">8.6 release notes</a></h2> diff --git a/client/src/main/java/com/vaadin/client/ui/VNativeButton.java b/client/src/main/java/com/vaadin/client/ui/VNativeButton.java index ac0f728ffe..3df97eb762 100644 --- a/client/src/main/java/com/vaadin/client/ui/VNativeButton.java +++ b/client/src/main/java/com/vaadin/client/ui/VNativeButton.java @@ -24,11 +24,8 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Button; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.MouseEventDetailsBuilder; -import com.vaadin.client.StyleConstants; import com.vaadin.client.Util; import com.vaadin.client.WidgetUtil.ErrorUtil; -import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.button.ButtonServerRpc; public class VNativeButton extends Button @@ -59,13 +56,10 @@ public class VNativeButton extends Button * mouse while clicking it. In this case mouse leaves the button without * moving. */ - private boolean clickPending; + public boolean clickPending; private boolean cancelNextClick = false; - /** For internal use only. May be removed or replaced in the future. */ - public boolean disableOnClick = false; - public VNativeButton() { setStyleName(CLASSNAME); @@ -145,20 +139,6 @@ public class VNativeButton extends Button // (#11854) setFocus(true); } - if (disableOnClick) { - setEnabled(false); - // FIXME: This should be moved to NativeButtonConnector along with - // buttonRpcProxy - addStyleName(StyleConstants.DISABLED); - buttonRpcProxy.disableOnClick(); - } - - // Add mouse details - MouseEventDetails details = MouseEventDetailsBuilder - .buildMouseEventDetails(event.getNativeEvent(), getElement()); - buttonRpcProxy.click(details); - - clickPending = false; } @Override diff --git a/client/src/main/java/com/vaadin/client/ui/nativebutton/NativeButtonConnector.java b/client/src/main/java/com/vaadin/client/ui/nativebutton/NativeButtonConnector.java index 2bf1829ad8..6616dc2d5f 100644 --- a/client/src/main/java/com/vaadin/client/ui/nativebutton/NativeButtonConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/nativebutton/NativeButtonConnector.java @@ -15,19 +15,24 @@ */ package com.vaadin.client.ui.nativebutton; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.vaadin.client.MouseEventDetailsBuilder; import com.vaadin.client.VCaption; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.ConnectorFocusAndBlurHandler; import com.vaadin.client.ui.Icon; import com.vaadin.client.ui.VNativeButton; +import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.button.ButtonServerRpc; import com.vaadin.shared.ui.button.NativeButtonState; import com.vaadin.ui.NativeButton; @Connect(NativeButton.class) -public class NativeButtonConnector extends AbstractComponentConnector { +public class NativeButtonConnector extends AbstractComponentConnector + implements ClickHandler { @Override public void init() { @@ -37,6 +42,7 @@ public class NativeButtonConnector extends AbstractComponentConnector { getWidget().client = getConnection(); getWidget().paintableId = getConnectorId(); + getWidget().addClickHandler(this); ConnectorFocusAndBlurHandler.addHandlers(this); } @@ -49,8 +55,6 @@ public class NativeButtonConnector extends AbstractComponentConnector { public void onStateChanged(StateChangeEvent stateChangeEvent) { super.onStateChanged(stateChangeEvent); - getWidget().disableOnClick = getState().disableOnClick; - // Set text VCaption.setCaptionText(getWidget(), getState()); @@ -77,4 +81,21 @@ public class NativeButtonConnector extends AbstractComponentConnector { public NativeButtonState getState() { return (NativeButtonState) super.getState(); } + + @Override + public void onClick(ClickEvent event) { + if (getState().disableOnClick) { + getState().enabled = false; + super.updateEnabledState(false); + getRpcProxy(ButtonServerRpc.class).disableOnClick(); + } + + // Add mouse details + MouseEventDetails details = MouseEventDetailsBuilder + .buildMouseEventDetails(event.getNativeEvent(), + getWidget().getElement()); + getRpcProxy(ButtonServerRpc.class).click(details); + + getWidget().clickPending = false; + } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.java b/uitest/src/main/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.java new file mode 100644 index 0000000000..f5ae65d392 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.java @@ -0,0 +1,27 @@ +package com.vaadin.tests.components.nativebutton; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.Button; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class NativeButtonDisableOnClick extends AbstractTestUI { + public static String UPDATED_CAPTION = "Updated caption"; + + @Override + protected void setup(VaadinRequest request) { + Button button = new NativeButton("Click Me"); + button.setId("buttonId"); + button.setDisableOnClick(true); + button.addClickListener(e -> { + if (UPDATED_CAPTION.equals(button.getCaption())) { + button.setCaption("Failed"); + } else { + button.setCaption(UPDATED_CAPTION); + } + }); + addComponent(button); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClickTest.java b/uitest/src/test/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClickTest.java new file mode 100644 index 0000000000..34f4590dd4 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClickTest.java @@ -0,0 +1,27 @@ +package com.vaadin.tests.components.nativebutton; + +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static org.junit.Assert.assertEquals; + +public class NativeButtonDisableOnClickTest extends MultiBrowserTest { + + @Test + public void testButtonIsDisabled() { + openTestURL(); + WebElement button = findElement(By.id("buttonId")); + assertEquals(true, button.isEnabled()); + + button.click(); + assertEquals(NativeButtonDisableOnClick.UPDATED_CAPTION, + button.getText()); + assertEquals(false, button.isEnabled()); + + button.click(); + assertEquals(NativeButtonDisableOnClick.UPDATED_CAPTION, + button.getText()); + } +} |