Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add workaround for RouterLinks #8

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Enhanced Tabs Add-on
* %%
* Copyright (C) 2023 Flowing Code
* Copyright (C) 2023-2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,13 +41,18 @@
import com.vaadin.flow.component.HasEnabled;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.shared.Registration;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -196,6 +201,39 @@ public void add(Tab... tabs) {
}
}

private static final Method UI_navigate;
static {
try {
UI_navigate = UI.class.getMethod("navigate", Class.class);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError("UI.navigate(Class)");
}
}
paodb marked this conversation as resolved.
Show resolved Hide resolved

private static void navigate(UI ui, Class<? extends Component> target) {
try {
UI_navigate.invoke(ui, target);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new UndeclaredThrowableException(e);
}
}

public RouterLink addRouterLink(String text, Class<? extends Component> target) {
RouterLink routerLink = new RouterLink(text, target);
routerLink.getElement().executeJs(
"this.addEventListener('click', e => {\n"+
"e.preventDefault();\n"+
"this.dispatchEvent(new CustomEvent('client-side-click'));\n"+
"});\n");

routerLink.getElement().addEventListener("client-side-click", event -> {
navigate(UI.getCurrent(), target);
});

add(new Tab(routerLink));
return routerLink;
}
paodb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Removes the given child tabs from this component.
*
Expand Down Expand Up @@ -546,6 +584,10 @@ public Tab getTabAt(int index) {
+ index));
}

public int getTabCount() {
return (int) getTabs().count();
}

private Stream<Tab> getTabs() {
return getContent().getItems().stream().map(EnhancedTabs::getTab).filter(Objects::nonNull);
}
Expand Down