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,7 +38,8 @@ class mk:
@staticmethod @staticmethod
def manage_binding(ft, binding: Binding): def manage_binding(ft, binding: Binding):
if binding: if binding:
# update the component to post on the correct route if ft.tag in ["input"]:
# update the component to post on the correct route input and forms only
htmx = binding.get_htmx_params() htmx = binding.get_htmx_params()
ft.attrs |= htmx ft.attrs |= htmx

View File

@@ -1,13 +1,22 @@
from dataclasses import dataclass
from typing import Any
import pytest import pytest
from fasthtml.components import * from fasthtml.components import *
from fasthtml.fastapp import fast_app from fasthtml.fastapp import fast_app
from myfasthtml.controls.helpers import mk from myfasthtml.controls.helpers import mk
from myfasthtml.core.bindings import Binding
from myfasthtml.core.commands import Command from myfasthtml.core.commands import Command
from myfasthtml.test.matcher import matches from myfasthtml.test.matcher import matches
from myfasthtml.test.testclient import MyTestClient from myfasthtml.test.testclient import MyTestClient
@dataclass
class Data:
value: Any
@pytest.fixture() @pytest.fixture()
def user(): def user():
test_app, rt = fast_app(default_hdrs=False) 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.find_element("button").click()
user.should_see("this is my new value") 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"