File tree Expand file tree Collapse file tree
main/java/com/vaadin/flow/router/internal
test/java/com/vaadin/flow/router Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2323import java .util .HashMap ;
2424import java .util .HashSet ;
2525import java .util .List ;
26+ import java .util .Objects ;
2627import java .util .Optional ;
2728import java .util .Set ;
29+ import java .util .function .Supplier ;
2830
2931import com .vaadin .flow .component .Component ;
3032import 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
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments