vaadinvaadin10

Vaadin 10 - Upload Component - remove file event


When file is uploaded, a Button is enabled:

// myUploadComponent extends Upload
myUploadComponent.addSucceededListener(event -> enabledMyButtonMEthod ()); // working well

I don't find out how to disable that Button when I remove the file (click on the cross next to it).

There should be something like 'addRemoveListener' ... ? How can I detect this event ?


Solution

  • You can listen to the "file-remove" event in the upload component. Here is an example.

    @Route("")
    public class MainView extends VerticalLayout {
    
        public MainView() { 
            MyUpload upload = new MyUpload();
            upload.addFileRemoveListener(e -> Notification.show("Button disabled"));
            add(upload);
        }
    
        class MyUpload extends Upload {
            Registration addFileRemoveListener(ComponentEventListener<FileRemoveEvent> listener) {
                return super.addListener(FileRemoveEvent.class, listener);
            }
        }
    
        @DomEvent("file-remove")
        public static class FileRemoveEvent extends ComponentEvent<Upload> {
            public FileRemoveEvent(Upload source, boolean fromClient) {
                super(source, fromClient);
            }
        }
    }