Skip to content

Commit ac03f19

Browse files
javier-godoypaodb
authored andcommitted
fix: add workaround for RouterLinks
Close #7
1 parent 1022a3d commit ac03f19

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main/java/com/flowingcode/vaadin/addons/enhancedtabs/EnhancedTabs.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Enhanced Tabs Add-on
44
* %%
5-
* Copyright (C) 2023 Flowing Code
5+
* Copyright (C) 2023-2024 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -41,11 +41,13 @@
4141
import com.vaadin.flow.component.HasEnabled;
4242
import com.vaadin.flow.component.HasSize;
4343
import com.vaadin.flow.component.HasStyle;
44+
import com.vaadin.flow.component.UI;
4445
import com.vaadin.flow.component.contextmenu.MenuItem;
4546
import com.vaadin.flow.component.dependency.CssImport;
4647
import com.vaadin.flow.component.dependency.JsModule;
4748
import com.vaadin.flow.component.menubar.MenuBar;
4849
import com.vaadin.flow.component.tabs.Tab;
50+
import com.vaadin.flow.router.RouterLink;
4951
import com.vaadin.flow.shared.Registration;
5052
import java.io.Serializable;
5153
import java.util.ArrayList;
@@ -196,6 +198,22 @@ public void add(Tab... tabs) {
196198
}
197199
}
198200

201+
public RouterLink addRouterLink(String text, Class<? extends Component> target) {
202+
RouterLink routerLink = new RouterLink(text, target);
203+
routerLink.getElement().executeJs(
204+
"this.addEventListener('click', e => {\n"+
205+
"e.preventDefault();\n"+
206+
"this.dispatchEvent(new CustomEvent('client-side-click'));\n"+
207+
"});\n");
208+
209+
routerLink.getElement().addEventListener("client-side-click", event -> {
210+
UI.getCurrent().navigate(target);
211+
});
212+
213+
add(new Tab(routerLink));
214+
return routerLink;
215+
}
216+
199217
/**
200218
* Removes the given child tabs from this component.
201219
*

0 commit comments

Comments
 (0)