I can bind checkbox (needs refactoring)

This commit is contained in:
2025-11-02 10:11:15 +01:00
parent aaba6a5468
commit c3d6958c1a
4 changed files with 217 additions and 17 deletions

View File

@@ -4,7 +4,7 @@ import pytest
from fasthtml.components import Label, Input
from myutils.observable import collect_return_values
from myfasthtml.core.bindings import BindingsManager, Binding
from myfasthtml.core.bindings import BindingsManager, Binding, DetectionMode
@dataclass
@@ -94,3 +94,41 @@ def test_i_can_collect_updates_values(data):
data.value = "another value"
collected = collect_return_values(data)
assert collected == [elt]
def test_i_can_react_to_value_change(data):
elt = Input(name="input_elt", value="hello")
binding = Binding(data, ft=elt, ft_name="input_elt", ft_attr="value")
res = binding.update({"input_elt": "new value"})
assert len(res) == 1
def test_i_do_not_react_to_other_value_change(data):
elt = Input(name="input_elt", value="hello")
binding = Binding(data, ft=elt, ft_name="input_elt", ft_attr="value")
res = binding.update({"other_input_elt": "new value"})
assert res is None
def test_i_can_react_to_attr_presence(data):
elt = Input(name="input_elt", type="checkbox")
binding = Binding(data, ft=elt, ft_name="input_elt", ft_attr="checked",
detection_mode=DetectionMode.AttributePresence)
res = binding.update({"checked": "true"})
assert len(res) == 1
def test_i_can_react_to_attr_non_presence(data):
elt = Input(name="input_elt", type="checkbox")
binding = Binding(data, ft=elt, ft_name="input_elt", ft_attr="checked",
detection_mode=DetectionMode.AttributePresence)
res = binding.update({})
assert len(res) == 1