mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 14:28:18 +00:00
refactor: deprecate Component.input and add raw_args, raw_kwargs, raw_slots (#1233)
* refactor: deprecate Component.input and add raw_args, raw_kwargs, raw_slots * docs: update changelog
This commit is contained in:
parent
eceebb9696
commit
04f79a6e6b
15 changed files with 295 additions and 101 deletions
|
@ -2115,7 +2115,7 @@ class HeroIcon(Component):
|
|||
viewbox: Optional[str] = None,
|
||||
attrs: Optional[Dict] = None,
|
||||
) -> Dict:
|
||||
kwargs = IconDefaults(**self.input.kwargs)
|
||||
kwargs = IconDefaults(**self.kwargs)
|
||||
|
||||
if kwargs.variant not in ["outline", "solid"]:
|
||||
raise ValueError(f"Invalid variant: {kwargs.variant}. Must be either 'outline' or 'solid'")
|
||||
|
|
|
@ -252,6 +252,47 @@ class TestComponentLegacyApi:
|
|||
template_2 = _get_component_template(comp)
|
||||
assert template_2._test_id == "123" # type: ignore[union-attr]
|
||||
|
||||
# TODO_v1 - Remove
|
||||
def test_input(self):
|
||||
class TestComponent(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
Variable: <strong>{{ variable }}</strong>
|
||||
{% slot 'my_slot' / %}
|
||||
"""
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert list(self.input.slots.keys()) == ["my_slot"]
|
||||
my_slot = self.input.slots["my_slot"]
|
||||
assert my_slot() == "MY_SLOT"
|
||||
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
def on_render_before(self, context, template):
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert list(self.input.slots.keys()) == ["my_slot"]
|
||||
my_slot = self.input.slots["my_slot"]
|
||||
assert my_slot() == "MY_SLOT"
|
||||
|
||||
rendered = TestComponent.render(
|
||||
kwargs={"variable": "test", "another": 1},
|
||||
args=(123, "str"),
|
||||
slots={"my_slot": "MY_SLOT"},
|
||||
)
|
||||
|
||||
assertHTMLEqual(
|
||||
rendered,
|
||||
"""
|
||||
Variable: <strong data-djc-id-ca1bc3e>test</strong> MY_SLOT
|
||||
""",
|
||||
)
|
||||
|
||||
@djc_test
|
||||
class TestComponent:
|
||||
|
@ -477,7 +518,7 @@ class TestComponentRenderAPI:
|
|||
rendered = SimpleComponent.render()
|
||||
assert rendered == "render_id: ca1bc3e"
|
||||
|
||||
def test_input(self):
|
||||
def test_raw_input(self):
|
||||
class TestComponent(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -486,11 +527,11 @@ class TestComponentRenderAPI:
|
|||
"""
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert list(self.input.slots.keys()) == ["my_slot"]
|
||||
my_slot = self.input.slots["my_slot"]
|
||||
assert self.raw_args == [123, "str"]
|
||||
assert self.raw_kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.context, Context)
|
||||
assert list(self.raw_slots.keys()) == ["my_slot"]
|
||||
my_slot = self.raw_slots["my_slot"]
|
||||
assert my_slot() == "MY_SLOT"
|
||||
|
||||
return {
|
||||
|
@ -498,11 +539,11 @@ class TestComponentRenderAPI:
|
|||
}
|
||||
|
||||
def on_render_before(self, context, template):
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert list(self.input.slots.keys()) == ["my_slot"]
|
||||
my_slot = self.input.slots["my_slot"]
|
||||
assert self.raw_args == [123, "str"]
|
||||
assert self.raw_kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.context, Context)
|
||||
assert list(self.raw_slots.keys()) == ["my_slot"]
|
||||
my_slot = self.raw_slots["my_slot"]
|
||||
assert my_slot() == "MY_SLOT"
|
||||
|
||||
rendered = TestComponent.render(
|
||||
|
|
|
@ -34,7 +34,7 @@ class TestComponentDefaults:
|
|||
"extra": "extra", # Default because `None` was given
|
||||
"fn": self.Defaults.fn, # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
assert self.raw_kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"another": 1, # Default because missing
|
||||
"extra": "extra", # Default because `None` was given
|
||||
|
@ -46,11 +46,11 @@ class TestComponentDefaults:
|
|||
assert [*slots.keys()] == ["my_slot"]
|
||||
assert slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert self.input.args == [123]
|
||||
assert [*self.input.slots.keys()] == ["my_slot"]
|
||||
assert self.input.slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
assert self.raw_args == [123]
|
||||
assert [*self.raw_slots.keys()] == ["my_slot"]
|
||||
assert self.raw_slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert isinstance(self.context, Context)
|
||||
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
|
@ -82,11 +82,11 @@ class TestComponentDefaults:
|
|||
"variable": "test", # User-given
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
assert self.raw_kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert isinstance(self.context, Context)
|
||||
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
|
@ -117,12 +117,12 @@ class TestComponentDefaults:
|
|||
# NOTE: NOT a factory, because it was set as `field(default=...)`
|
||||
"fn": self.Defaults.fn.default, # type: ignore[attr-defined]
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
assert self.raw_kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: NOT a factory, because it was set as `field(default=...)`
|
||||
"fn": self.Defaults.fn.default, # type: ignore[attr-defined]
|
||||
}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert isinstance(self.context, Context)
|
||||
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
|
@ -153,12 +153,12 @@ class TestComponentDefaults:
|
|||
# NOTE: IS a factory, because it was set as `field(default_factory=...)`
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
assert self.raw_kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: IS a factory, because it was set as `field(default_factory=...)`
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert isinstance(self.input.context, Context)
|
||||
assert isinstance(self.context, Context)
|
||||
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
|
|
|
@ -729,7 +729,7 @@ class TestSlot:
|
|||
template = "CAPTURER"
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
seen_slots.append(self.input.slots["my_slot"])
|
||||
seen_slots.append(self.slots["my_slot"])
|
||||
|
||||
MyTopLevelComponent.render()
|
||||
|
||||
|
|
|
@ -1066,7 +1066,7 @@ class TestPassthroughSlots:
|
|||
class OuterComp(Component):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slots": self.input.slots,
|
||||
"slots": self.slots,
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -1117,7 +1117,7 @@ class TestPassthroughSlots:
|
|||
class OuterComp(Component):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slots": self.input.slots,
|
||||
"slots": self.slots,
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue