Skip to content

Commit

Permalink
Add annotation support for HX-Retarget
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaggi committed May 1, 2024
1 parent 7dcfdfe commit c8669d3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ The following annotations are currently supported:
* [@HxRefresh](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRefresh.html)
* [@HxReplaceUrl](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReplaceUrl.html)
* [@HxReswap](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReswap.html)
* [@HxRetarget](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRetarget.html)
* [@HxTrigger](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxTrigger.html)

>**Note** Please refer to the related Javadoc to learn more about the available options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public boolean preHandle(HttpServletRequest request,
setHxRedirect(response, method);
setHxReplaceUrl(response, method);
setHxReswap(response, method);
setHxRetarget(response, method);
setHxTrigger(response, method);
setHxRefresh(response, method);
setVary(request, response);
Expand Down Expand Up @@ -89,6 +90,13 @@ private void setHxReswap(HttpServletResponse response, Method method) {
}
}

private void setHxRetarget(HttpServletResponse response, Method method) {
HxRetarget methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxRetarget.class);
if (methodAnnotation != null) {
response.setHeader(HX_RETARGET.getValue(), methodAnnotation.value());
}
}

private void setHxTrigger(HttpServletResponse response, Method method) {
HxTrigger methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxTrigger.class);
if (methodAnnotation != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.wimdeblauwe.htmx.spring.boot.mvc;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to specify a CSS selector that updates the target of
* the content update to a different element on the page.
*
* @see <a href="https://htmx.org/reference/#response_headers">HX-Retarget</a>
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HxRetarget {

/**
* A CSS selector that updates the target of the content update to a different element on the page.
*/
String value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@ public void testHxReswap() throws Exception {
.andExpect(header().string("HX-Reswap", "innerHTML swap:300ms"));
}

@Test
public void testHxRetarget() throws Exception {
mockMvc.perform(get("/hx-retarget"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Retarget", "#target"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ public String hxReswap() {
return "";
}

@GetMapping("/hx-retarget")
@HxRetarget("#target")
@ResponseBody
public String hxRetarget() {
return "";
}

}

0 comments on commit c8669d3

Please sign in to comment.