mirror of
https://github.com/django-components/django-components.git
synced 2025-10-14 15:59:00 +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
|
@ -80,7 +80,7 @@ and so `selected_items` will be set to `[1, 2, 3]`.
|
|||
|
||||
This is **NOT recommended**, because:
|
||||
|
||||
- The defaults will NOT be applied to inputs when using [`self.input`](../../../reference/api/#django_components.Component.input) property.
|
||||
- The defaults will NOT be applied to inputs when using [`self.raw_kwargs`](../../../reference/api/#django_components.Component.raw_kwargs) property.
|
||||
- The defaults will NOT be applied when a field is given but set to `None`.
|
||||
|
||||
Instead, define the defaults in the [`Defaults`](../../../reference/api/#django_components.Component.Defaults) class.
|
||||
|
|
|
@ -241,8 +241,24 @@ class ProfileCard(Component):
|
|||
}
|
||||
```
|
||||
|
||||
<!-- TODO_v1 - Remove -->
|
||||
|
||||
### `input` property (low-level)
|
||||
|
||||
!!! warning
|
||||
|
||||
The `input` property is deprecated and will be removed in v1.
|
||||
|
||||
Instead, use properties defined on the
|
||||
[`Component`](../../../reference/api/#django_components.Component) class
|
||||
directly like
|
||||
[`self.context`](../../../reference/api/#django_components.Component.context).
|
||||
|
||||
To access the unmodified inputs, use
|
||||
[`self.raw_args`](../../../reference/api/#django_components.Component.raw_args),
|
||||
[`self.raw_kwargs`](../../../reference/api/#django_components.Component.raw_kwargs),
|
||||
and [`self.raw_slots`](../../../reference/api/#django_components.Component.raw_slots) properties.
|
||||
|
||||
The previous two approaches allow you to access only the most important inputs.
|
||||
|
||||
There are additional settings that may be passed to components.
|
||||
|
@ -260,8 +276,6 @@ This includes:
|
|||
- [`input.type`](../../../reference/api/#django_components.ComponentInput.type) - The type of the component (document, fragment)
|
||||
- [`input.render_dependencies`](../../../reference/api/#django_components.ComponentInput.render_dependencies) - Whether to render dependencies (CSS, JS)
|
||||
|
||||
For more details, see [Component inputs](../render_api/#other-inputs).
|
||||
|
||||
```python
|
||||
class ProfileCard(Component):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
|
@ -336,7 +350,7 @@ class ProfileCard(Component):
|
|||
|
||||
This is **NOT recommended**, because:
|
||||
|
||||
- The defaults will NOT be applied to inputs when using [`self.input`](../../../reference/api/#django_components.Component.input) property.
|
||||
- The defaults will NOT be applied to inputs when using [`self.raw_kwargs`](../../../reference/api/#django_components.Component.raw_kwargs) property.
|
||||
- The defaults will NOT be applied when a field is given but set to `None`.
|
||||
|
||||
Instead, define the defaults in the [`Defaults`](../../../reference/api/#django_components.Component.Defaults) class.
|
||||
|
@ -348,8 +362,10 @@ All three data methods have access to the Component's [Render API](../render_api
|
|||
- [`self.args`](../render_api/#args) - The positional arguments for the current render call
|
||||
- [`self.kwargs`](../render_api/#kwargs) - The keyword arguments for the current render call
|
||||
- [`self.slots`](../render_api/#slots) - The slots for the current render call
|
||||
- [`self.raw_args`](../render_api/#args) - Unmodified positional arguments for the current render call
|
||||
- [`self.raw_kwargs`](../render_api/#kwargs) - Unmodified keyword arguments for the current render call
|
||||
- [`self.raw_slots`](../render_api/#slots) - Unmodified slots for the current render call
|
||||
- [`self.context`](../render_api/#context) - The context for the current render call
|
||||
- [`self.input`](../render_api/#other-inputs) - All the component inputs
|
||||
- [`self.id`](../render_api/#component-id) - The unique ID for the current render call
|
||||
- [`self.request`](../render_api/#request-and-context-processors) - The request object
|
||||
- [`self.context_processors_data`](../render_api/#request-and-context-processors) - Data from Django's context processors
|
||||
|
@ -357,6 +373,7 @@ All three data methods have access to the Component's [Render API](../render_api
|
|||
- [`self.registry`](../render_api/#template-tag-metadata) - The [`ComponentRegistry`](../../../reference/api/#django_components.ComponentRegistry) instance
|
||||
- [`self.registered_name`](../render_api/#template-tag-metadata) - The name under which the component was registered
|
||||
- [`self.outer_context`](../render_api/#template-tag-metadata) - The context outside of the [`{% component %}`](../../../reference/template_tags#component) tag
|
||||
- `self.deps_strategy` - The strategy for rendering dependencies
|
||||
|
||||
## Type hints
|
||||
|
||||
|
@ -399,7 +416,11 @@ class Button(Component):
|
|||
|
||||
!!! note
|
||||
|
||||
The data available via [`self.input`](../../../reference/api/#django_components.Component.input) property is NOT typed.
|
||||
To access "untyped" inputs, use [`self.raw_args`](../../../reference/api/#django_components.Component.raw_args),
|
||||
[`self.raw_kwargs`](../../../reference/api/#django_components.Component.raw_kwargs),
|
||||
and [`self.raw_slots`](../../../reference/api/#django_components.Component.raw_slots) properties.
|
||||
|
||||
These are plain lists and dictionaries, even when you added typing to your component.
|
||||
|
||||
### Typing data
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class Table(Component):
|
|||
assert self.args == [123, "str"]
|
||||
assert self.kwargs == {"variable": "test", "another": 1}
|
||||
footer_slot = self.slots["footer"]
|
||||
some_var = self.input.context["some_var"]
|
||||
some_var = self.context["some_var"]
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
# Access the request object and Django's context processors, if available
|
||||
|
@ -47,8 +47,11 @@ The Render API includes:
|
|||
- [`self.args`](../render_api/#args) - The positional arguments for the current render call
|
||||
- [`self.kwargs`](../render_api/#kwargs) - The keyword arguments for the current render call
|
||||
- [`self.slots`](../render_api/#slots) - The slots for the current render call
|
||||
- [`self.raw_args`](../render_api/#args) - Unmodified positional arguments for the current render call
|
||||
- [`self.raw_kwargs`](../render_api/#kwargs) - Unmodified keyword arguments for the current render call
|
||||
- [`self.raw_slots`](../render_api/#slots) - Unmodified slots for the current render call
|
||||
- [`self.context`](../render_api/#context) - The context for the current render call
|
||||
- [`self.input`](../render_api/#other-inputs) - All the component inputs
|
||||
- [`self.deps_strategy`](../../advanced/rendering_js_css#dependencies-strategies) - The strategy for rendering dependencies
|
||||
|
||||
- Request-related:
|
||||
- [`self.request`](../render_api/#request-and-context-processors) - The request object (if available)
|
||||
|
@ -78,6 +81,9 @@ then the [`Component.args`](../../../reference/api/#django_components.Component.
|
|||
|
||||
Otherwise, `args` will be a plain list.
|
||||
|
||||
Use [`self.raw_args`](../../../reference/api/#django_components.Component.raw_args)
|
||||
to access the positional arguments as a plain list irrespective of [`Component.Args`](../../../reference/api/#django_components.Component.Args).
|
||||
|
||||
**Example:**
|
||||
|
||||
With `Args` class:
|
||||
|
@ -120,6 +126,9 @@ then the [`Component.kwargs`](../../../reference/api/#django_components.Componen
|
|||
|
||||
Otherwise, `kwargs` will be a plain dictionary.
|
||||
|
||||
Use [`self.raw_kwargs`](../../../reference/api/#django_components.Component.raw_kwargs)
|
||||
to access the keyword arguments as a plain dictionary irrespective of [`Component.Kwargs`](../../../reference/api/#django_components.Component.Kwargs).
|
||||
|
||||
**Example:**
|
||||
|
||||
With `Kwargs` class:
|
||||
|
@ -162,6 +171,9 @@ then the [`Component.slots`](../../../reference/api/#django_components.Component
|
|||
|
||||
Otherwise, `slots` will be a plain dictionary.
|
||||
|
||||
Use [`self.raw_slots`](../../../reference/api/#django_components.Component.raw_slots)
|
||||
to access the slots as a plain dictionary irrespective of [`Component.Slots`](../../../reference/api/#django_components.Component.Slots).
|
||||
|
||||
**Example:**
|
||||
|
||||
With `Slots` class:
|
||||
|
@ -217,44 +229,6 @@ Whether the context variables defined in `context` are available to the template
|
|||
- In `"isolated"` context behavior mode, the template will NOT have access to this context,
|
||||
and data MUST be passed via component's args and kwargs.
|
||||
|
||||
### Other inputs
|
||||
|
||||
You can access the most important inputs via [`self.args`](../render_api/#args),
|
||||
[`self.kwargs`](../render_api/#kwargs),
|
||||
and [`self.slots`](../render_api/#slots) properties.
|
||||
|
||||
There are additional settings that may be passed to components.
|
||||
If you need to access these, you can use [`self.input`](../../../reference/api/#django_components.Component.input) property
|
||||
for a low-level access to all the inputs passed to the component.
|
||||
|
||||
[`self.input`](../../../reference/api/#django_components.Component.input) ([`ComponentInput`](../../../reference/api/#django_components.ComponentInput)) has the mostly the same fields as the input to [`Component.render()`](../../../reference/api/#django_components.Component.render). This includes:
|
||||
|
||||
- `args` - List of positional arguments
|
||||
- `kwargs` - Dictionary of keyword arguments
|
||||
- `slots` - Dictionary of slots. Values are normalized to [`Slot`](../../../reference/api/#django_components.Slot) instances
|
||||
- `context` - [`Context`](https://docs.djangoproject.com/en/5.2/ref/templates/api/#django.template.Context) object that should be used to render the component
|
||||
- And other kwargs passed to [`Component.render()`](../../../reference/api/#django_components.Component.render) like `type` and `render_dependencies`
|
||||
|
||||
For example, you can use [`self.input.args`](../../../reference/api/#django_components.ComponentInput.args)
|
||||
and [`self.input.kwargs`](../../../reference/api/#django_components.ComponentInput.kwargs)
|
||||
to access the positional and keyword arguments passed to [`Component.render()`](../../../reference/api/#django_components.Component.render).
|
||||
|
||||
```python
|
||||
class Table(Component):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
# Access component's inputs, slots and context
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
footer_slot = self.input.slots["footer"]
|
||||
some_var = self.input.context["some_var"]
|
||||
|
||||
rendered = TestComponent.render(
|
||||
kwargs={"variable": "test", "another": 1},
|
||||
args=(123, "str"),
|
||||
slots={"footer": "MY_SLOT"},
|
||||
)
|
||||
```
|
||||
|
||||
## Component ID
|
||||
|
||||
Component ID (or render ID) is a unique identifier for the current render call.
|
||||
|
|
|
@ -230,7 +230,7 @@ class Table(Component):
|
|||
assert self.args == [123, "str"]
|
||||
assert self.kwargs == {"variable": "test", "another": 1}
|
||||
footer_slot = self.slots["footer"]
|
||||
some_var = self.input.context["some_var"]
|
||||
some_var = self.context["some_var"]
|
||||
|
||||
# Access the request object and Django's context processors, if available
|
||||
assert self.request.GET == {"query": "something"}
|
||||
|
|
|
@ -67,7 +67,7 @@ If you insert this tag multiple times, ALL JS scripts will be duplicately insert
|
|||
|
||||
|
||||
|
||||
<a href="https://github.com/django-components/django-components/tree/master/src/django_components/templatetags/component_tags.py#L3685" target="_blank">See source code</a>
|
||||
<a href="https://github.com/django-components/django-components/tree/master/src/django_components/templatetags/component_tags.py#L3796" target="_blank">See source code</a>
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue