Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disabled items can now be part of a CheckboxGroup's valid value (#1903) (CP: 21.0) #1967

Merged
merged 2 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.vaadin.flow.component.checkbox.tests;

import com.vaadin.flow.component.checkbox.CheckboxGroup;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

Expand All @@ -18,6 +20,18 @@ public CheckboxGroupDisabledItemPage() {
group.select("bar");
group.setItemEnabledProvider(item -> !"bar".equals(item));
group.setId("checkbox-group-disabled-item");
add(group);

NativeButton toggleBarButton = new NativeButton("Toggle \"bar\"",
event -> {
boolean isBarSelected = group.isSelected("bar");
if (isBarSelected) {
group.deselect("bar");
} else {
group.select("bar");
}
});
toggleBarButton.setId("toggle-bar-button");

add(group, new Div(toggleBarButton));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,45 @@ public void disabledGroupItemChecked() {
Assert.assertEquals(Boolean.TRUE.toString(),
checkboxes.get(1).getAttribute("checked"));
}

@Test
public void disabledItemCanBeCheckedProgrammatically() {
open();
TestBenchElement group = $(TestBenchElement.class)
.id("checkbox-group-disabled-item");
List<TestBenchElement> checkboxes = group.$("vaadin-checkbox").all();
TestBenchElement secondCheckbox = checkboxes.get(1);
TestBenchElement toggleBarButton = $("button").id("toggle-bar-button");

// Deselect
toggleBarButton.click();
Assert.assertNull(secondCheckbox.getAttribute("checked"));

// Reselect
toggleBarButton.click();
Assert.assertEquals(Boolean.TRUE.toString(),
secondCheckbox.getAttribute("checked"));
}

/**
* Regression test for:
* https://github.com/vaadin/flow-components/issues/1185
*/
@Test
public void enabledItemCanBeCheckedManuallyWhenSettingItemEnabledProviderAfterSelectingValue() {
open();
TestBenchElement group = $(TestBenchElement.class)
.id("checkbox-group-disabled-item");
List<TestBenchElement> checkboxes = group.$("vaadin-checkbox").all();
TestBenchElement firstCheckbox = checkboxes.get(0);

// Select
firstCheckbox.click();
Assert.assertEquals(Boolean.TRUE.toString(),
firstCheckbox.getAttribute("checked"));

// Deselect
firstCheckbox.click();
Assert.assertNull(firstCheckbox.getAttribute("checked"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,16 @@ protected boolean valueEquals(Set<T> value1, Set<T> value2) {

@Override
protected boolean hasValidValue() {
Set<T> selectedItems = presentationToModel(this,
// we need to compare old value with new value to see if any disabled
// items changed their value
Set<T> value = presentationToModel(this,
(JsonArray) getElement().getPropertyRaw(VALUE));
if (selectedItems == null || selectedItems.isEmpty()) {
return true;
}
return selectedItems.stream().allMatch(itemEnabledProvider);
Set<T> oldValue = getValue();

// disabled items cannot change their value
return getCheckboxItems().filter(CheckBoxItem::isDisabledBoolean)
.noneMatch(item -> oldValue.contains(item.getItem()) != value
.contains(item.getItem()));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -540,7 +544,7 @@ private void validateSelectionEnabledState(PropertyChangeEvent event) {
} finally {
registerValidation();
}
// Now make sure that the button is still in the correct state
// Now make sure that the checkbox is still in the correct state
Set<T> value = presentationToModel(this,
(JsonArray) event.getValue());
getCheckboxItems()
Expand Down