Skip to content

Commit cabbd7c

Browse files
authoredDec 16, 2024
feat: add support for views with HasUrlParameter to SideNavItem (#6950)
1 parent b9c7180 commit cabbd7c

File tree

2 files changed

+79
-0
lines changed
  • vaadin-side-nav-flow-parent/vaadin-side-nav-flow/src

2 files changed

+79
-0
lines changed
 

‎vaadin-side-nav-flow-parent/vaadin-side-nav-flow/src/main/java/com/vaadin/flow/component/sidenav/SideNavItem.java

+49
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import com.vaadin.flow.dom.Element;
3737
import com.vaadin.flow.internal.JsonSerializer;
3838
import com.vaadin.flow.internal.UrlUtil;
39+
import com.vaadin.flow.router.HasUrlParameter;
3940
import com.vaadin.flow.router.QueryParameters;
4041
import com.vaadin.flow.router.RouteAlias;
4142
import com.vaadin.flow.router.RouteConfiguration;
4243
import com.vaadin.flow.router.RouteParameters;
4344
import com.vaadin.flow.router.internal.ConfigureRoutes;
45+
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
4446

4547
import elemental.json.JsonArray;
4648

@@ -103,6 +105,26 @@ public SideNavItem(String label, Class<? extends Component> view) {
103105
setLabel(label);
104106
}
105107

108+
/**
109+
* Creates a new menu item using the given label that links to the given
110+
* view, which must implement {@link HasUrlParameter}.
111+
*
112+
* @param label
113+
* the label for the item
114+
* @param view
115+
* the view to link to, must implement {@link HasUrlParameter}
116+
* @param parameter
117+
* the URL parameter for the view
118+
* @param <T>
119+
* the type of the URL parameter
120+
* @param <C>
121+
* the type of the view
122+
*/
123+
public <T, C extends Component & HasUrlParameter<T>> SideNavItem(
124+
String label, Class<? extends C> view, T parameter) {
125+
this(label, view, HasUrlParameterFormat.getParameters(parameter));
126+
}
127+
106128
/**
107129
* Creates a new menu item using the given label that links to the given
108130
* view.
@@ -262,6 +284,33 @@ public void setPath(Class<? extends Component> view) {
262284
setPath(view, RouteParameters.empty());
263285
}
264286

287+
/**
288+
* Retrieves {@link com.vaadin.flow.router.Route} and
289+
* {@link com.vaadin.flow.router.RouteAlias} annotations from the specified
290+
* view, and then sets the corresponding path and path aliases for this
291+
* item.
292+
* <p>
293+
* Note: Vaadin Router will be used to determine the URL path of the view
294+
* and this URL will be then set to this navigation item using
295+
* {@link SideNavItem#setPath(String)}.
296+
*
297+
* @param view
298+
* The view to link to. The view should be annotated with the
299+
* {@link com.vaadin.flow.router.Route} annotation and must
300+
* implement {@link HasUrlParameter}. Set to null to disable
301+
* navigation for this item.
302+
* @param parameter
303+
* the URL parameter for the view
304+
* @param <T>
305+
* the type of the URL parameter
306+
* @param <C>
307+
* the type of the view
308+
*/
309+
public <T, C extends Component & HasUrlParameter<T>> void setPath(
310+
Class<? extends C> view, T parameter) {
311+
setPath(view, HasUrlParameterFormat.getParameters(parameter));
312+
}
313+
265314
/**
266315
* Retrieves {@link com.vaadin.flow.router.Route} and
267316
* {@link com.vaadin.flow.router.RouteAlias} annotations from the specified

‎vaadin-side-nav-flow-parent/vaadin-side-nav-flow/src/test/java/com/vaadin/flow/component/sidenav/tests/SideNavItemTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.vaadin.flow.component.html.Div;
3434
import com.vaadin.flow.component.sidenav.SideNavItem;
3535
import com.vaadin.flow.dom.Element;
36+
import com.vaadin.flow.router.BeforeEvent;
37+
import com.vaadin.flow.router.HasUrlParameter;
3638
import com.vaadin.flow.router.NotFoundException;
3739
import com.vaadin.flow.router.QueryParameters;
3840
import com.vaadin.flow.router.Route;
@@ -605,6 +607,25 @@ public void setPathAsComponent_aliasWithMissingParameterNotAdded() {
605607
}, TestRouteWithAliases.class);
606608
}
607609

610+
@Test
611+
public void createFromComponentWithHasUrlParameter_pathContainsParameters() {
612+
runWithMockRouter(() -> {
613+
sideNavItem = new SideNavItem("test",
614+
TestRouteWithHasUrlParameter.class, "bar/baz");
615+
616+
assertPath("foo/bar/baz");
617+
}, TestRouteWithHasUrlParameter.class);
618+
}
619+
620+
@Test
621+
public void setPathAsComponentWithHasUrlParameter_pathContainsParameters() {
622+
runWithMockRouter(() -> {
623+
sideNavItem.setPath(TestRouteWithHasUrlParameter.class, "bar/baz");
624+
625+
assertPath("foo/bar/baz");
626+
}, TestRouteWithHasUrlParameter.class);
627+
}
628+
608629
@Test
609630
public void setTarget_hasTarget() {
610631
sideNavItem.setTarget("_blank");
@@ -779,6 +800,15 @@ private static class TestRouteWithRouteParams extends Component {
779800

780801
}
781802

803+
@Route("foo")
804+
private static class TestRouteWithHasUrlParameter extends Component
805+
implements HasUrlParameter<String> {
806+
@Override
807+
public void setParameter(BeforeEvent event, String parameter) {
808+
809+
}
810+
}
811+
782812
@Route("foo/bar")
783813
@RouteAlias("foo/baz")
784814
@RouteAlias("foo/qux")

0 commit comments

Comments
 (0)