Skip to content

Commit 7f68b55

Browse files
authored
fix: enforce checkbox web component disabled state (#2578)
1 parent 18a0fa5 commit 7f68b55

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow-integration-tests/src/main/java/com/vaadin/flow/component/checkbox/tests/CheckboxGroupDisabledItemPage.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public CheckboxGroupDisabledItemPage() {
3232
});
3333
toggleBarButton.setId("toggle-bar-button");
3434

35-
add(group, new Div(toggleBarButton));
35+
NativeButton toggleEnabledButton = new NativeButton("Toggle enabled",
36+
event -> group.setEnabled(!group.isEnabled()));
37+
toggleEnabledButton.setId("toggle-enabled-button");
38+
39+
add(group, new Div(toggleBarButton, toggleEnabledButton));
3640
}
3741
}

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow-integration-tests/src/test/java/com/vaadin/flow/component/checkbox/tests/CheckboxGroupDisabledItemIT.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,25 @@ public void enabledItemCanBeCheckedManuallyWhenSettingItemEnabledProviderAfterSe
7878
firstCheckbox.click();
7979
Assert.assertNull(firstCheckbox.getAttribute("checked"));
8080
}
81+
82+
@Test
83+
public void enablingTheGroupDoesnNotEnableItemDisabledWithItemEnabledProvider() {
84+
open();
85+
TestBenchElement group = $(TestBenchElement.class)
86+
.id("checkbox-group-disabled-item");
87+
List<TestBenchElement> checkboxes = group.$("vaadin-checkbox").all();
88+
TestBenchElement toggleEnabledButton = $("button")
89+
.id("toggle-enabled-button");
90+
91+
// Disable group
92+
toggleEnabledButton.click();
93+
94+
// Re-enable group
95+
toggleEnabledButton.click();
96+
97+
Assert.assertEquals("Second checkbox should be disabled",
98+
Boolean.TRUE.toString(),
99+
checkboxes.get(1).getAttribute("disabled"));
100+
}
101+
81102
}

‎vaadin-checkbox-flow-parent/vaadin-checkbox-flow/src/main/java/com/vaadin/flow/component/checkbox/CheckboxGroup.java‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,12 @@ private void updateCheckbox(CheckBoxItem<T> checkbox) {
527527
private void updateEnabled(CheckBoxItem<T> checkbox) {
528528
boolean disabled = isDisabledBoolean()
529529
|| !getItemEnabledProvider().test(checkbox.getItem());
530-
Serializable rawValue = checkbox.getElement()
531-
.getPropertyRaw("disabled");
532-
if (rawValue instanceof Boolean) {
533-
// convert the boolean value to a String to force update the
534-
// property value. Otherwise since the provided value is the same as
535-
// the current one the update don't do anything.
536-
checkbox.getElement().setProperty("disabled",
537-
disabled ? Boolean.TRUE.toString() : null);
538-
} else {
539-
checkbox.setDisabled(disabled);
540-
}
530+
checkbox.setDisabled(disabled);
531+
// When enabling a disabled checkbox group, individual checkbox Web
532+
// Components that should remain disabled (due to itemEnabledProvider),
533+
// may end up rendering as enabled.
534+
// Enforce the Web Component state using JS.
535+
checkbox.getElement().executeJs("this.disabled = $0", disabled);
541536
}
542537

543538
private void validateSelectionEnabledState(PropertyChangeEvent event) {

0 commit comments

Comments
 (0)