Skip to content

Commit fe84828

Browse files
mstahvcaalador
andauthored
feat: provide an API to make router ignore an anchor (#17745)
Anchors in Vaadin are simply broken if the url happens to be relative. With this API that can be fixed. Fixes #17617 --------- Co-authored-by: Mikael Grankvist <mgrankvi@vaadin.com> Co-authored-by: caalador <mikael.grankvist@vaadin.com>
1 parent 6b518e1 commit fe84828

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

flow-html-components/src/main/java/com/vaadin/flow/component/html/Anchor.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,32 @@ public void removeHref() {
170170
*/
171171
public void setHref(AbstractStreamResource href) {
172172
this.href = href;
173-
getElement().setAttribute(ROUTER_IGNORE_ATTRIBUTE, true);
173+
setRouterIgnore(true);
174174
assignHrefAttribute();
175175
}
176176

177+
/**
178+
* The routing mechanism in Vaadin by default intercepts all anchor elements
179+
* with relative URL. This method can be used make the router ignore this
180+
* anchor and this way make this anchor behave normally and cause a full
181+
* page load.
182+
*
183+
* @param ignore
184+
* true if this link should not be intercepted by the single-page
185+
* web application routing mechanism in Vaadin.
186+
*/
187+
public void setRouterIgnore(boolean ignore) {
188+
getElement().setAttribute(ROUTER_IGNORE_ATTRIBUTE, ignore);
189+
}
190+
191+
/**
192+
* @return true if this anchor should be ignored by the Vaadin router and
193+
* behave normally.
194+
*/
195+
public boolean isRouterIgnore() {
196+
return getElement().hasAttribute(ROUTER_IGNORE_ATTRIBUTE);
197+
}
198+
177199
/**
178200
* Gets the URL that this anchor links to.
179201
*

flow-html-components/src/test/java/com/vaadin/flow/component/html/AnchorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public void getTargetValue_useSomeStringValue_targetIsReturned() {
138138
protected void addProperties() {
139139
addStringProperty("href", "", false);
140140
addOptionalStringProperty("target");
141+
addProperty("routerIgnore", boolean.class, false, true, false, true,
142+
"router-ignore");
141143
}
142144

143145
@Test

flow-html-components/src/test/java/com/vaadin/flow/component/html/ComponentProperty.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
public class ComponentProperty {
2525
public String name;
26+
public String propertyOrAttributeTag;
2627
public Object defaultValue, otherValue;
2728
public boolean optional;
2829
public boolean removeDefault;
@@ -34,13 +35,18 @@ public <T> ComponentProperty(Class<? extends Component> componentType,
3435
boolean optional, boolean removeDefault) {
3536
this.componentType = componentType;
3637
this.name = name;
38+
this.propertyOrAttributeTag = name;
3739
this.type = type;
3840
this.defaultValue = defaultValue;
3941
this.optional = optional;
4042
this.removeDefault = removeDefault;
4143
this.otherValue = otherValue == null ? name + name : otherValue;
4244
}
4345

46+
public void setPropertyOrAttributeTag(String propertyOrAttributeTag) {
47+
this.propertyOrAttributeTag = propertyOrAttributeTag;
48+
}
49+
4450
public boolean isOptional() {
4551
return optional;
4652
}

flow-html-components/src/test/java/com/vaadin/flow/component/html/ComponentTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ protected <U> void addProperty(String propertyName, Class<U> propertyType,
109109
isOptional, removeDefault));
110110
}
111111

112+
protected <U> void addProperty(String propertyName, Class<U> propertyType,
113+
U defaultValue, U otherValue, boolean isOptional,
114+
boolean removeDefault, String propertyOrAttributeTag) {
115+
ComponentProperty property = new ComponentProperty(
116+
getComponent().getClass(), propertyName, propertyType,
117+
defaultValue, otherValue, isOptional, removeDefault);
118+
property.setPropertyOrAttributeTag(propertyOrAttributeTag);
119+
properties.add(property);
120+
}
121+
112122
protected Component createComponent() throws InstantiationException,
113123
IllegalAccessException, ClassNotFoundException {
114124
String componentClass = getClass().getName().replace("Test", "");
@@ -370,7 +380,8 @@ private void assertNonDefaultValues() throws Exception {
370380
Assert.assertEquals(property.otherValue,
371381
property.getUsingGetter(component));
372382
}
373-
assertPropertyOrAttribute(component, property.name);
383+
assertPropertyOrAttribute(component,
384+
property.propertyOrAttributeTag);
374385
}
375386
}
376387

0 commit comments

Comments
 (0)