Vaadin Flow: Best practise for hiding fields / change labels in FormLayout

312 views Asked by At

What is the best practise for following code snippet...

public class Panel {

    private final TextField textField1 = new TextField();
    private final TextField textField2 = new TextField();
    private final TextArea textArea1 = new TextArea();
    private final TextArea textArea2 = new TextArea();

... many other fields

    private final Checkbox cb1 = new Checkbox();
    private final Checkbox cb2 = new Checkbox();


    public Component getContent() {
        FormLayout layout = new FormLayout();
        layout.addFormItem(textField1, "label for textfield 1");
        layout.addFormItem(textField2, "label for textfield 2");

...        

        layout.addFormItem(cb1, "label for checkbox 1");
        layout.addFormItem(cb2, "label for checkbox 2");
        return layout;
    }
    
    public void updateFields(boolean predicate) {
        textField1.setVisible(predicate);
        textField2.setVisible(!predicate);

        textArea1.setLabel(predicate ? "label A" : "label B");
...

        cb1.setVisible(predicate);
        cb2.setVisible(!predicate);
    }
}

It looks not very practically to save all form-item-references from "FormLayout#addFormItem" to update the visibility state of the fields... It is also not very practical to separate the labels from the fields.. In Vaadin8 it was simply possible to save the label at the field and it was used for the FormLayout...

Did I miss a point?!? Is there a better Opportunity to get the advantages of a FormLayout but with the "original" Labels of the fields?!

edit: https://github.com/vaadin/flow-components/issues/3352

0

There are 0 answers