Skip to content

Commit

Permalink
fix: change enabled state of radio-buttons when enabled state of radi…
Browse files Browse the repository at this point in the history
…o-button-group changes (#774) (#799)

ItemEnableProvider is no longer ignored when re-enabling a disabled radio-button-group

Co-authored-by: David Sosa <[email protected]>
  • Loading branch information
vaadin-bot and sosa-vaadin authored Apr 12, 2021
1 parent 045a146 commit b817779
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
public class DisabledItemsPage extends Div {

public DisabledItemsPage() {
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup();
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setId("button-group");
radioButtonGroup.setItemEnabledProvider("one"::equals);
radioButtonGroup.setEnabled(false);

NativeButton nativeButton = new NativeButton("add",
event -> radioButtonGroup.setItems("one", "two"));
nativeButton.setId("add-button");

add(radioButtonGroup, nativeButton);
NativeButton enableButton = new NativeButton("enable",
event -> radioButtonGroup.setEnabled(true));
enableButton.setId("enable-button");

add(radioButtonGroup, nativeButton, enableButton);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ public void set_items_to_disabled_group_should_be_disabled() {
Boolean.TRUE.toString(), button.getAttribute("disabled"));
}
}

@Test
public void disabled_items_with_item_enabled_provider_should_stay_disabled_after_enabling_group() {
open();
WebElement group = findElement(By.id("button-group"));
// Click button to add items
findElement(By.id("add-button")).click();
for (WebElement radioButton : group.findElements(By.tagName("vaadin-radio-button"))) {
Assert.assertEquals("All buttons should be disabled", Boolean.TRUE.toString(), radioButton.getAttribute("disabled"));
}

// Click button to enable items
findElement(By.id("enable-button")).click();
List<WebElement> radioButtons = group.findElements(By.tagName("vaadin-radio-button"));
WebElement firstButton = radioButtons.get(0);
WebElement secondButton = radioButtons.get(1);
Assert.assertNull("First button should not be disabled", firstButton.getAttribute("disabled"));
Assert.assertEquals("Second button should be disabled", Boolean.TRUE.toString(), secondButton.getAttribute("disabled"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ protected boolean valueEquals(T value1, T value2) {
private void updateEnabled(RadioButton<T> button) {
boolean disabled = isDisabledBoolean()
|| !getItemEnabledProvider().test(button.getItem());
button.setEnabled(!disabled);
Serializable rawValue = button.getElement().getPropertyRaw("disabled");
if (rawValue instanceof Boolean) {
// convert the boolean value to a String to force update the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ public void selectDisabledItem_noRedundantEvent() {
Assert.assertEquals("enabled", event.getValue());
}

@Test
public void disabledItems_itemEnabledProvider_stayDisabled() {
RadioButtonGroup<String> group = new RadioButtonGroup<>();
group.setItems("enabled", "disabled");
group.setItemEnabledProvider("enabled"::equals);

List<RadioButton<String>> children = group.getChildren()
.map(child -> (RadioButton<String>) child)
.collect(Collectors.toList());

Assert.assertTrue(children.get(0).isEnabled());
Assert.assertFalse(children.get(1).isEnabled());

group.setEnabled(false);
Assert.assertFalse(children.get(0).isEnabled());
Assert.assertFalse(children.get(1).isEnabled());

group.setEnabled(true);
Assert.assertTrue(children.get(0).isEnabled());
Assert.assertFalse(children.get(1).isEnabled());

group.setEnabled(false);
Assert.assertFalse(children.get(0).isEnabled());
Assert.assertFalse(children.get(1).isEnabled());
}

@Test
public void changeItems_selectionIsReset() {
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
Expand Down

0 comments on commit b817779

Please sign in to comment.