Skip to content

Commit

Permalink
fix: add workaround for RouterLinks in EnhancedRouteTabs
Browse files Browse the repository at this point in the history
Close #89
  • Loading branch information
javier-godoy committed Jun 12, 2024
1 parent 2b3fe14 commit 342ae1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ public class EnhancedRouteTabs extends EnhancedTabs implements BeforeEnterObserv

private final Map<RouterLink, Tab> routerLinkTabMap = new LinkedHashMap<>();

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

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

routerLink.setHighlightCondition(HighlightConditions.sameLocation());
routerLink.setHighlightAction(
(link, shouldHighlight) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -152,8 +151,7 @@ public void addDemo(Class<? extends Component> clazz, String label) {
if (!clazz.isAnnotationPresent(Route.class)) {
throw new IllegalArgumentException(clazz + " must be annotated as Route");
}
RouterLink tab = new RouterLink(label, clazz);
tabs.add(tab);
tabs.addRouterLink(label, clazz);
}

/**
Expand Down

0 comments on commit 342ae1c

Please sign in to comment.