Skip to content

Commit 2d07564

Browse files
fix: added HasDynamicTitle checking in parent layouts (#15355) (#15423)
Checks the navigation targets including parent layouts and takes the page title from the one implementing HasDynamicTitle. Fixes: #13871 Co-authored-by: marcin <marcin@vaadin.com>
1 parent 8324936 commit 2d07564

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractNavigationStateRenderer.java‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.util.HashMap;
2424
import java.util.HashSet;
2525
import java.util.List;
26+
import java.util.Objects;
2627
import java.util.Optional;
2728
import java.util.Set;
29+
import java.util.function.Supplier;
2830

2931
import com.vaadin.flow.component.Component;
3032
import com.vaadin.flow.component.HasElement;
@@ -888,14 +890,18 @@ private static void checkForDuplicates(
888890

889891
private static void updatePageTitle(NavigationEvent navigationEvent,
890892
Component routeTarget) {
891-
String title;
892893

893-
if (routeTarget instanceof HasDynamicTitle) {
894-
title = ((HasDynamicTitle) routeTarget).getPageTitle();
895-
} else {
896-
title = lookForTitleInTarget(routeTarget).map(PageTitle::value)
897-
.orElse("");
898-
}
894+
Supplier<String> lookForTitleInTarget = () -> lookForTitleInTarget(
895+
routeTarget).map(PageTitle::value).orElse("");
896+
897+
// check for HasDynamicTitle in current router targets chain
898+
String title = navigationEvent.getUI().getInternals()
899+
.getActiveRouterTargetsChain().stream()
900+
.filter(HasDynamicTitle.class::isInstance)
901+
.map(tc -> ((HasDynamicTitle) tc).getPageTitle())
902+
.filter(Objects::nonNull).findFirst()
903+
.orElseGet(lookForTitleInTarget);
904+
899905
navigationEvent.getUI().getPage().setTitle(title);
900906
}
901907

‎flow-server/src/test/java/com/vaadin/flow/router/RouterTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,21 @@ public static class ParentWithTitle extends Component
519519
public static class ChildWithoutTitle extends Component {
520520
}
521521

522+
@RoutePrefix("parent-with-dynamic-title")
523+
@Tag(Tag.DIV)
524+
public static class ParentWithDynamicTitle extends Component
525+
implements RouterLayout, HasDynamicTitle {
526+
@Override
527+
public String getPageTitle() {
528+
return DYNAMIC_TITLE;
529+
}
530+
}
531+
532+
@Route(value = "child2", layout = ParentWithDynamicTitle.class)
533+
@Tag(Tag.DIV)
534+
public static class ChildWithoutTitle2 extends Component {
535+
}
536+
522537
@Route("navigation-target-with-dynamic-title")
523538
@Tag(Tag.DIV)
524539
public static class NavigationTargetWithDynamicTitle extends Component
@@ -1704,6 +1719,17 @@ public void page_title_not_set_from_annotation_in_parent()
17041719
Assert.assertEquals("", ui.getInternals().getTitle());
17051720
}
17061721

1722+
@Test
1723+
public void page_title_set_from_dynamic_title_in_parent()
1724+
throws InvalidRouteConfigurationException {
1725+
setNavigationTargets(ChildWithoutTitle2.class);
1726+
1727+
router.navigate(ui, new Location("parent-with-dynamic-title/child2"),
1728+
NavigationTrigger.PROGRAMMATIC);
1729+
1730+
Assert.assertEquals(DYNAMIC_TITLE, ui.getInternals().getTitle());
1731+
}
1732+
17071733
@Test
17081734
public void page_title_set_dynamically()
17091735
throws InvalidRouteConfigurationException {

0 commit comments

Comments
 (0)