Skip to content

Commit 5183d75

Browse files
vaadin-botvursenclaude
authored
fix: preserve slotted children on setItems in radio/checkbox group (#9863) (#9864)
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent ccaff75 commit 5183d75

8 files changed

Lines changed: 66 additions & 135 deletions

File tree

  • vaadin-checkbox-flow-parent
  • vaadin-radio-button-flow-parent

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,17 @@
1515
*/
1616
package com.vaadin.flow.component.checkbox.tests;
1717

18-
import java.util.Set;
19-
2018
import com.vaadin.flow.component.checkbox.Checkbox;
2119
import com.vaadin.flow.component.checkbox.CheckboxGroup;
2220
import com.vaadin.flow.component.html.Div;
2321
import com.vaadin.flow.component.html.NativeButton;
2422
import com.vaadin.flow.component.html.Span;
25-
import com.vaadin.flow.data.binder.Binder;
2623
import com.vaadin.flow.router.Route;
2724

2825
@Route("vaadin-checkbox/helper")
2926
public class HelperPage extends Div {
3027

3128
public HelperPage() {
32-
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
33-
Span span = new Span("Helper text");
34-
checkboxGroup.setHelperComponent(span);
35-
36-
checkboxGroup.setItems("foo", "bar", "baz");
37-
Binder<Bean> binder = new Binder<>();
38-
binder.bind(checkboxGroup, bean -> bean.choices,
39-
(bean, value) -> bean.choices = value);
40-
binder.setBean(new Bean());
41-
4229
CheckboxGroup<String> groupWithHelperGenerator = new CheckboxGroup<>();
4330
groupWithHelperGenerator.setItems("A", "B", "C");
4431
groupWithHelperGenerator
@@ -71,12 +58,8 @@ public HelperPage() {
7158
e -> helperComponentCheckbox.setHelperComponent(null));
7259
emptyHelperComponent.setId("empty-helper-component");
7360

74-
add(checkboxGroup, groupWithHelperGenerator, clearItemHelperGenerator,
61+
add(groupWithHelperGenerator, clearItemHelperGenerator,
7562
helperTextCheckbox, helperComponentCheckbox, emptyHelperText,
7663
emptyHelperComponent);
7764
}
78-
79-
public static class Bean {
80-
private Set<String> choices;
81-
}
8265
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ public void init() {
3535
open();
3636
}
3737

38-
/**
39-
* Assert that helper component exists after setItems.
40-
* https://github.com/vaadin/vaadin-checkbox/issues/191
41-
*/
42-
@Test
43-
public void checkboxGroupHelperComponent() {
44-
TestBenchElement checkboxGroup = $("vaadin-checkbox-group").first();
45-
46-
TestBenchElement helperComponent = checkboxGroup.$("span")
47-
.withAttribute("slot", "helper").first();
48-
Assert.assertEquals("Helper text", helperComponent.getText());
49-
50-
}
51-
5238
@Test
5339
public void checkboxGroupHelperGenerator() {
5440
CheckboxGroupElement group = $(CheckboxGroupElement.class)

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,10 @@ public SelectionPreservationMode getSelectionPreservationMode() {
831831
@SuppressWarnings("unchecked")
832832
private void rebuild() {
833833
synchronized (dataProvider) {
834-
// Cache helper component before removal
835-
Component helperComponent = getHelperComponent();
836-
837-
// Remove all known children (doesn't remove client-side-only
838-
// children such as the label)
839-
getChildren().forEach(this::remove);
840-
841-
// reinsert helper component
842-
// see https://github.com/vaadin/vaadin-checkbox/issues/191
843-
setHelperComponent(helperComponent);
834+
// Remove children in the default slot
835+
getChildren()
836+
.filter(child -> !child.getElement().hasAttribute("slot"))
837+
.forEach(this::remove);
844838

845839
final AtomicInteger itemCounter = new AtomicInteger(0);
846840

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,35 @@ void changeItems_selectionIsReset() {
186186
"Captured value should be empty");
187187
}
188188

189+
@Test
190+
void customComponentInLabelSlot_setItems_componentIsPreserved() {
191+
CheckboxGroup<String> group = new CheckboxGroup<>();
192+
Div customLabel = new Div();
193+
customLabel.getElement().setAttribute("slot", "label");
194+
group.getElement().appendChild(customLabel.getElement());
195+
196+
group.setItems("foo", "bar");
197+
group.setItems("foo", "baz");
198+
199+
Assertions.assertEquals(group.getElement(),
200+
customLabel.getElement().getParent());
201+
Assertions.assertEquals(2,
202+
group.getChildren().filter(Checkbox.class::isInstance).count());
203+
}
204+
205+
@Test
206+
void helperComponent_setItems_helperComponentIsPreserved() {
207+
CheckboxGroup<String> group = new CheckboxGroup<>();
208+
Div helper = new Div();
209+
group.setHelperComponent(helper);
210+
211+
group.setItems("foo", "bar");
212+
213+
Assertions.assertEquals(helper, group.getHelperComponent());
214+
Assertions.assertEquals(group.getElement(),
215+
helper.getElement().getParent());
216+
}
217+
189218
@Test
190219
void deselectAll_selectionIsReset() {
191220
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();

‎vaadin-radio-button-flow-parent/vaadin-radio-button-flow-integration-tests/src/main/java/com/vaadin/flow/component/radiobutton/tests/HelperPage.java‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎vaadin-radio-button-flow-parent/vaadin-radio-button-flow-integration-tests/src/test/java/com/vaadin/flow/component/radiobutton/tests/HelperIT.java‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎vaadin-radio-button-flow-parent/vaadin-radio-button-flow/src/main/java/com/vaadin/flow/component/radiobutton/RadioButtonGroup.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -679,17 +679,11 @@ public Optional<String> getAriaLabelledBy() {
679679
@SuppressWarnings("unchecked")
680680
private void rebuild() {
681681
synchronized (dataProvider) {
682-
// Cache helper component before removal
683-
Component helperComponent = getHelperComponent();
684-
685-
// Remove all known children (doesn't remove client-side-only
686-
// children such as the label)
682+
// Remove children in the default slot
687683
getChildren()
684+
.filter(child -> !child.getElement().hasAttribute("slot"))
688685
.forEach(child -> child.getElement().removeFromParent());
689686

690-
// reinsert helper component
691-
setHelperComponent(helperComponent);
692-
693687
final AtomicInteger itemCounter = new AtomicInteger(0);
694688
getDataProvider().fetch(DataViewUtils.getQuery(this))
695689
.map(item -> createRadioButton((T) item))

‎vaadin-radio-button-flow-parent/vaadin-radio-button-flow/src/test/java/com/vaadin/flow/component/radiobutton/RadioButtonGroupTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.vaadin.flow.component.HasAriaLabel;
3535
import com.vaadin.flow.component.HasValue;
3636
import com.vaadin.flow.component.HasValue.ValueChangeEvent;
37+
import com.vaadin.flow.component.html.Div;
3738
import com.vaadin.flow.component.radiobutton.dataview.RadioButtonGroupListDataView;
3839
import com.vaadin.flow.component.shared.HasThemeVariant;
3940
import com.vaadin.flow.component.shared.HasTooltip;
@@ -229,6 +230,35 @@ void changeItems_selectionIsReset() {
229230
Assertions.assertEquals(null, capture.get());
230231
}
231232

233+
@Test
234+
void customComponentInLabelSlot_setItems_componentIsPreserved() {
235+
RadioButtonGroup<String> group = new RadioButtonGroup<>();
236+
Div customLabel = new Div();
237+
customLabel.getElement().setAttribute("slot", "label");
238+
group.getElement().appendChild(customLabel.getElement());
239+
240+
group.setItems("foo", "bar");
241+
group.setItems("foo", "baz");
242+
243+
Assertions.assertEquals(group.getElement(),
244+
customLabel.getElement().getParent());
245+
Assertions.assertEquals(2, group.getChildren()
246+
.filter(RadioButton.class::isInstance).count());
247+
}
248+
249+
@Test
250+
void helperComponent_setItems_helperComponentIsPreserved() {
251+
RadioButtonGroup<String> group = new RadioButtonGroup<>();
252+
Div helper = new Div();
253+
group.setHelperComponent(helper);
254+
255+
group.setItems("foo", "bar");
256+
257+
Assertions.assertEquals(helper, group.getHelperComponent());
258+
Assertions.assertEquals(group.getElement(),
259+
helper.getElement().getParent());
260+
}
261+
232262
@Test
233263
@SuppressWarnings("unchecked")
234264
void testResetAllItems() {

0 commit comments

Comments
 (0)