# Actions
Components can also trigger methods from the templates by listening to any valid event type. The most common events would be `click`, `input`, `keydown`, `keyup`, and `mouseenter`, but [MDN has a list of all of the browser event types available](https://developer.mozilla.org/en-US/docs/Web/Events).
## Events
An example action to call the `clear_name` method on the component.
```html
Hello {{ name|title }}
```
```python
# clear_name.py
from django_unicorn.components import UnicornView
class ClearNameView(UnicornView):
name = "World"
def clear_name(self):
self.name = ""
```
When the button is clicked, the `name` property will get set to an empty string. Then, the component will intelligently re-render itself and the text input will update to match the property on the component.
```{tip}
Instance methods without arguments can be called from the template with or without parenthesis.
```
## Passing arguments
Actions can also pass basic Python types to the backend component.
```html
Hello {{ name|title }} 👋
```
```python
# passing_args.py
from django_unicorn.components import UnicornView
class PassingArgsView(UnicornView):
name = "World"
def set(self, name="Universe"):
self.name = name
```
### Argument types
Most basic Python types, including `string`, `int`, `float`, `bool`, `list`, `tuple`, `dictionary`, and `set`, are supported by default.
```html
```
#### Coerced types
Arguments with the types of `datetime`, `date`, `time`, `timedelta`, and `UUID` can be coerced by using a type annotation in the action method.
```{note}
[Django's `dateparse` methods](https://docs.djangoproject.com/en/stable/ref/utils/#module-django.utils.dateparse) are used to convert strings to the date-related type.
```
```{note}
Both integer and float epochs can also be coerced into `datetime` or `date` objects.
```
```python
# argument_type_hints.py
from django_unicorn.components import UnicornView
from datetime import date, datetime
from uuid import UUID
class ArgumentTypeHintsView(UnicornView):
def is_datetime(self, obj: datetime):
assert type(obj) is datetime
def is_uuid(self, obj: UUID):
assert type(obj) is UUID
def is_date(self, obj: date = None):
assert type(obj) is date
```
```html
```
#### Django models
Django models can be instantiated as an argument or with a `pk` kwarg and a type annotation.
```python
# argument_model.py
from django_unicorn.components import UnicornView
from django.contrib.auth.models import User
class ArgumentModelView(UnicornView):
def is_user_via_arg(self, obj: User):
assert type(obj) is User
def is_user_via_kwarg(self, pk: User=None):
assert type(obj) is User
```
```html
```
#### Custom types
Custom objects can also be used as a type annotation and `Unicorn` will attempt to instantiate the object with the value that is passed in as the argument.
```python
# argument_custom_type.py
from django_unicorn.components import UnicornView
class CustomType:
def __init__(self, custom_type_id: int):
self.custom_type_id = custom_type_id
class ArgumentCustomTypeView(UnicornView):
def is_custom_type(self, obj: CustomType):
assert type(obj) is CustomType
```
```html
```
#### Enums
Enums themselves cannot be passed as an argument, but the enum _value_ can be since that is a Python primitive. Use the enum as a type annotation to coerce the value into the specified enum.
```python
# enum.py
from django_unicorn.components import UnicornView
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
PURPLE = 4
class EnumView(UnicornView):
color = Color.RED
purple_color = Color.PURPLE
def set_color(self, color: Color):
self.color = color
```
```html
Color: {{ color }}
Color int: {{ color.value }}
```
## Set shortcut
Actions can also set properties without requiring an explicit method.
```html
Hello {{ name|title }} 👋
```
```python
# set_shortcut.py
from django_unicorn.components import UnicornView
class SetShortcutView(UnicornView):
name = "World"
```
## Modifiers
Similar to models, actions also have modifiers which change how the method gets called.
### prevent
Prevents the default action the browser would use for that element. The same as calling `preventDefault`.
:::{code} html
:force: true
:::
### stop
Stops the event from bubbling up the event chain. The same as calling `stopPropagation`.
:::{code} html
:force: true
:::
### discard
Discards any model updates from being saved before calling the specified method on the view. Useful for a cancel button.
:::{code} html
:force: true
:::
```python
# discard_modifier.py
from django_unicorn.components import UnicornView
class DiscardModifierView(UnicornView):
name = None
def cancel(self):
pass
```
### debounce
Waits the specified time in milliseconds before calling the specified method.
:::{code} html
:force: true
:::
## Special arguments
### $event
A reference to the event that triggered the action.
```html
Update
```
### $returnValue
A reference to the last return value from an action method.
```html
Update
```
### $parent
A reference to the parent of the current component.
## Special methods
### $refresh
Refresh and re-render the component from its current state.
```html
```
### $reset
Revert the component to its original state.
```html
```
### $toggle
Toggle a field on the component. Can only be used for fields that are booleans.
```html
```
```{tip}
Multiple fields can be toggled at the same time by passing in multiple fields at a time: `unicorn:click="$toggle('check', 'another_check', 'a_third_check')"`. Nested properties are also supported: `unicorn:click="$toggle('nested.check')"`.
```
### $validate
Validates the component.
```html
```
## Calling methods
Sometimes you need to trigger a method on a component from regular JavaScript. That is possible with `Unicorn.call()`. The first argument is the name (or key) of the component and the second argument is the name of the method to call.
```html
{% unicorn 'hello-world' %}
```
```html
{% unicorn 'hello-world' key='hello-universe' %}
```
Passing arguments to the method call is also supported.
```html
{% unicorn 'hello-world' %}
```
## Return values
To retrieve the last action method's return value, use `Unicorn.getReturnValue()`.
```html
{% unicorn 'hello-world' %}
```