From ff3f6e5c43abf51ffeff0df013ecc1a171e46149 Mon Sep 17 00:00:00 2001 From: Falko Schindler Date: Mon, 2 Oct 2023 17:43:49 +0200 Subject: [PATCH] code review --- nicegui/element.py | 18 +++----- tests/test_element.py | 46 +++++++++---------- .../element_documentation.py | 12 ++--- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/nicegui/element.py b/nicegui/element.py index 2f79f6bb3..c8c62065e 100644 --- a/nicegui/element.py +++ b/nicegui/element.py @@ -201,9 +201,7 @@ def default_classes(cls, add: Optional[str] = None, *, remove: Optional[str] = N :param remove: whitespace-delimited string of classes to remove from the element :param replace: whitespace-delimited string of classes to use instead of existing ones """ - new_classes = cls._update_classes_list(cls._default_classes, add, remove, replace) - if cls._default_classes != new_classes: - cls._default_classes = new_classes + cls._default_classes = cls._update_classes_list(cls._default_classes, add, remove, replace) return cls @staticmethod @@ -247,13 +245,12 @@ def default_style(cls, add: Optional[str] = None, *, remove: Optional[str] = Non :param remove: semicolon-separated list of styles to remove from the element :param replace: semicolon-separated list of styles to use instead of existing ones """ - style_dict = deepcopy(cls._default_style) if replace is None else {} + if replace is not None: + cls._default_style.clear() for key in cls._parse_style(remove): - style_dict.pop(key, None) - style_dict.update(cls._parse_style(add)) - style_dict.update(cls._parse_style(replace)) - if cls._default_style != style_dict: - cls._default_style = style_dict + cls._default_style.pop(key, None) + cls._default_style.update(cls._parse_style(add)) + cls._default_style.update(cls._parse_style(replace)) return cls @staticmethod @@ -309,8 +306,7 @@ def default_props(cls, add: Optional[str] = None, *, remove: Optional[str] = Non if key in cls._default_props: del cls._default_props[key] for key, value in cls._parse_props(add).items(): - if cls._default_props.get(key) != value: - cls._default_props[key] = value + cls._default_props[key] = value return cls def tooltip(self, text: str) -> Self: diff --git a/tests/test_element.py b/tests/test_element.py index 97df1e303..5e4c0fdbf 100644 --- a/tests/test_element.py +++ b/tests/test_element.py @@ -182,45 +182,45 @@ class MyButton(ui.button): def test_default_classes(): - ui.button.default_classes('bg-white; text-green;') + ui.button.default_classes('bg-white text-green') button_a = ui.button('Button A') button_b = ui.button('Button B') - assert 'bg-white;' in button_a._classes, 'default classes are set' - assert 'text-green;' in button_a._classes - assert 'bg-white;' in button_b._classes - assert 'text-green;' in button_b._classes + assert 'bg-white' in button_a._classes, 'default classes are set' + assert 'text-green' in button_a._classes + assert 'bg-white' in button_b._classes + assert 'text-green' in button_b._classes - ui.button.default_classes(remove='text-green;') + ui.button.default_classes(remove='text-green') button_c = ui.button('Button C') - assert 'text-green;' not in button_c._classes, '"text-green" class was removed' - assert 'bg-white;' in button_c._classes, 'other classes are still there' + assert 'text-green' not in button_c._classes, '"text-green" class was removed' + assert 'bg-white' in button_c._classes, 'other classes are still there' - ui.input.default_classes('text-black;') + ui.input.default_classes('text-black') input_a = ui.input() - assert 'text-black;' in input_a._classes - assert 'bg-white;' not in input_a._classes, 'default classes of ui.button do not affect ui.input' + assert 'text-black' in input_a._classes + assert 'bg-white' not in input_a._classes, 'default classes of ui.button do not affect ui.input' class MyButton(ui.button): pass - MyButton.default_classes('w-full;') + MyButton.default_classes('w-full') button_d = MyButton() button_e = ui.button() - assert 'w-full;' in button_d._classes - assert 'bg-white;' in button_d._classes, 'default classes are inherited' - assert 'w-full;' not in button_e._classes, 'default classes of MyButton do not affect ui.button' - assert 'bg-white;' in button_e._classes + assert 'w-full' in button_d._classes + assert 'bg-white' in button_d._classes, 'default classes are inherited' + assert 'w-full' not in button_e._classes, 'default classes of MyButton do not affect ui.button' + assert 'bg-white' in button_e._classes - ui.button.default_classes('h-40;').default_classes('max-h-80;') + ui.button.default_classes('h-40').default_classes('max-h-80') button_f = ui.button() - assert 'h-40;' in button_f._classes - assert 'max-h-80;' in button_f._classes + assert 'h-40' in button_f._classes + assert 'max-h-80' in button_f._classes def test_default_style(): ui.button.default_style('color: green; font-size: 200%') button_a = ui.button('Button A') button_b = ui.button('Button B') - assert button_a._style.get('color') == 'green', 'default style are set' + assert button_a._style.get('color') == 'green', 'default style is set' assert button_a._style.get('font-size') == '200%' assert button_b._style.get('color') == 'green' assert button_b._style.get('font-size') == '200%' @@ -233,7 +233,7 @@ def test_default_style(): ui.input.default_style('font-weight: 300') input_a = ui.input() assert input_a._style.get('font-weight') == '300' - assert input_a._style.get('font-size') is None, 'default style of ui.button do not affect ui.input' + assert input_a._style.get('font-size') is None, 'default style of ui.button does not affect ui.input' class MyButton(ui.button): pass @@ -241,8 +241,8 @@ class MyButton(ui.button): button_d = MyButton() button_e = ui.button() assert button_d._style.get('font-family') == 'courier' - assert button_d._style.get('font-size') == '200%', 'default style are inherited' - assert button_e._style.get('font-family') is None, 'default style of MyButton do not affect ui.button' + assert button_d._style.get('font-size') == '200%', 'default style is inherited' + assert button_e._style.get('font-family') is None, 'default style of MyButton does not affect ui.button' assert button_e._style.get('font-size') == '200%' ui.button.default_style('border: 2px').default_style('padding: 30px') diff --git a/website/more_documentation/element_documentation.py b/website/more_documentation/element_documentation.py index a76946ea8..2e3ce3b11 100644 --- a/website/more_documentation/element_documentation.py +++ b/website/more_documentation/element_documentation.py @@ -46,22 +46,22 @@ def default_props() -> None: Subclasses inherit the default classes of their parent class. ''') def default_classes() -> None: - ui.label.default_classes('bg-purple') + ui.label.default_classes('bg-blue-100 p-2') ui.label('Label A') ui.label('Label B') # END OF DEMO - ui.label.default_classes(remove='bg-purple') + ui.label.default_classes(remove='bg-blue-100 p-2') @text_demo('Default style', ''' - You can set default style for all elements of a certain class. + You can set a default style for all elements of a certain class. This way you can avoid repeating the same style over and over again. - Default style only apply to elements created after the default style was set. + A default style only applies to elements created after the default style was set. Subclasses inherit the default style of their parent class. ''') def default_style() -> None: - ui.label.default_style('color: green') + ui.label.default_style('color: tomato') ui.label('Label A') ui.label('Label B') # END OF DEMO - ui.label.default_style(remove='color: green') + ui.label.default_style(remove='color: tomato')