From 0fc937264efc8090d3987518feff122d7f0c4b48 Mon Sep 17 00:00:00 2001 From: Khalid Abuhakmeh Date: Wed, 3 Jan 2024 13:42:09 -0500 Subject: [PATCH] Update HtmxResponseHeaders with new constants and methods The HtmxResponseHeaders class has been updated to include new constants and a new method 'Reselect'. The constants have been rearranged for better reference based on the htmx documentation. The 'Reselect' method allows to choose specific parts of the response for swapping in, overriding an existing 'hx-select' on the triggering element. #40 --- src/Htmx/HtmxResponseHeaders.cs | 37 +++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Htmx/HtmxResponseHeaders.cs b/src/Htmx/HtmxResponseHeaders.cs index 97c16fe..bdf8482 100644 --- a/src/Htmx/HtmxResponseHeaders.cs +++ b/src/Htmx/HtmxResponseHeaders.cs @@ -22,20 +22,32 @@ public class HtmxResponseHeaders public static class Keys { - public const string PushUrl = "HX-Push-Url"; + // Sorted by https://htmx.org/reference/#response_headers to make it easier to update public const string Location = "HX-Location"; + public const string PushUrl = "HX-Push-Url"; public const string Redirect = "HX-Redirect"; public const string Refresh = "HX-Refresh"; + public const string ReplaceUrl = "HX-Replace-Url"; + public const string Reswap = "HX-Reswap"; + public const string Retarget = "HX-Retarget"; + public const string Reselect = "HX-Reselect"; public const string Trigger = "HX-Trigger"; public const string TriggerAfterSettle = "HX-Trigger-After-Settle"; public const string TriggerAfterSwap = "HX-Trigger-After-Swap"; - public const string Reswap = "HX-Reswap"; - public const string Retarget = "HX-Retarget"; - public const string ReplaceUrl = "HX-Replace-Url"; public static string[] All { get; } = new[] { - PushUrl, Location, Redirect, Refresh, Trigger, TriggerAfterSettle, TriggerAfterSwap, Reswap, Retarget, ReplaceUrl + Location, + PushUrl, + Redirect, + Refresh, + ReplaceUrl, + Reswap, + Retarget, + Reselect, + Trigger, + TriggerAfterSettle, + TriggerAfterSwap, }; } @@ -88,6 +100,19 @@ public HtmxResponseHeaders Reswap(string value) _headers[Keys.Reswap] = value; return this; } + + /// + /// a CSS selector that allows you to choose which part of the response is used to be swapped in. + /// Overrides an existing hx-select on the triggering element + /// See https://htmx.org/attributes/hx-select/ for hx-select behavior. + /// + /// + /// + public HtmxResponseHeaders Reselect(string value) + { + _headers[Keys.Reselect] = value; + return this; + } /// /// Allows you to do a client-side redirect that does not do a full page reload @@ -246,7 +271,7 @@ private void ParsePossibleExistingTriggers(string headerKey, HtmxTriggerTiming t // this might still throw :( var jsonObject = JsonNode.Parse(ref reader)?.AsObject(); // Load any existing triggers - foreach (var (key, value) in jsonObject!) + foreach (var (key, value) in jsonObject!) WithTrigger(key, value, timing); } else