Prevent double click

Is there a way to prevent double click in router navigation?

Click on a router link starts navigation. While system is navigating users are able to click onece more.

Thank you very much, Thomas

There is no any standard/out of the box available way to do it in the framework.
May be you can create a ticket.

I think you should be able to achieve that via JS: the navigation link is just a link (a) element.
So you may attach a JS listener for it which e.g. disables the link after the first click.

On the other hand : the navigation is done inside the same page (via replacing the content).
So if your link stays on the page after the navigation then it will be disabled forever. I don’t think you want this
behavior.
But if link disappears then it should work.

If link stays then you are requesting a temporary “disabled” link. And as I mentioned this is not a part of framework for sure and I don’t see a simple way to make a workaround for this.

There are fkidding me … there is not listener to doubleClick, but getClinkCount in event?

So in each lamdba I have to check, if user didn’t by mistake didn’t click “Create” twice? WTF.

wHO did this?

it’s even worse than changing lightbulbs in modern cars.

agh, there is workaround

	/**
	 * Disables button after 1st click, then enables it after certain time => 2nd click is performed over disabled button
	 * => double-click is disabled
	 * @param btnSubmit
	 */
	private static void preventDoubleClick(Button btnSubmit) {
		UI ui = UI.getCurrent();
		btnSubmit.setDisableOnClick(true);
		btnSubmit.addClickListener(event -> setTimeoutAsync(() -> {
			ui.access(() -> btnSubmit.setEnabled(true));
		}, 300));
	}

	public static void setTimeoutAsync(Runnable runnable, int delay) {
		new Thread(() -> {
			try {
				Thread.sleep(delay);
				runnable.run();
			}
			catch (Exception e){
				log.error(e.getMessage(), e);
			}
		}).start();
	}

more correct fix (just instantly adds constructors ;))

public CustomButton extends Button {
	private ComponentEventListener<ClickEvent<Button>> doubleClickListener;
	private ComponentEventListener<ClickEvent<Button>> moreClickListener;

	@Override
	public Registration addClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		return super.addClickListener(event -> {
			if(event.getClickCount() == 1) {
				listener.onComponentEvent(event);
			} else if (event.getClickCount() == 2 && doubleClickListener != null) {
				doubleClickListener.onComponentEvent(event);
			} else if (event.getClickCount() > 2 && moreClickListener != null) {
				moreClickListener.onComponentEvent(event);
			}
		});
	}

	public GimsButton doubleClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		this.doubleClickListener = listener;
		return this;
	}

	public GimsButton moreClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		this.moreClickListener = listener;
		return this;
	}
}