mirror of
https://github.com/django-components/django-components.git
synced 2025-08-15 11:40:14 +00:00
chore: Push dev to master to release v0.110 (#767)
* feat: skeleton of dependency manager backend (#688) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: selectolax update and tests cleanup (#702) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: move release notes to own file (#704) * chore: merge changes from master (#705) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yassin Rakha <yaso2go@gmail.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se> fix for nested slots (#698) (#699) * refactor: remove joint {% component_dependencies %} tag (#706) Co-authored-by: Emil Stenström <emil@emilstenstrom.se> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: split up utils file and move utils to util dir (#707) * docs: Move docs inside src/ to allow imports in python scripts (#708) * refactor: Docs prep 1 (#715) * refactor: Document template tags (#716) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: pass slot fills in template via slots param (#719) * chore: Merge master to dev (#729) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yassin Rakha <yaso2go@gmail.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se> Co-authored-by: Tom Larsen <larsent@gmail.com> fix for nested slots (#698) (#699) * fix: Do not raise error if multiple slots with same name are flagged as default (#727) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: tag formatter - allow fwd slash in end tag (#730) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: Use lowercase names for registry settings (#731) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * docs: add docstrings (#732) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * feat: define settings as a data class for type hints, intellisense, and docs (#733) * refactor: fix reload-on-change logic, expose autodiscover's dirs-getting logic, rename settings (#734) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * docs: document settings (#743) * docs: document settings * refactor: fix linter errors * feat: passthrough slots and more (#758) * feat: passthrough slots and more * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: remove ComponentSlotContext.slots * refactor: update comment * docs: update changelog * refactor: update docstrings * refactor: document and test-cover more changes * refactor: revert fill without name * docs: update README --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * fix: apostrophes in tags (#765) * refactor: fix merge error - duplicate code --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se>
This commit is contained in:
parent
9f891453d5
commit
5fd45ab424
97 changed files with 8727 additions and 3011 deletions
|
@ -7,14 +7,82 @@ from django_components.component_registry import all_registries
|
|||
|
||||
class DynamicComponent(Component):
|
||||
"""
|
||||
Dynamic component - This component takes inputs and renders the outputs depending on the
|
||||
`is` and `registry` arguments.
|
||||
This component is given a registered name or a reference to another component,
|
||||
and behaves as if the other component was in its place.
|
||||
|
||||
- `is` - required - The component class or registered name of the component that will be
|
||||
rendered in this place.
|
||||
The args, kwargs, and slot fills are all passed down to the underlying component.
|
||||
|
||||
- `registry` - optional - Specify the registry to search for the registered name. If omitted,
|
||||
all registries are searched.
|
||||
Args:
|
||||
is (str | Type[Component]): Component that should be rendered. Either a registered name of a component,
|
||||
or a [Component](../api#django_components.Component) class directly. Required.
|
||||
registry (ComponentRegistry, optional): Specify the [registry](../api#django_components.ComponentRegistry)\
|
||||
to search for the registered name. If omitted, all registries are searched until the first match.
|
||||
*args: Additional data passed to the component.
|
||||
**kwargs: Additional data passed to the component.
|
||||
|
||||
**Slots:**
|
||||
|
||||
* Any slots, depending on the actual component.
|
||||
|
||||
**Examples:**
|
||||
|
||||
Django
|
||||
```django
|
||||
{% component "dynamic" is=table_comp data=table_data headers=table_headers %}
|
||||
{% fill "pagination" %}
|
||||
{% component "pagination" / %}
|
||||
{% endfill %}
|
||||
{% endcomponent %}
|
||||
```
|
||||
|
||||
Python
|
||||
```py
|
||||
from django_components import DynamicComponent
|
||||
|
||||
DynamicComponent.render(
|
||||
kwargs={
|
||||
"is": table_comp,
|
||||
"data": table_data,
|
||||
"headers": table_headers,
|
||||
},
|
||||
slots={
|
||||
"pagination": PaginationComponent.render(
|
||||
render_dependencies=False,
|
||||
),
|
||||
},
|
||||
)
|
||||
```
|
||||
|
||||
# Use cases
|
||||
|
||||
Dynamic components are suitable if you are writing something like a form component. You may design
|
||||
it such that users give you a list of input types, and you render components depending on the input types.
|
||||
|
||||
While you could handle this with a series of if / else statements, that's not an extensible approach.
|
||||
Instead, you can use the dynamic component in place of normal components.
|
||||
|
||||
# Component name
|
||||
|
||||
By default, the dynamic component is registered under the name `"dynamic"`. In case of a conflict,
|
||||
you can set the
|
||||
[`COMPONENTS.dynamic_component_name`](../settings#django_components.app_settings.ComponentsSettings.dynamic_component_name)
|
||||
setting to change the name used for the dynamic components.
|
||||
|
||||
```py
|
||||
# settings.py
|
||||
COMPONENTS = ComponentsSettings(
|
||||
dynamic_component_name="my_dynamic",
|
||||
)
|
||||
```
|
||||
|
||||
After which you will be able to use the dynamic component with the new name:
|
||||
```django
|
||||
{% component "my_dynamic" is=table_comp data=table_data headers=table_headers %}
|
||||
{% fill "pagination" %}
|
||||
{% component "pagination" / %}
|
||||
{% endfill %}
|
||||
{% endcomponent %}
|
||||
```
|
||||
"""
|
||||
|
||||
_is_dynamic_component = True
|
||||
|
@ -32,27 +100,30 @@ class DynamicComponent(Component):
|
|||
|
||||
comp_class = self._resolve_component(comp_name_or_class, registry)
|
||||
|
||||
# NOTE: Slots are passed at component instantiation
|
||||
comp = comp_class(
|
||||
registered_name=self.registered_name,
|
||||
component_id=self.component_id,
|
||||
outer_context=self.outer_context,
|
||||
fill_content=self.fill_content,
|
||||
registry=self.registry,
|
||||
)
|
||||
output = comp.render(
|
||||
context=self.input.context,
|
||||
args=args,
|
||||
kwargs=kwargs,
|
||||
escape_slots_content=self.input.escape_slots_content,
|
||||
slots=self.input.slots,
|
||||
# NOTE: Since we're accessing slots as `self.input.slots`, the content of slot functions
|
||||
# was already escaped (if set so).
|
||||
escape_slots_content=False,
|
||||
type=self.input.type,
|
||||
render_dependencies=self.input.render_dependencies,
|
||||
)
|
||||
|
||||
return {
|
||||
"output": output,
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
{{ output }}
|
||||
"""
|
||||
template: types.django_html = """{{ output }}"""
|
||||
|
||||
def _resolve_component(
|
||||
self,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue