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 #92

Merged
merged 3 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 @@ -93,7 +93,7 @@
<dependency>
<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>enhanced-tabs-addon</artifactId>
<version>1.0.0</version>
<version>1.0.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.flowingcode.vaadin.addons.enhancedtabs.EnhancedTabs;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.router.BeforeEnterEvent;
Expand All @@ -38,20 +39,26 @@
*
* @see https://cookbook.vaadin.com/tabs-with-routes/a
*/
public class EnhancedRouteTabs extends EnhancedTabs implements BeforeEnterObserver {
public class EnhancedRouteTabs extends Composite<EnhancedTabs> implements BeforeEnterObserver {

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

public void add(RouterLink routerLink) {
public void add(String text, Class<? extends Component> target) {
RouterLink routerLink = getContent().addRouterLink(text, target);
routerLink.setHighlightCondition(HighlightConditions.sameLocation());
routerLink.setHighlightAction(
(link, shouldHighlight) -> {
if (shouldHighlight) {
setSelectedTab(routerLinkTabMap.get(routerLink));
}
});
routerLinkTabMap.put(routerLink, new Tab(routerLink));
add(routerLinkTabMap.get(routerLink));

Tab tab = getContent().getTabAt(getContent().getTabCount() - 1);
routerLinkTabMap.put(routerLink, tab);
}

private void setSelectedTab(Tab tab) {
getContent().setSelectedTab(tab);
}

@Override
Expand All @@ -67,11 +74,7 @@ public void beforeEnter(BeforeEnterEvent event) {
}
}

public Map<RouterLink, Tab> getRouterLinkTabMap() {
return routerLinkTabMap;
}

public RouterLink getFirstRoute() {
private RouterLink getFirstRoute() {
Optional<RouterLink> first =
routerLinkTabMap.entrySet().stream().map(Map.Entry::getKey).findFirst();
return first.isPresent() ? first.get() : null;
Expand All @@ -80,8 +83,8 @@ public RouterLink getFirstRoute() {
@Deprecated
public void addLegacyTab(String label, Component content) {
Tab tab = new Tab(label);
add(tab);
addSelectedChangeListener(
getContent().add(tab);
getContent().addSelectedChangeListener(
ev -> {
if (ev.getSelectedTab() == tab) {
TabbedDemo tabbedDemo = (TabbedDemo) getParent().get();
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 @@ -69,7 +68,6 @@ public TabbedDemo() {
demoHelperViewer = new DialogDemoHelperViewer();

tabs = new EnhancedRouteTabs();
tabs.setWidthFull();

// Footer
orientationCB = new Checkbox("Toggle Orientation");
Expand Down Expand Up @@ -152,8 +150,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.add(label, clazz);
}

/**
Expand Down
Loading