Skip to content

Commit 611666f

Browse files
committed
feat: add support for RouterLink
Closes #49
1 parent 53abf1d commit 611666f

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

src/main/java/com/flowingcode/addons/applayout/MenuItem.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.vaadin.flow.component.dependency.JsModule;
3030
import com.vaadin.flow.component.dependency.NpmPackage;
3131
import com.vaadin.flow.component.icon.IconFactory;
32+
import com.vaadin.flow.router.RouterLink;
3233
import com.vaadin.flow.server.Command;
3334

3435
/**
@@ -58,7 +59,12 @@ public MenuItem(String label, Command command) {
5859
this.setLabel(label);
5960
this.setCommand(command);
6061
}
61-
62+
63+
/** Create a new instance of {@code MenuItem} with a label for the given navigation target. */
64+
public MenuItem(String label, Class<? extends Component> navigationTarget) {
65+
setLink(new RouterLink(label, navigationTarget));
66+
}
67+
6268
/** Create a new instance of {@code MenuItem} with a label and an icon. */
6369
public MenuItem(String label, String icon) {
6470
this(label);
@@ -112,6 +118,17 @@ public MenuItem setLabel(String label) {
112118
return this;
113119
}
114120

121+
public MenuItem setLink(RouterLink link) {
122+
setLabel(link.getText());
123+
setHref(link.getHref());
124+
return this;
125+
}
126+
127+
public MenuItem setHref(String href) {
128+
getElement().setProperty("href", href);
129+
return this;
130+
}
131+
115132
public MenuItem configure(Consumer<MenuItem> consumer) {
116133
consumer.accept(this);
117134
return this;

src/main/resources/META-INF/frontend/fc-applayout/fc-menuitem.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
23+
import {} from '@polymer/polymer/lib/elements/dom-if.js';
2324

2425
import {ThemableMixin} from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
2526
import "@polymer/paper-item/paper-icon-item.js";
@@ -49,6 +50,10 @@ class MenuItem extends ThemableMixin(PolymerElement) {
4950
:host #label {
5051
flex-grow: 1
5152
}
53+
:host a#label {
54+
color: inherit;
55+
text-decoration: none
56+
}
5257
</style>
5358
5459
<iron-iconset-svg name="fc-menuitem-icons" size="24">
@@ -60,7 +65,16 @@ class MenuItem extends ThemableMixin(PolymerElement) {
6065
<iron-collapse-button no-icons="true">
6166
<paper-icon-item id="item" slot="collapse-trigger" role="option" disabled="[[disabled]]">
6267
<iron-icon src="[[src]]" icon="[[icon]]" slot="item-icon"></iron-icon>
63-
<span id="label">[[label]]</span>
68+
<dom-if if="[[href]]" restamp>
69+
<template>
70+
<a router-link href="{{href}}" id="label" onclick="getRootNode().host.__closeDrawer()">[[label]]</a>
71+
</template>
72+
</dom-if>
73+
<dom-if if="{{!href}}" restamp>
74+
<template>
75+
<span id="label">[[label]]</span>
76+
</template>
77+
</dom-if>
6478
<slot></slot>
6579
</paper-icon-item>
6680
<div slot="collapse-content" class="sub-menu">
@@ -73,6 +87,11 @@ class MenuItem extends ThemableMixin(PolymerElement) {
7387
return {
7488
key: String,
7589
label : String,
90+
href: {
91+
type: String,
92+
notify: true,
93+
value: null,
94+
},
7695
src : {
7796
type: String,
7897
reflectToAttribute: true
@@ -125,6 +144,11 @@ class MenuItem extends ThemableMixin(PolymerElement) {
125144
return !!(this.src || this.icon);
126145
}
127146

147+
__closeDrawer() {
148+
let container = this.closest('[fc-menuitem-container]');
149+
if (container) container.close();
150+
}
151+
128152
connectedCallback () {
129153
super.connectedCallback ();
130154
var slot = this.shadowRoot.querySelector("slot[name='menu-item']");

src/test/java/com/flowingcode/addons/applayout/ApplayoutDemoView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ private Component[] createMenuItems() {
181181

182182
new MenuItem("Toggle", "fc-menuitem-icons:empty").configure(mi -> mi.add(new PaperToggle())),
183183

184+
new MenuItem("External link").setHref("http://www.google.com"),
185+
186+
new MenuItem("Internal Link", InternalView.class),
187+
184188
// icon as VaadinIcon enum
185189
new MenuItem("Content", VaadinIcon.BOOK, () -> showHamletContent()).setCommand(MouseButton.MIDDLE,
186190
() -> {

src/test/java/com/flowingcode/addons/applayout/DemoView.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,4 @@ public void beforeEnter(BeforeEnterEvent event) {
3333
event.forwardTo(ApplayoutDemoView.class);
3434
}
3535

36-
}
37-
38-
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.flowingcode.addons.applayout;
2+
3+
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.component.html.Span;
5+
import com.vaadin.flow.router.Route;
6+
7+
@Route("internal-view")
8+
public class InternalView extends Div {
9+
10+
public InternalView() {
11+
add(new Span("Internal view"));
12+
}
13+
}

0 commit comments

Comments
 (0)