Browse Source

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
tags/8.7.0.beta1
Anastasia Smirnova 5 years ago
parent
commit
7ecd44cfb6

+ 1
- 0
all/src/main/templates/release-notes.html View File

@@ -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>

+ 1
- 21
client/src/main/java/com/vaadin/client/ui/VNativeButton.java View File

@@ -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

+ 24
- 3
client/src/main/java/com/vaadin/client/ui/nativebutton/NativeButtonConnector.java View File

@@ -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;
}
}

+ 27
- 0
uitest/src/main/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.java View File

@@ -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);
}
}

+ 27
- 0
uitest/src/test/java/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClickTest.java View File

@@ -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());
}
}

Loading…
Cancel
Save