I can bind radio

This commit is contained in:
2025-11-07 21:28:19 +01:00
parent cc11e4edaa
commit e8ecf72205
12 changed files with 965 additions and 156 deletions

View File

@@ -1,8 +1,8 @@
from fasthtml.components import *
from myfasthtml.core.bindings import Binding, BooleanConverter, DetectionMode, UpdateMode
from myfasthtml.core.bindings import Binding
from myfasthtml.core.commands import Command
from myfasthtml.core.utils import merge_classes, get_default_ft_attr, is_checkbox
from myfasthtml.core.utils import merge_classes
class mk:
@@ -36,29 +36,12 @@ class mk:
return ft
@staticmethod
def manage_binding(ft, binding: Binding):
def manage_binding(ft, binding: Binding, ft_attr=None):
if not binding:
return ft
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)
ft_name = ft.attrs.get("name")
if is_checkbox(ft):
data_converter = BooleanConverter()
detection_mode = DetectionMode.AttributePresence
update_mode = UpdateMode.AttributePresence
else:
data_converter = None
detection_mode = None
update_mode = None
binding.bind_ft(ft, ft_name, ft_attr, data_converter, detection_mode, update_mode) # force the ft
binding.bind_ft(ft, ft_attr)
binding.init()
return ft
@staticmethod

View File

@@ -7,7 +7,7 @@ from fasthtml.fastapp import fast_app
from myutils.observable import make_observable, bind, collect_return_values, unbind
from myfasthtml.core.constants import Routes, ROUTE_ROOT
from myfasthtml.core.utils import get_default_attr
from myfasthtml.core.utils import get_default_attr, get_default_ft_attr, is_checkbox, is_radio
bindings_app, bindings_rt = fast_app()
logger = logging.getLogger("Bindings")
@@ -61,27 +61,29 @@ class AttrPresentDetection(AttrChangedDetection):
class FtUpdate:
def update(self, ft, ft_name, ft_attr, old, new):
def update(self, ft, ft_name, ft_attr, old, new, converter):
pass
class ValueChangeFtUpdate(FtUpdate):
def update(self, ft, ft_name, ft_attr, old, new):
def update(self, ft, ft_name, ft_attr, old, new, converter):
# simple mode, just update the text or the attribute
new_to_use = converter.convert(new) if converter else new
if ft_attr is None:
ft.children = (new,)
ft.children = (new_to_use,)
else:
ft.attrs[ft_attr] = new
ft.attrs[ft_attr] = new_to_use
return ft
class AttributePresenceFtUpdate(FtUpdate):
def update(self, ft, ft_name, ft_attr, old, new):
def update(self, ft, ft_name, ft_attr, old, new, converter):
# attribute presence mode, toggle the attribute (add or remove it)
new_to_use = converter.convert(new) if converter else new
if ft_attr is None:
ft.children = (bool(new),)
ft.children = (bool(new_to_use),)
else:
ft.attrs[ft_attr] = "true" if new else None # FastHtml auto remove None attributes
ft.attrs[ft_attr] = "true" if new_to_use else None # FastHtml auto remove None attributes
return ft
@@ -104,6 +106,14 @@ class BooleanConverter(DataConverter):
return False
class RadioConverter(DataConverter):
def __init__(self, radio_value):
self.radio_value = radio_value
def convert(self, data):
return data == self.radio_value
class Binding:
def __init__(self, data: Any, attr: str = None):
"""
@@ -136,8 +146,8 @@ class Binding:
def bind_ft(self,
ft,
name,
attr=None,
name=None,
data_converter: DataConverter = None,
detection_mode: DetectionMode = None,
update_mode: UpdateMode = None):
@@ -159,18 +169,37 @@ class Binding:
if self._is_active:
self.deactivate()
if ft.tag in ["input"]:
# I must not force the htmx
if {"hx-post", "hx_post"} & set(ft.attrs.keys()):
raise ValueError(f"Binding '{self.id}': htmx post already set on input.")
# update the component to post on the correct route input and forms only
htmx = self.get_htmx_params()
ft.attrs |= htmx
# Configure UI elements
self.ft = self._safe_ft(ft)
self.ft_name = name
self.ft_attr = attr
self.ft_name = name or ft.attrs.get("name")
self.ft_attr = attr or get_default_ft_attr(ft)
if is_checkbox(ft):
default_data_converter = BooleanConverter()
default_detection_mode = DetectionMode.AttributePresence
default_update_mode = UpdateMode.AttributePresence
elif is_radio(ft):
default_data_converter = RadioConverter(ft.attrs["value"])
default_detection_mode = DetectionMode.ValueChange
default_update_mode = UpdateMode.AttributePresence
else:
default_data_converter = None
default_detection_mode = DetectionMode.ValueChange
default_update_mode = UpdateMode.ValueChange
# Update optional parameters if provided
if data_converter is not None:
self.data_converter = data_converter
if detection_mode is not None:
self.detection_mode = detection_mode
if update_mode is not None:
self.update_mode = update_mode
self.data_converter = data_converter or default_data_converter
self.detection_mode = detection_mode or default_detection_mode
self.update_mode = update_mode or default_update_mode
# Create strategy objects
self._detection = self._factory(self.detection_mode)
@@ -187,6 +216,16 @@ class Binding:
"hx-vals": f'{{"b_id": "{self.id}"}}',
}
def init(self):
"""
Initialise the UI element with the value of the data
:return:
"""
old_value = None # to complicated to retrieve as it depends on the nature of self.ft
new_value = getattr(self.data, self.data_attr)
self.notify(old_value, new_value)
return self
def notify(self, old, new):
"""
Callback when the data attribute changes.
@@ -204,16 +243,21 @@ class Binding:
return None
logger.debug(f"Binding '{self.id}': Changing from '{old}' to '{new}'")
self.ft = self._update.update(self.ft, self.ft_name, self.ft_attr, old, new)
self.ft = self._update.update(self.ft, self.ft_name, self.ft_attr, old, new, self.data_converter)
self.ft.attrs["hx-swap-oob"] = "true"
return self.ft
def update(self, values: dict):
"""
Called by the FastHTML router when a request is received.
:param values:
:return: the list of updated elements (all elements that are bound to this binding)
"""
logger.debug(f"Binding '{self.id}': Updating with {values=}.")
matches, value = self._detection.matches(values)
if matches:
setattr(self.data, self.data_attr, self.data_converter.convert(value) if self.data_converter else value)
setattr(self.data, self.data_attr, value)
res = collect_return_values(self.data)
return res

View File

@@ -113,8 +113,17 @@ def is_checkbox(elt):
return False
def is_radio(elt):
if isinstance(elt, (FT, MyFT)):
return elt.tag == "input" and elt.attrs.get("type", None) == "radio"
elif isinstance(elt, Tag):
return elt.name == "input" and elt.attrs.get("type", None) == "radio"
else:
return False
@utils_rt(Routes.Commands)
def post(session: str, c_id: str):
def post(session, c_id: str):
"""
Default routes for all commands.
:param session:
@@ -131,7 +140,7 @@ def post(session: str, c_id: str):
@utils_rt(Routes.Bindings)
def post(session: str, b_id: str, values: dict):
def post(session, b_id: str, values: dict):
"""
Default routes for all bindings.
:param session:

View File

@@ -14,10 +14,13 @@ class Predicate:
raise NotImplementedError
def __str__(self):
return f"{self.__class__.__name__}({self.value})"
return f"{self.__class__.__name__}({self.value if self.value is not None else ''})"
def __repr__(self):
return f"{self.__class__.__name__}({self.value if self.value is not None else ''})"
def __eq__(self, other):
if not isinstance(other, Predicate):
if type(self) is not type(other):
return False
return self.value == other.value
@@ -25,7 +28,24 @@ class Predicate:
return hash(self.value)
class StartsWith(Predicate):
class AttrPredicate(Predicate):
"""
Predicate that validates an attribute value.
It's given as a value of an attribute.
"""
pass
class ChildrenPredicate(Predicate):
"""
Predicate given as a child of an element.
"""
def to_debug(self, element):
return element
class StartsWith(AttrPredicate):
def __init__(self, value):
super().__init__(value)
@@ -33,7 +53,7 @@ class StartsWith(Predicate):
return actual.startswith(self.value)
class Contains(Predicate):
class Contains(AttrPredicate):
def __init__(self, value):
super().__init__(value)
@@ -41,7 +61,7 @@ class Contains(Predicate):
return self.value in actual
class DoesNotContain(Predicate):
class DoesNotContain(AttrPredicate):
def __init__(self, value):
super().__init__(value)
@@ -49,16 +69,35 @@ class DoesNotContain(Predicate):
return self.value not in actual
class Empty(ChildrenPredicate):
def __init__(self):
super().__init__(None)
def validate(self, actual):
return len(actual.children) == 0 and len(actual.attrs) == 0
class AttributeForbidden(ChildrenPredicate):
"""
To validate that an attribute is not present in an element.
"""
def __init__(self, value):
super().__init__(value)
def validate(self, actual):
return self.value not in actual.attrs or actual.attrs[self.value] is None
def to_debug(self, element):
element.attrs[self.value] = "** NOT ALLOWED **"
return element
@dataclass
class DoNotCheck:
desc: str = None
@dataclass
class Empty:
desc: str = None
class ErrorOutput:
def __init__(self, path, element, expected):
self.path = path
@@ -99,12 +138,13 @@ class ErrorOutput:
self._add_to_output(error_str)
# render the children
if len(self.expected.children) > 0:
expected_children = [c for c in self.expected.children if not isinstance(c, ChildrenPredicate)]
if len(expected_children) > 0:
self.indent += " "
element_index = 0
for expected_child in self.expected.children:
for expected_child in expected_children:
if hasattr(expected_child, "tag"):
if element_index < len(self.element.children):
if element_index < len(expected_children):
# display the child
element_child = self.element.children[element_index]
child_str = self._str_element(element_child, expected_child, keep_open=False)
@@ -122,7 +162,8 @@ class ErrorOutput:
self._add_to_output(child_str)
else:
self._add_to_output(expected_child)
if expected_child in self.element.children:
self._add_to_output(expected_child)
self.indent = self.indent[:-2]
self._add_to_output(")")
@@ -145,9 +186,8 @@ class ErrorOutput:
# the attributes are compared to the expected element
elt_attrs = {attr_name: element.attrs.get(attr_name, "** MISSING **") for attr_name in
[attr_name for attr_name in expected.attrs if attr_name is not None]}
elt_attrs_str = " ".join(f'"{attr_name}"="{attr_value}"' for attr_name, attr_value in elt_attrs.items())
#
elt_attrs_str = " ".join(f'"{attr_name}"="{attr_value}"' for attr_name, attr_value in elt_attrs.items())
tag_str = f"({element.tag} {elt_attrs_str}"
# manage the closing tag
@@ -157,8 +197,8 @@ class ErrorOutput:
tag_str += "..." if elt_attrs_str == "" else " ..."
else:
# close the tag if there are no children
if len(element.children) == 0: tag_str += ")"
if len([c for c in element.children if not isinstance(c, Predicate)]) == 0: tag_str += ")"
return tag_str
def _detect_error(self, element, expected):
@@ -307,16 +347,18 @@ def matches(actual, expected, path=""):
_actual=actual.tag,
_expected=expected.tag)
# special case when the expected element is empty
if len(expected.children) > 0 and isinstance(expected.children[0], Empty):
assert len(actual.children) == 0, _error_msg("Actual is not empty:", _actual=actual)
assert len(actual.attrs) == 0, _error_msg("Actual is not empty:", _actual=actual)
return True
# special conditions
for predicate in [c for c in expected.children if isinstance(c, ChildrenPredicate)]:
assert predicate.validate(actual), \
_error_msg(f"The condition '{predicate}' is not satisfied.",
_actual=actual,
_expected=predicate.to_debug(expected))
# compare the attributes
for expected_attr, expected_value in expected.attrs.items():
assert expected_attr in actual.attrs, _error_msg(f"'{expected_attr}' is not found in Actual.",
_actual=actual.attrs)
_actual=actual,
_expected=expected)
if isinstance(expected_value, Predicate):
assert expected_value.validate(actual.attrs[expected_attr]), \
@@ -327,14 +369,15 @@ def matches(actual, expected, path=""):
else:
assert actual.attrs[expected_attr] == expected.attrs[expected_attr], \
_error_msg(f"The values are different for '{expected_attr}': ",
_actual=actual.attrs[expected_attr],
_expected=expected.attrs[expected_attr])
_actual=actual,
_expected=expected)
# compare the children
if len(actual.children) < len(expected.children):
expected_children = [c for c in expected.children if not isinstance(c, Predicate)]
if len(actual.children) < len(expected_children):
_assert_error("Actual is lesser than expected: ", _actual=actual, _expected=expected)
for actual_child, expected_child in zip(actual.children, expected.children):
for actual_child, expected_child in zip(actual.children, expected_children):
assert matches(actual_child, expected_child, path=path)
else:

View File

@@ -300,7 +300,7 @@ class TestableElement:
self.fields[name] = raw_value
elif name not in self.fields:
# If no radio is checked yet, don't set a default
pass
self.fields[name] = None
elif input_type == 'number':
# Number: int or float based on value
@@ -1104,6 +1104,9 @@ class TestableRadio(TestableControl):
source: The source HTML or BeautifulSoup Tag.
"""
super().__init__(client, source, "input")
nb_radio_buttons = len(self.element.find_all("input", type="radio"))
assert nb_radio_buttons > 0, "No radio buttons found."
assert nb_radio_buttons < 2, "Only one radio button per name is supported."
self._radio_value = self.my_ft.attrs.get('value', '')
@property
@@ -1573,6 +1576,9 @@ class MyTestClient:
f"Found {len(remaining)} forms (with the specified fields). Expected exactly 1."
)
def find_input(self, identifier: str) -> TestableInput:
pass
def get_content(self) -> str:
"""
Get the raw HTML content of the last opened page.