Skip to content

Commit 8ff2874

Browse files
committed
fix(demo): add examples using setValue
1 parent decfa5f commit 8ff2874

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/test/java/com/flowingcode/vaadin/addons/chipfield/BinderDemo.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.flowingcode.vaadin.addons.chipfield;
2121

2222
import java.util.Arrays;
23+
import java.util.List;
24+
import java.util.stream.Collectors;
2325

2426
import com.vaadin.flow.component.button.Button;
2527
import com.vaadin.flow.component.notification.Notification;
@@ -36,7 +38,10 @@ public BinderDemo() {
3638

3739
ChipField<String> chf = new ChipField<>("Choose planet features (try with: 'Rings', 'Moons', 'Water', etc.)");
3840
chf.setWidthFull();
39-
chf.setItems(Arrays.asList("Rings", "Moons", "Water", "Rocks", "Lava", "Ice", "Cold", "Heat", "Atmosphere"));
41+
42+
List<String> items = Arrays.asList("Rings", "Moons", "Water", "Rocks", "Lava", "Ice", "Cold", "Heat", "Atmosphere");
43+
chf.setItems(items);
44+
4045
Binder<Planet> binder = new Binder<>();
4146
binder.bind(chf, Planet::getFeatures, Planet::setFeatures);
4247
binder.setBean(p);
@@ -48,6 +53,11 @@ public BinderDemo() {
4853
add(new Button("Show planet features",
4954
e -> Notification.show("Features: " + p.getFeatures(), 5000, Position.BOTTOM_START)));
5055

56+
add(new Button("Random features", ev -> {
57+
p.setFeatures(items.stream().filter(x -> Math.random() > 0.7).collect(Collectors.toList()));
58+
binder.setBean(p);
59+
}));
60+
5161
}
5262

5363
}

src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ public DataProviderDemo() {
6161
chf.addChipClickedListener(
6262
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Clicked!", 5000, Position.BOTTOM_END));
6363

64+
buttons.add(new Button("All planets", ev -> {
65+
chf.setValue(Planet.all());
66+
}));
67+
68+
buttons.add(new Button("Remove Inner planets", ev -> {
69+
chf.removeSelectedItem(new Planet("Mercury"));
70+
chf.removeSelectedItem(new Planet("Venus"));
71+
chf.removeSelectedItem(new Planet("Earth"));
72+
chf.removeSelectedItem(new Planet("Mars"));
73+
}));
74+
6475
add(new VerticalLayout(chf, buttons));
6576
}
6677

src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ public List<String> getFeatures() {
4646
public void setFeatures(List<String> features) {
4747
this.features = features;
4848
}
49+
50+
@Override
51+
public int hashCode() {
52+
final int prime = 31;
53+
int result = 1;
54+
result = prime * result + ((name == null) ? 0 : name.hashCode());
55+
return result;
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj) {
61+
return true;
62+
}
63+
if (obj == null) {
64+
return false;
65+
}
66+
if (getClass() != obj.getClass()) {
67+
return false;
68+
}
69+
Planet other = (Planet) obj;
70+
if (name == null) {
71+
if (other.name != null) {
72+
return false;
73+
}
74+
} else if (!name.equals(other.name)) {
75+
return false;
76+
}
77+
return true;
4978
}
5079

5180
public static List<Planet> all() {

0 commit comments

Comments
 (0)