Added search field

This commit is contained in:
Andrey Terentev 2023-11-16 21:03:11 +07:00
parent e55a16265a
commit b0eecb708a

View File

@ -2,7 +2,10 @@ package ru.dragonestia.loadbalancer.web.component;
import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import ru.dragonestia.loadbalancer.web.model.Node; import ru.dragonestia.loadbalancer.web.model.Node;
import java.util.List; import java.util.List;
@ -10,16 +13,38 @@ import java.util.List;
public class NodeList extends VerticalLayout { public class NodeList extends VerticalLayout {
private final Grid<Node> nodesGrid; private final Grid<Node> nodesGrid;
private final TextField searchField;
private List<Node> cachedNodes;
public NodeList(List<Node> nodes) { public NodeList(List<Node> nodes) {
super(); super();
cachedNodes = nodes;
add(new H2("Nodes")); add(new H2("Nodes"));
add(searchField = createSearchField());
add(nodesGrid = createGrid()); add(nodesGrid = createGrid());
update(nodes); update(nodes);
} }
private TextField createSearchField() {
var field = new TextField("Search node");
field.setPrefixComponent(new Icon(VaadinIcon.SEARCH));
field.setClearButtonVisible(true);
field.setHelperText("Press Enter to search");
field.addValueChangeListener(event -> applySearch(event.getValue()));
return field;
}
private void applySearch(String input) {
var temp = input.trim();
nodesGrid.setItems(cachedNodes.stream()
.filter(node -> node.identifier().startsWith(temp))
.toList());
}
private Grid<Node> createGrid() { private Grid<Node> createGrid() {
var grid = new Grid<>(Node.class, false); var grid = new Grid<>(Node.class, false);
grid.addColumn(Node::identifier).setHeader("Identifier"); grid.addColumn(Node::identifier).setHeader("Identifier");
@ -28,7 +53,8 @@ public class NodeList extends VerticalLayout {
} }
public void update(List<Node> nodes) { public void update(List<Node> nodes) {
nodesGrid.setItems(nodes); cachedNodes = nodes;
applySearch(searchField.getValue());
} }
private void registerNode(Node node) { private void registerNode(Node node) {
@ -36,9 +62,4 @@ public class NodeList extends VerticalLayout {
update(List.of(node)); update(List.of(node));
} }
public interface OnRegister {
List<Node> register(Node node);
}
} }