Skip to content

Commit 0cca20f

Browse files
change render_props arg from NodeList to Component (#139)
1 parent 27d5119 commit 0cca20f

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
2222

2323
- **Internal**: Refactored slot handling logic by moving slot processing from `BirdNode` to `BoundComponent`.
2424
- **Internal**: Simplified component context management in `BirdNode` by offloading context prep to `BoundComponent`.
25+
- **Internal**: Refactored prop rendering in `Params` to take a `Component` instance instead of the raw `NodeList`.
2526

2627
### Removed
2728

src/django_bird/components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def render(self, context: Context):
131131
]
132132
self.params.attrs.extend(data_attrs)
133133

134-
props = self.params.render_props(self.component.nodelist, context)
134+
props = self.params.render_props(self.component, context)
135135
attrs = self.params.render_attrs(context)
136136
slots = self.fill_slots(context)
137137

src/django_bird/params.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
from dataclasses import dataclass
44
from dataclasses import field
5+
from typing import TYPE_CHECKING
56
from typing import Any
67

78
from django import template
8-
from django.template.base import NodeList
99
from django.template.context import Context
1010
from django.utils.safestring import SafeString
1111
from django.utils.safestring import mark_safe
1212

1313
from .templatetags.tags.prop import PropNode
1414

15+
if TYPE_CHECKING:
16+
from django_bird.components import Component
17+
1518

1619
@dataclass
1720
class Params:
@@ -23,13 +26,13 @@ def with_attrs(cls, attrs: list[Param] | None) -> Params:
2326
"""Create a Params instance with a copy of the provided attrs."""
2427
return cls(attrs=attrs.copy() if attrs is not None else [], props=[])
2528

26-
def render_props(self, nodelist: NodeList | None, context: Context):
27-
if nodelist is None:
29+
def render_props(self, component: Component, context: Context):
30+
if component.nodelist is None:
2831
return
2932

3033
attrs_to_remove = set()
3134

32-
for node in nodelist:
35+
for node in component.nodelist:
3336
if not isinstance(node, PropNode):
3437
continue
3538

tests/test_params.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import pytest
44

5+
from django_bird.components import Component
56
from django_bird.params import Param
67
from django_bird.params import Params
78
from django_bird.params import Value
8-
from django_bird.templatetags.tags.prop import PropNode
9+
10+
from .utils import TestComponent
911

1012

1113
class TestValue:
@@ -141,39 +143,39 @@ def test_from_bit(self, bit, expected):
141143

142144
class TestParams:
143145
@pytest.mark.parametrize(
144-
"params,nodelist,context,expected_props,expected_attrs",
146+
"params,test_component,context,expected_props,expected_attrs",
145147
[
146148
(
147149
Params(attrs=[Param(name="class", value=Value(None))]),
148-
[PropNode(name="class", default="btn", attrs=[])],
150+
TestComponent(name="test", content="{% bird:prop class='btn' %}"),
149151
{},
150152
{"class": "btn"},
151153
[],
152154
),
153155
(
154156
Params(attrs=[Param(name="class", value=Value("btn", quoted=False))]),
155-
[PropNode(name="class", default=None, attrs=[])],
157+
TestComponent(name="test", content="{% bird:prop class %}"),
156158
{},
157159
{"class": "btn"},
158160
[],
159161
),
160162
(
161163
Params(attrs=[Param(name="class", value=Value("btn", quoted=False))]),
162-
None,
164+
TestComponent(name="test", content=""),
165+
{},
163166
{},
164-
None,
165167
[Param(name="class", value=Value("btn", quoted=False))],
166168
),
167169
(
168170
Params(attrs=[Param(name="class", value=Value("static", quoted=True))]),
169-
[PropNode(name="class", default=None, attrs=[])],
171+
TestComponent(name="test", content="{% bird:prop class %}"),
170172
{"static": "dynamic"},
171173
{"class": "static"},
172174
[],
173175
),
174176
(
175177
Params(attrs=[Param(name="class", value=Value("var", quoted=False))]),
176-
[PropNode(name="class", default=None, attrs=[])],
178+
TestComponent(name="test", content="{% bird:prop class %}"),
177179
{"var": "dynamic"},
178180
{"class": "dynamic"},
179181
[],
@@ -182,7 +184,7 @@ class TestParams:
182184
Params(
183185
attrs=[Param(name="class", value=Value("undefined", quoted=False))]
184186
),
185-
[PropNode(name="class", default=None, attrs=[])],
187+
TestComponent(name="test", content="{% bird:prop class %}"),
186188
{},
187189
{"class": "undefined"},
188190
[],
@@ -191,17 +193,25 @@ class TestParams:
191193
Params(
192194
attrs=[Param(name="class", value=Value("user.name", quoted=False))]
193195
),
194-
[PropNode(name="class", default=None, attrs=[])],
196+
TestComponent(name="test", content="{% bird:prop class %}"),
195197
{},
196198
{"class": "user.name"},
197199
[],
198200
),
199201
],
200202
)
201203
def test_render_props(
202-
self, params, nodelist, context, expected_props, expected_attrs
204+
self,
205+
params,
206+
test_component,
207+
context,
208+
expected_props,
209+
expected_attrs,
210+
templates_dir,
203211
):
204-
assert params.render_props(nodelist, context) == expected_props
212+
test_component.create(templates_dir)
213+
component = Component.from_name(test_component.name)
214+
assert params.render_props(component, context) == expected_props
205215
assert params.attrs == expected_attrs
206216

207217
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)