Skip to content

Commit

Permalink
fix(demo): add examples using setValue
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy committed Jan 26, 2021
1 parent decfa5f commit 8ff2874
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.flowingcode.vaadin.addons.chipfield;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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

ChipField<String> chf = new ChipField<>("Choose planet features (try with: 'Rings', 'Moons', 'Water', etc.)");
chf.setWidthFull();
chf.setItems(Arrays.asList("Rings", "Moons", "Water", "Rocks", "Lava", "Ice", "Cold", "Heat", "Atmosphere"));

List<String> items = Arrays.asList("Rings", "Moons", "Water", "Rocks", "Lava", "Ice", "Cold", "Heat", "Atmosphere");
chf.setItems(items);

Binder<Planet> binder = new Binder<>();
binder.bind(chf, Planet::getFeatures, Planet::setFeatures);
binder.setBean(p);
Expand All @@ -48,6 +53,11 @@ public BinderDemo() {
add(new Button("Show planet features",
e -> Notification.show("Features: " + p.getFeatures(), 5000, Position.BOTTOM_START)));

add(new Button("Random features", ev -> {
p.setFeatures(items.stream().filter(x -> Math.random() > 0.7).collect(Collectors.toList()));
binder.setBean(p);
}));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public DataProviderDemo() {
chf.addChipClickedListener(
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Clicked!", 5000, Position.BOTTOM_END));

buttons.add(new Button("All planets", ev -> {
chf.setValue(Planet.all());
}));

buttons.add(new Button("Remove Inner planets", ev -> {
chf.removeSelectedItem(new Planet("Mercury"));
chf.removeSelectedItem(new Planet("Venus"));
chf.removeSelectedItem(new Planet("Earth"));
chf.removeSelectedItem(new Planet("Mars"));
}));

add(new VerticalLayout(chf, buttons));
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ public List<String> getFeatures() {
public void setFeatures(List<String> features) {
this.features = features;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Planet other = (Planet) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}

public static List<Planet> all() {
Expand Down

0 comments on commit 8ff2874

Please sign in to comment.