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

View File

@@ -1,11 +1,12 @@
from dataclasses import dataclass
from typing import Any
import pytest
from fasthtml.components import Input, Label
from fasthtml.fastapp import fast_app
from myfasthtml.controls.helpers import mk
from myfasthtml.core.bindings import Binding
from myfasthtml.core.bindings import Binding, DetectionMode, UpdateMode, BooleanConverter
from myfasthtml.core.commands import Command, CommandsManager
from myfasthtml.test.testclient import MyTestClient, TestableElement
@@ -16,7 +17,7 @@ def new_value(value):
@dataclass
class Data:
value: str
value: Any
@pytest.fixture()
@@ -62,7 +63,7 @@ class TestingCommand:
class TestingBindings:
def test_i_can_bind_elements(self, user, rt):
def test_i_can_bind_input(self, user, rt):
@rt("/")
def index():
data = Data("hello world")
@@ -76,4 +77,27 @@ class TestingBindings:
user.should_see("")
testable_input = user.find_element("input")
testable_input.send("new value")
user.should_see("new value") # the one from the label
user.should_see("new value") # the one from the label
def test_i_can_bind_checkbox(self, user, rt):
@rt("/")
def index():
data = Data(True)
input_elt = Input(name="input_name", type="checkbox")
label_elt = Label()
mk.manage_binding(input_elt, Binding(data, ft_attr="checked",
detection_mode=DetectionMode.AttributePresence,
update_mode=UpdateMode.AttributePresence,
data_converter=BooleanConverter()))
mk.manage_binding(label_elt, Binding(data))
return input_elt, label_elt
user.open("/")
user.should_see("")
testable_input = user.find_element("input")
testable_input.check()
user.should_see("True")
testable_input.uncheck()
user.should_see("False")