Skip to content

Enhancement: support 'select' attribute of hxlocation #11

@krikkiteer

Description

@krikkiteer

Summary

According to the current htmx-docs, HXLocation supports an additional header 'select' which allows extracting elements of a response that shall be put into a given target.
This is currently not reflected in litestar-htmx's HXLocation.

It would be convenient to be able to use this like shown below as it extends coverage of htmx funcs in the plugin.

relevant htmx-docs can be found here: https://htmx.org/headers/hx-location/

Basic Example

return HXLocation(
    redirect_to=request.app.route_reverse("organisations:detail:contacts", org_id=org_id),
    target="#orgmain",  
    select="#orgmain",
    status_code=200,
)

Drawbacks and Impact

i can't see any drawbacks or incompatibilities that might arise, since its a completely optional parameter.

Unresolved questions

A solution might look like:

diff --git a/litestar_htmx/response.py b/litestar_htmx/response.py
index 74a49e7..4f248c9 100644
--- a/litestar_htmx/response.py
+++ b/litestar_htmx/response.py
@@ -135,6 +135,7 @@ class HXLocation(Response):
         source: str | None = None,
         event: str | None = None,
         target: str | None = None,
+        select: str | None = None,
         swap: ReSwapMethod | None = None,
         hx_headers: dict[str, Any] | None = None,
         values: dict[str, str] | None = None,
@@ -155,6 +156,7 @@ class HXLocation(Response):
                     source=source,
                     event=event,
                     target=target,
+                    select=select,
                     swap=swap,
                     values=values,
                     hx_headers=hx_headers,
diff --git a/litestar_htmx/types.py b/litestar_htmx/types.py
index 2ef61a7..2647263 100644
--- a/litestar_htmx/types.py
+++ b/litestar_htmx/types.py
@@ -24,6 +24,7 @@ class LocationType(TypedDict):
     source: str | None
     event: str | None
     target: str | None
+    select: str | None
     swap: ReSwapMethod | None
     values: dict[str, str] | None
     hx_headers: dict[str, Any] | None
diff --git a/tests/test_htmx_response.py b/tests/test_htmx_response.py
index 6489db1..4de0920 100644
--- a/tests/test_htmx_response.py
+++ b/tests/test_htmx_response.py
@@ -219,6 +219,7 @@ async def test_hx_location_response_with_all_parameters() -> None:
             source="#button",
             event="click",
             target="#content",
+            select="#content",
             swap="innerHTML",
             hx_headers={"attribute": "value"},
             values={"action": "true"},
@@ -230,7 +231,7 @@ async def test_hx_location_response_with_all_parameters() -> None:
         assert response.status_code == HTTP_200_OK
         assert "Location" not in response.headers
         assert spec == (
-            '{"path":"/contact-us","source":"#button","event":"click","target":"#content","swap":"innerHTML",'
+            '{"path":"/contact-us","source":"#button","event":"click","target":"#content","select":"#content","swap":"innerHTML",'
             '"values":{"action":"true"},"hx_headers":{"attribute":"value"}}'
         )

question is - should i make a pull-request for this ? and are there any aspects or requirements for tests i did miss ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions