Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vaadin/flow-components into feat/…
Browse files Browse the repository at this point in the history
…themable-menu-items
  • Loading branch information
Erik Lumme committed Sep 1, 2021
2 parents 548a16a + 13c08b4 commit b89de56
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2021 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.flow.component.button.tests;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route("vaadin-button/detach-reattach-disable-on-click-button")
public class DetachReattachDisableOnClickButtonPage extends Div {

public DetachReattachDisableOnClickButtonPage() {
Button disableOnClickButton = new Button("Disable on click");
disableOnClickButton.setId("disable-on-click");
disableOnClickButton.setDisableOnClick(true);
disableOnClickButton.addClickListener(event -> {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
event.getSource().setEnabled(true);
}
});

Button removeFromViewButton = new Button("Remove from view",
event -> remove(disableOnClickButton));
removeFromViewButton.setId("remove-from-view");
Button addToViewButton = new Button("Add to view",
event -> add(disableOnClickButton));
addToViewButton.setId("add-to-view");

add(removeFromViewButton, addToViewButton, disableOnClickButton);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2000-2021 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.flow.component.button.tests;

import com.vaadin.flow.component.button.testbench.ButtonElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

@TestPath("vaadin-button/detach-reattach-disable-on-click-button")
public class DetachReattachDisableOnClickButtonIT extends AbstractComponentIT {

private ButtonElement getDisableOnClickButton() {
return $(ButtonElement.class).id("disable-on-click");
}

private void assertDisableOnClickButtonEnabled(
ButtonElement disableOnClickButton) {
Assert.assertTrue("'Disable on click' button should be enabled",
disableOnClickButton.isEnabled());
}

private void assertDisableOnClickButtonDisabled(
ButtonElement disableOnClickButton) {
Assert.assertFalse("'Disable on click' button should be disabled",
disableOnClickButton.isEnabled());
}

private void clickDisableOnClickButton(ButtonElement disableOnClickButton) {
getCommandExecutor().disableWaitForVaadin();

// Click 'Disable on click' button
disableOnClickButton.click();

// Check 'Disable on click' button is disabled
assertDisableOnClickButtonDisabled(disableOnClickButton);

waitUntil(ExpectedConditions.elementToBeClickable(
$(ButtonElement.class).id("disable-on-click")), 2000);

// Check 'Disable on click' button is enabled again
assertDisableOnClickButtonEnabled(disableOnClickButton);

getCommandExecutor().enableWaitForVaadin();
}

@Test
public void testDetachingAndReattachingShouldKeepDisabledOnClick() {
open();

ButtonElement disableOnClickButton = getDisableOnClickButton();

// Check 'Disable on click' button should be enabled
assertDisableOnClickButtonEnabled(disableOnClickButton);

// Remove 'Disable on click' button
ButtonElement removeFromViewButton = $(ButtonElement.class)
.id("remove-from-view");
removeFromViewButton.click();

waitUntil(ExpectedConditions
.numberOfElementsToBe(By.id("disable-on-click"), 0), 2000);

// Re-attach 'Disable on click" button
ButtonElement addToViewButton = $(ButtonElement.class)
.id("add-to-view");
addToViewButton.click();

waitUntil(ExpectedConditions
.numberOfElementsToBe(By.id("disable-on-click"), 1), 2000);

disableOnClickButton = getDisableOnClickButton();

// Check 'Disable on click' button is enabled
assertDisableOnClickButtonEnabled(disableOnClickButton);

// Click 'Disable on click' button
clickDisableOnClickButton(disableOnClickButton);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
*/
package com.vaadin.flow.component.button;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEventListener;
Expand All @@ -32,6 +26,12 @@
import com.vaadin.flow.internal.nodefeature.ElementAttributeMap;
import com.vaadin.flow.internal.nodefeature.NodeFeature;
import com.vaadin.flow.shared.Registration;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Server-side component for the <code>vaadin-button</code> element.
Expand All @@ -44,7 +44,6 @@ public class Button extends GeneratedVaadinButton<Button>
private Component iconComponent;
private boolean iconAfterText;
private boolean disableOnClick = false;
private boolean disableOnClickConfigured = false;

// Register immediately as first listener
private Registration disableListener = addClickListener(
Expand Down Expand Up @@ -320,7 +319,6 @@ public boolean isAutofocus() {
public void setDisableOnClick(boolean disableOnClick) {
this.disableOnClick = disableOnClick;
if (disableOnClick) {
initDisableOnClick();
getElement().setAttribute("disableOnClick", "true");
} else {
getElement().removeAttribute("disableOnClick");
Expand All @@ -341,13 +339,10 @@ public boolean isDisableOnClick() {
* if server-side handling takes some time.
*/
private void initDisableOnClick() {
if (!disableOnClickConfigured) {
getElement().executeJs("var disableEvent = function () {"
+ "if($0.getAttribute('disableOnClick')){"
+ " $0.setAttribute('disabled', 'true');" + "}" + "};"
+ "$0.addEventListener('click', disableEvent)");
disableOnClickConfigured = true;
}
getElement().executeJs("var disableEvent = function () {"
+ "if($0.getAttribute('disableOnClick')){"
+ " $0.setAttribute('disabled', 'true');" + "}" + "};"
+ "$0.addEventListener('click', disableEvent)");
}

private void updateIconSlot() {
Expand Down Expand Up @@ -421,4 +416,10 @@ private void doDisableOnClick() {
}));
}
}

@Override
protected void onAttach(AttachEvent attachEvent) {
initDisableOnClick();
}

}
2 changes: 1 addition & 1 deletion vaadin-flow-components-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>LATEST</version>
<version>4.4.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit b89de56

Please sign in to comment.