diff --git a/src/myfasthtml/controls/helpers.py b/src/myfasthtml/controls/helpers.py index 6078c62..954e9f2 100644 --- a/src/myfasthtml/controls/helpers.py +++ b/src/myfasthtml/controls/helpers.py @@ -38,9 +38,10 @@ class mk: @staticmethod def manage_binding(ft, binding: Binding): if binding: - # update the component to post on the correct route - htmx = binding.get_htmx_params() - ft.attrs |= htmx + if ft.tag in ["input"]: + # update the component to post on the correct route input and forms only + htmx = binding.get_htmx_params() + ft.attrs |= htmx # update the binding with the ft ft_attr = binding.ft_attr or get_default_ft_attr(ft) diff --git a/tests/controls/test_helpers.py b/tests/controls/test_helpers.py index b5326df..5269e13 100644 --- a/tests/controls/test_helpers.py +++ b/tests/controls/test_helpers.py @@ -1,13 +1,22 @@ +from dataclasses import dataclass +from typing import Any + import pytest from fasthtml.components import * from fasthtml.fastapp import fast_app from myfasthtml.controls.helpers import mk +from myfasthtml.core.bindings import Binding from myfasthtml.core.commands import Command from myfasthtml.test.matcher import matches from myfasthtml.test.testclient import MyTestClient +@dataclass +class Data: + value: Any + + @pytest.fixture() def user(): test_app, rt = fast_app(default_hdrs=False) @@ -46,3 +55,37 @@ def test_i_can_mk_button_with_command(user, rt): user.find_element("button").click() user.should_see("this is my new value") + + +class TestingBindings: + @pytest.fixture() + def data(self): + return Data("value") + + def test_i_can_bind_an_input(self, data): + elt = Input(name="input_elt", value="hello") + binding = Binding(data, "value") + elt = mk.manage_binding(elt, binding) + + # element is updated + assert "hx-post" in elt.attrs + assert "hx-vals" in elt.attrs + assert "b_id" in elt.attrs["hx-vals"] + + # binding is also updated + assert binding.ft == elt + assert binding.ft_name == "input_elt" + + def test_i_can_bind_none_input(self, data): + elt = Label("hello", name="input_elt") + binding = Binding(data, "value") + elt = mk.manage_binding(elt, binding) + + # element is updated + assert "hx-post" not in elt.attrs + assert "hx-get" not in elt.attrs + + # binding is also updated + assert binding.ft == elt + assert binding.ft_name == "input_elt" +