Only inputs react on binding

This commit is contained in:
2025-11-02 10:35:06 +01:00
parent c3d6958c1a
commit 7553c28f8e
2 changed files with 47 additions and 3 deletions

View File

@@ -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)

View File

@@ -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"