gidravlic
gidravlic

Reputation: 114

Prevent tab switching

I use Vaadin23.

Is there a legal way to prevent tab switching? As i understand documentation method

public Registration addSelectedChangeListener(
            ComponentEventListener<SelectedChangeEvent> listener) {
        return addListener(SelectedChangeEvent.class, listener);
    }

register listener which is triggered after tab change event.

What are the options?

Thanks!

Upvotes: 0

Views: 131

Answers (1)

ollitietavainen
ollitietavainen

Reputation: 4290

If your tab contents are navigation targets (Routes), you can use the BeforeLeaveObserver interface in your form view:

public class SignupForm extends Div
        implements BeforeLeaveObserver {
    @Override
    public void beforeLeave(BeforeLeaveEvent event) {
        if (hasChanges()) {
            ContinueNavigationAction action =
                    event.postpone();
            ConfirmDialog confirmDialog = new ConfirmDialog();
                 confirmDialog.setText("Your form has changes! Are you sure you want to leave?");
                 confirmDialog.setCancelable(true);
                 confirmDialog.addConfirmListener(__ -> action.proceed());
                 confirmDialog.open();
        }
    }

    private boolean hasChanges() {
        // TODO: implement your logic to check if there are unsaved changes
    }
}

Upvotes: 1

Related Questions