From 9fcba06e7564298da8742dc22d474ffa0f70eb63 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 26 Nov 2021 12:49:48 -0500 Subject: [PATCH] Add HX-Retarget header (#2) --- src/Directory.Build.props | 4 ++-- src/Htmx.Tests/Tests.fs | 11 +++++++++++ src/Htmx/Htmx.fs | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 5ae7a35..420830b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,8 +1,8 @@  - 0.9.2 - Complete view engine modules; add READMEs + 0.9.3 + Add support for HX-Retarget header (added in htmx 1.6.1) danieljsummers Bit Badger Solutions https://github.com/bit-badger/Giraffe.Htmx diff --git a/src/Htmx.Tests/Tests.fs b/src/Htmx.Tests/Tests.fs index 556ab93..e026770 100644 --- a/src/Htmx.Tests/Tests.fs +++ b/src/Htmx.Tests/Tests.fs @@ -250,6 +250,17 @@ module HandlerTests = Assert.Equal ("false", dic.["HX-Refresh"].[0]) } + [] + let ``withHxRetarget succeeds`` () = + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + task { + let! _ = withHxRetarget "#somewhereElse" next ctx + Assert.True (dic.ContainsKey "HX-Retarget") + Assert.Equal ("#somewhereElse", dic.["HX-Retarget"].[0]) + } + [] let ``withHxTrigger succeeds`` () = let ctx = Substitute.For () diff --git a/src/Htmx/Htmx.fs b/src/Htmx/Htmx.fs index 3258d38..a892516 100644 --- a/src/Htmx/Htmx.fs +++ b/src/Htmx/Htmx.fs @@ -74,6 +74,10 @@ module Handlers = let withHxRefresh : bool -> HttpHandler = toLowerBool >> setHttpHeader "HX-Refresh" + /// Allows you to override the `hx-target` attribute + let withHxRetarget : string -> HttpHandler = + setHttpHeader "HX-Retarget" + /// Allows you to trigger a single client side event let withHxTrigger : string -> HttpHandler = setHttpHeader "HX-Trigger"