Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Oct 2, 2023
1 parent 48806e3 commit ff3f6e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
18 changes: 7 additions & 11 deletions nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
46 changes: 23 additions & 23 deletions tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand All @@ -233,16 +233,16 @@ 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
MyButton.default_style('font-family: courier')
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')
Expand Down
12 changes: 6 additions & 6 deletions website/more_documentation/element_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit ff3f6e5

Please sign in to comment.