Skip to content

Commit

Permalink
Add annotation support for HX-Reselect
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaggi committed May 1, 2024
1 parent 64b06c3 commit b03bf62
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The following annotations are currently supported:
* [@HxRedirect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRedirect.html)
* [@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)
* [@HxReselect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public boolean preHandle(HttpServletRequest request,
setHxReplaceUrl(response, method);
setHxReswap(response, method);
setHxRetarget(response, method);
setHxReselect(response, method);
setHxTrigger(response, method);
setHxRefresh(response, method);
setVary(request, response);
Expand Down Expand Up @@ -97,6 +98,13 @@ private void setHxRetarget(HttpServletResponse response, Method method) {
}
}

private void setHxReselect(HttpServletResponse response, Method method) {
HxReselect methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxReselect.class);
if (methodAnnotation != null) {
response.setHeader(HX_RESELECT.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,24 @@
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 allows you to choose which part
* of the response is used to be swapped in.
*
* @see <a href="https://htmx.org/reference/#response_headers">HX-Retarget</a>
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HxReselect {

/**
* A CSS selector that allows you to choose which part of the response is used to be swapped in.
* <p>Overrides an existing <a href="https://htmx.org/attributes/hx-select/">hx-select</a> on the triggering element.
*/
String value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ public void testHxRetarget() throws Exception {
.andExpect(header().string("HX-Retarget", "#target"));
}

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

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

@GetMapping("/hx-reselect")
@HxReselect("#target")
@ResponseBody
public String hxReselect() {
return "";
}

}

0 comments on commit b03bf62

Please sign in to comment.