javavaadinvaadin-flowvaadin14vaadin22

Vaadin22 get notified when a user clears a textfield


In Vaadin 22 it's possible to add a clear button to a textfield by calling the following method:

TextField textField = new TextField();
textField.setClearButtonVisible(true);

I wonder if it's possible to get notified when someone clicks the clear button.

I tried to attach a click listener to it by plain javascript. But that is not that simple because the clear button is in the shadow dom.


Solution

  • You don't have this information on the Java side. As a workaround you can add your own clear button as a suffix component:

    textfield.setSuffixComponent(myClearButton);
    

    And use the clickListener on the button. You will also need to manually display/hide it based on the state of the textfield.