mirror of
https://github.com/django-components/django-components.git
synced 2025-07-24 00:35:15 +00:00
Add support for calling register as a decorator (#38)
This commit is contained in:
commit
b9446c2818
2 changed files with 25 additions and 0 deletions
|
@ -97,3 +97,20 @@ def is_slot_node(node):
|
||||||
|
|
||||||
# This variable represents the global component registry
|
# This variable represents the global component registry
|
||||||
registry = ComponentRegistry()
|
registry = ComponentRegistry()
|
||||||
|
|
||||||
|
def register(name):
|
||||||
|
"""Class decorator to register a component.
|
||||||
|
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
@register("my_component")
|
||||||
|
class MyComponent(component.Component):
|
||||||
|
...
|
||||||
|
|
||||||
|
"""
|
||||||
|
def decorator(component):
|
||||||
|
registry.register(name=name, component=component)
|
||||||
|
return component
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
|
@ -9,6 +9,14 @@ from django_components import component
|
||||||
|
|
||||||
|
|
||||||
class ComponentRegistryTest(SimpleTestCase):
|
class ComponentRegistryTest(SimpleTestCase):
|
||||||
|
|
||||||
|
def test_register_class_decorator(self):
|
||||||
|
@component.register("decorated_component")
|
||||||
|
class TestComponent(component.Component):
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.assertEqual(component.registry.get("decorated_component"), TestComponent)
|
||||||
|
|
||||||
def test_empty_component(self):
|
def test_empty_component(self):
|
||||||
class EmptyComponent(component.Component):
|
class EmptyComponent(component.Component):
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue