Created
May 11, 2022 08:02
-
-
Save TatuLund/4c06cd4aa62388420ad1e3d5f764fc7f to your computer and use it in GitHub Desktop.
How to update "has-value" attribute in the custom field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.tatu.vaadin20.views; | |
| import com.vaadin.flow.component.button.Button; | |
| import com.vaadin.flow.component.customfield.CustomField; | |
| import com.vaadin.flow.component.html.Span; | |
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; | |
| import com.vaadin.flow.component.textfield.TextField; | |
| import com.vaadin.flow.data.binder.Binder; | |
| import com.vaadin.flow.router.Route; | |
| @Route(value = "binder") | |
| public class BinderDemoView extends VerticalLayout { | |
| private Binder<Pojo> binder = new Binder<>(); | |
| private Pojo pojo; | |
| public BinderDemoView() { | |
| pojo = new Pojo("Pojo example", "value1", "value2"); | |
| buildUI(); | |
| } | |
| private void buildUI() { | |
| add(new Span("Binder demo")); | |
| CustomTextField name = new CustomTextField(); | |
| name.setLabel("Name"); | |
| CustomTextField field1 = new CustomTextField(); | |
| field1.setLabel("Field 1"); | |
| TextField field2 = new TextField(); | |
| field2.setLabel("Field 2"); | |
| add(name, field1, field2); | |
| binder.forField(name).bind(Pojo::getName, Pojo::setName); | |
| binder.forField(field1).asRequired("is Required").bind(Pojo::getField1, | |
| Pojo::setField1); | |
| binder.forField(field2).asRequired("is Required").bind(Pojo::getField2, | |
| Pojo::setField2); | |
| Button setBean = new Button("Set Bean", event -> { | |
| setBean(); | |
| }); | |
| add(setBean); | |
| } | |
| public void setBean() { | |
| binder.setBean(pojo); | |
| } | |
| public static class CustomTextField extends CustomField<String> { | |
| private TextField mainComponent = new TextField(); | |
| public CustomTextField() { | |
| add(mainComponent); | |
| // Workaround | |
| this.addValueChangeListener(event -> { | |
| if (event.getValue() != null) { | |
| getElement().setAttribute("has-value", true); | |
| } else { | |
| getElement().removeAttribute("has-value"); | |
| } | |
| }); | |
| } | |
| @Override | |
| public void setInvalid(boolean invalid) { | |
| super.setInvalid(invalid); | |
| mainComponent.setInvalid(invalid); | |
| } | |
| @Override | |
| protected String generateModelValue() { | |
| if (mainComponent.getValue().isEmpty()) { | |
| return null; | |
| } | |
| return mainComponent.getValue(); | |
| } | |
| @Override | |
| protected void setPresentationValue(String newPresentationValue) { | |
| if (newPresentationValue == null) { | |
| newPresentationValue = ""; | |
| } | |
| mainComponent.setValue(newPresentationValue); | |
| } | |
| } | |
| public static class Pojo { | |
| private String Name; | |
| private String field1; | |
| private String field2; | |
| public Pojo(String name, String field1, String field2) { | |
| Name = name; | |
| this.field1 = field1; | |
| this.field2 = field2; | |
| } | |
| public String getName() { | |
| return Name; | |
| } | |
| public void setName(String name) { | |
| Name = name; | |
| } | |
| public String getField1() { | |
| return field1; | |
| } | |
| public void setField1(String field1) { | |
| this.field1 = field1; | |
| } | |
| public String getField2() { | |
| return field2; | |
| } | |
| public void setField2(String field2) { | |
| this.field2 = field2; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment