Browse Source

#109: factored out bind_text_* methods into mixin

Hannes Römer 2 years ago
parent
commit
8f8b9e6174
4 changed files with 25 additions and 45 deletions
  1. 19 0
      nicegui/binding.py
  2. 2 15
      nicegui/elements/button.py
  3. 2 15
      nicegui/elements/expansion.py
  4. 2 15
      nicegui/elements/label.py

+ 19 - 0
nicegui/binding.py

@@ -101,3 +101,22 @@ class BindableProperty:
         update_views(propagate(owner, self.name))
         if value_changed and self.on_change is not None:
             self.on_change(owner, value)
+
+
+class BindTextMixin:
+    """
+    Mixin providing bind text methods.
+    """
+
+    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
+        bind_to(self, 'text', target_object, target_name, forward=forward)
+        return self
+
+    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
+        bind_from(self, 'text', target_object, target_name, backward=backward)
+        return self
+
+    def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
+        bind_from(self, 'text', target_object, target_name, backward=backward)
+        bind_to(self, 'text', target_object, target_name, forward=forward)
+        return self

+ 2 - 15
nicegui/elements/button.py

@@ -2,12 +2,12 @@ from typing import Callable, Optional
 
 import justpy as jp
 
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindTextMixin
 from ..events import ClickEventArguments, handle_event
 from .element import Element
 
 
-class Button(Element):
+class Button(Element, BindTextMixin):
     text = BindableProperty()
 
     def __init__(self, text: str = '', *, on_click: Optional[Callable] = None):
@@ -30,16 +30,3 @@ class Button(Element):
 
     def set_text(self, text: str):
         self.text = text
-
-    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self
-
-    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        return self
-
-    def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self

+ 2 - 15
nicegui/elements/expansion.py

@@ -2,11 +2,11 @@ from typing import Optional
 
 import justpy as jp
 
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindTextMixin
 from .group import Group
 
 
-class Expansion(Group):
+class Expansion(Group, BindTextMixin):
     text = BindableProperty()
 
     def __init__(self, text: str, *, icon: Optional[str] = None):
@@ -25,16 +25,3 @@ class Expansion(Group):
 
     def set_text(self, text: str):
         self.text = text
-
-    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self
-
-    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        return self
-
-    def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self

+ 2 - 15
nicegui/elements/label.py

@@ -1,10 +1,10 @@
 import justpy as jp
 
-from ..binding import BindableProperty, bind_from, bind_to
+from ..binding import BindableProperty, BindTextMixin
 from .element import Element
 
 
-class Label(Element):
+class Label(Element, BindTextMixin):
     text = BindableProperty()
 
     def __init__(self, text: str = ''):
@@ -22,16 +22,3 @@ class Label(Element):
 
     def set_text(self, text: str):
         self.text = text
-
-    def bind_text_to(self, target_object, target_name, forward=lambda x: x):
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self
-
-    def bind_text_from(self, target_object, target_name, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        return self
-
-    def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
-        bind_from(self, 'text', target_object, target_name, backward=backward)
-        bind_to(self, 'text', target_object, target_name, forward=forward)
-        return self