Skip to content

Commit

Permalink
[Feature] FormActionType
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Mar 16, 2023
1 parent c29f758 commit 1e16d02
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 53 deletions.
1 change: 1 addition & 0 deletions docs/reference/actions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ The following action types are natively available in the bundle:
- Common types
- [Link](types/link.md)
- [Button](types/button.md)
- [Form](types/form.md)
- Base types
- [Action](types/action.md)
31 changes: 0 additions & 31 deletions docs/reference/actions/types/_link_options.md

This file was deleted.

56 changes: 51 additions & 5 deletions docs/reference/actions/types/button.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
{% set option_display_icon_default = 'false' %}

# ButtonActionType

The [:material-github: ButtonActionType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Action/Type/ButtonActionType.php) represents an action displayed as a button.
Uses [LinkActionType](./link.md) as parent.

## Options

This action type has no additional options.
### `href`

**type**: `string` or `callable` **default**: `'#'`

Sets the value that will be used as a link `href` attribute (see [href attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-href)).
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\LinkActionType;

$builder
->addAction('show', LinkActionType::class, [
'href' => $this->urlGenerator->generate('category_show', [
'id' => $category->getId(),
]),
])
;
```

### `target`

**type**: `string` or `callable` **default**: `'_self'`

Sets the value that will be used as an anchor `target` attribute (see [target attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target)).
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

### `link_attr`

**type**: `array` **default**: `[]`

If you want to add extra attributes to an HTML representation of the button's link, you can use the link_attr option.
It's an associative array with HTML attributes as keys.

### `icon_attr`

**type**: `array` **default**: `[]`

If you want to add extra attributes to an HTML representation of the button's icon, you can use the icon_attr option.
It's an associative array with HTML attributes as keys.

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\ButtonActionType;

$builder
->addAction('remove', ButtonActionType::class, [
'icon_attr' => [
'class' => 'fa fa-trash',
],
])
;
```

## Inherited options

{% include-markdown "_link_options.md" heading-offset=2 %}
{% include-markdown "_action_options.md" heading-offset=2 %}
67 changes: 67 additions & 0 deletions docs/reference/actions/types/form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# FormActionType

The [:material-github: FormActionType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Action/Type/FormActionType.php) represents an action displayed as a button wrapped in form.

## Options

### `action`

**type**: `string` or `callable` **default**: `'#'`

Sets the value that will be used as a form's `action` attribute.

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\FormActionType;

$builder
->addAction('send', FormActionType::class, [
'action' => $this->urlGenerator->generate('sms_send'),
])
;
```

### `method`

**type**: `string` or `callable` **default**: `'GET'`

Sets the value that will be used as a form's `method` attribute.

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\FormActionType;

$builder
->addAction('send', FormActionType::class, [
'method' => 'POST',
])
;
```

### `button_attr`

**type**: `array` **default**: `[]`

If you want to add extra attributes to an HTML representation of the form's submit button, you can use the button_attr option.
It's an associative array with HTML attributes as keys.

### `icon_attr`

**type**: `array` **default**: `[]`

If you want to add extra attributes to an HTML representation of the button's icon, you can use the icon_attr option.
It's an associative array with HTML attributes as keys.

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\FormActionType;

$builder
->addAction('send', FormActionType::class, [
'icon_attr' => [
'class' => 'fa fa-envelope',
],
])
;
```

## Inherited options

{% include-markdown "_action_options.md" heading-offset=2 %}
31 changes: 28 additions & 3 deletions docs/reference/actions/types/link.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
{% set option_display_icon_default = 'true' %}

# LinkActionType

The [:material-github: LinkActionType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Action/Type/LinkActionType.php) represents an action displayed as a link.
Uses [ActionType](./action.md) as parent.

## Options

{% include-markdown "_link_options.md" heading-offset=2 %}
### `href`

**type**: `string` or `callable` **default**: `'#'`

Sets the value that will be used as a link `href` attribute (see [href attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-href)).

```php
use Kreyu\Bundle\DataTableBundle\Action\Type\LinkActionType;

$builder
->addAction('back', LinkActionType::class, [
'href' => $this->urlGenerator->generate('category_index'),
])
;
```

### `target`

**type**: `string` or `callable` **default**: `'_self'`

Sets the value that will be used as an anchor `target` attribute (see [target attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target)).
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

### `display_icon`

**type**: `bool` **default**: `true`

If this value is true, an icon will be visible next to the link label.

## Inherited options

Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ nav:
- Common types:
- Link: 'reference/actions/types/link.md'
- Button: 'reference/actions/types/button.md'
- Form: 'reference/actions/types/form.md'
- Base types:
- Action: 'reference/actions/types/action.md'
- 'Creating custom action type': 'reference/actions/creating_custom_action_type.md'
Expand Down
4 changes: 2 additions & 2 deletions src/Action/Type/ActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function configureOptions(OptionsResolver $resolver): void
'block_prefix' => null,
'attr' => [],
])
->setAllowedTypes('label', ['null', 'string', TranslatableMessage::class])
->setAllowedTypes('label', ['null', 'bool', 'string', 'callable', TranslatableMessage::class])
->setAllowedTypes('label_translation_parameters', ['array'])
->setAllowedTypes('translation_domain', ['null', 'bool', 'string'])
->setAllowedTypes('block_name', ['null', 'string'])
Expand Down Expand Up @@ -102,7 +102,7 @@ private function resolveCallableOptions(array $options, ActionInterface $action)
}

if ($option instanceof \Closure) {
$option = $option($options['data'], $action, $options);
$option = $option($action->getData(), $action, $options);
}

$options[$key] = $option;
Expand Down
20 changes: 12 additions & 8 deletions src/Action/Type/ButtonActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ class ButtonActionType extends AbstractActionType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'display_icon' => false,
]);
}

public function getParent(): ?string
{
return LinkActionType::class;
$resolver
->setDefaults([
'href' => '#',
'target' => '_self',
'link_attr' => [],
'icon_attr' => [],
])
->setAllowedTypes('href', ['string', 'callable'])
->setAllowedTypes('target', ['string', 'callable'])
->setAllowedTypes('link_attr', ['array', 'callable'])
->setAllowedTypes('icon_attr', ['array', 'callable'])
;
}
}
26 changes: 26 additions & 0 deletions src/Action/Type/FormActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Action\Type;

use Symfony\Component\OptionsResolver\OptionsResolver;

class FormActionType extends AbstractActionType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'method' => 'GET',
'action' => '#',
'button_attr' => [],
'icon_attr' => [],
])
->setAllowedTypes('method', ['string', 'callable'])
->setAllowedTypes('action', ['string', 'callable'])
->setAllowedTypes('button_attr', ['array', 'callable'])
->setAllowedTypes('icon_attr', ['array', 'callable'])
;
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kreyu\Bundle\DataTableBundle\Action\ActionRegistryInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\ButtonActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\FormActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\LinkActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeFactory;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeFactoryInterface;
Expand Down Expand Up @@ -54,4 +55,9 @@
->set('kreyu_data_table.action.type.button', ButtonActionType::class)
->tag('kreyu_data_table.action.type')
;

$services
->set('kreyu_data_table.action.type.form', FormActionType::class)
->tag('kreyu_data_table.action.type')
;
};
22 changes: 21 additions & 1 deletion src/Resources/views/themes/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,30 @@

{% block kreyu_data_table_action_button %}
<button {{ block('attributes') }}>
{{- block('kreyu_data_table_action_link') -}}
<a {% with { attr: { href, target }|merge(link_attr) } %}{{ block('attributes') }}{% endwith %}>
{% if icon_attr %}<i {% with { attr: icon_attr } %}{{ block('attributes') }}{% endwith %}></i>{% endif %}
{{- label|trans(label_translation_parameters, translation_domain) -}}
</a>
</button>
{% endblock %}

{% block kreyu_data_table_action_form %}
{% set non_standard_method = false %}

{% if method|upper not in ['GET', 'POST'] %}
{% set non_standard_method = method %}
{% set method = 'POST' %}
{% endif %}

<form {% with { attr: { action, method }|merge(attr) } %}{{ block('attributes') }}{% endwith %}>
{% if non_standard_method %}<input type="hidden" name="_method" value="{{ non_standard_method }}"/>{% endif %}
<button {% with { attr: { type: 'submit' }|merge(button_attr) } %}{{ block('attributes') }}{% endwith %}>
{% if icon_attr %}<i {% with { attr: icon_attr } %}{{ block('attributes') }}{% endwith %}></i>{% endif %}
{{- label|trans(label_translation_parameters, translation_domain) -}}
</button>
</form>
{% endblock %}

{% block sort_arrow_asc %}↑{% endblock %}

{% block sort_arrow_desc %}↓{% endblock %}
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/views/themes/bootstrap_5.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@
{% endwith %}
{% endblock %}

{% block kreyu_data_table_action_button %}
{% with { attr: { class: 'btn btn-primary' }|merge(attr|default({})) } %}
{{- block('kreyu_data_table_action_link') -}}
{% block kreyu_data_table_action_form %}
{% with { button_attr: { class: 'btn btn-primary' }|merge(button_attr|default({})) } %}
{{ parent() }}
{% endwith %}
{% endblock %}

Expand Down
6 changes: 6 additions & 0 deletions src/Resources/views/themes/tabler.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@
{{ parent() }}
{% endwith %}
{% endblock %}

{% block kreyu_data_table_action_button %}
{% with { attr: { class: 'btn btn-primary' ~ (icon_attr and not label ? ' btn-icon') }|merge(attr|default({})) } %}
{{ parent() }}
{% endwith %}
{% endblock %}

0 comments on commit 1e16d02

Please sign in to comment.