Skip to content

Commit

Permalink
Remove dropdown controller, add Bootstrap data parameters to use its …
Browse files Browse the repository at this point in the history
…own dropdown implementation, improve dropdown attr
  • Loading branch information
Kreyu committed Jan 12, 2025
1 parent 37eddaf commit 753e28b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
15 changes: 0 additions & 15 deletions assets/controllers/dropdown.js

This file was deleted.

5 changes: 0 additions & 5 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
"main": "controllers/state.js",
"fetch": "eager",
"enabled": true
},
"dropdown": {
"main": "controllers/dropdown.js",
"fetch": "eager",
"enabled": true
}
},
"importmap": {
Expand Down
16 changes: 14 additions & 2 deletions src/Action/Type/Dropdown/DropdownActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@
use Kreyu\Bundle\DataTableBundle\Action\ActionInterface;
use Kreyu\Bundle\DataTableBundle\Action\ActionView;
use Kreyu\Bundle\DataTableBundle\Action\Type\AbstractActionType;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Kreyu\Bundle\DataTableBundle\Exception\UnexpectedTypeException;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DropdownActionType extends AbstractActionType
{
public function buildView(ActionView $view, ActionInterface $action, array $options): void
{
$itemActions = [];
/** @var ActionBuilderInterface $itemActionBuilder */

if (is_callable($options['actions']) && $view->parent instanceof ColumnValueView) {
$options['actions'] = $options['actions']($view->parent->value);
}

foreach ($options['actions'] as $itemActionBuilder) {
if (!$itemActionBuilder instanceof ActionBuilderInterface) {
throw new UnexpectedTypeException($itemActionBuilder, ActionBuilderInterface::class);
}

$itemActionBuilder->setContext($action->getConfig()->getContext());

$itemAction = $itemActionBuilder->getAction();
$itemAction->setDataTable($action->getDataTable());

Expand All @@ -29,7 +41,7 @@ public function buildView(ActionView $view, ActionInterface $action, array $opti
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->define('actions')
->allowedTypes(ActionBuilderInterface::class.'[]')
->allowedTypes(ActionBuilderInterface::class.'[]', 'callable')
->required()
;
}
Expand Down
15 changes: 7 additions & 8 deletions src/Resources/views/themes/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,13 @@
{% endblock %}
{% block action_dropdown_control %}
<div data-controller="kreyu--data-table-bundle--dropdown">
<button id="toggleButton" data-action="kreyu--data-table-bundle--dropdown#toggle">{{- block('action_control', theme, _context) -}}</button>
<ul data-kreyu--data-table-bundle--dropdown-target="content">
{% for action in actions %}
<li>{{ data_table_action(action) }}</li>
{% endfor %}
</ul>
</div>
{# Themes that extend base theme should provide their own implementation of dropdown #}
<button>{{- block('action_control', theme, _context) -}}</button>
<ul>
{% for action in actions %}
<li>{{ data_table_action(action) }}</li>
{% endfor %}
</ul>
{% endblock %}
{% block action_link_dropdown_item_control %}
Expand Down
25 changes: 16 additions & 9 deletions src/Resources/views/themes/bootstrap_5.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,19 @@
{% endblock %}

{% block action_dropdown_control %}
<div class="dropdown" data-controller="kreyu--data-table-bundle--dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" aria-expanded="false"
data-action="kreyu--data-table-bundle--dropdown#toggle"
>
{{- block('action_control', theme, _context) -}}
<div class="dropdown d-inline-block">
{% set attr = {
'class': 'btn btn-primary dropdown-toggle',
'type': 'button',
'id': data_table.vars.name ~ '--row-action--' ~ name ~ '--button',
'aria-expanded': 'false',
'data-bs-toggle': 'dropdown',
}|merge(attr) %}

<button {{ block('attributes') }}>
{% with { attr: {} } %}{{- block('action_control', theme) -}}{% endwith %}
</button>

{# We must define the confirmation modals outside the ul/li, because the ul is in "position: relative" and that does not work properly with modals. #}
{% for action in actions %}
{% if action.vars.confirmation %}
Expand All @@ -740,14 +747,14 @@
{% if batch %}
{% set confirm_button_attr = {
'data-kreyu--data-table-bundle--batch-target': 'identifierHolder',
}|merge(action.vars.confirm_button_attr) %}
}|merge(confirm_button_attr) %}
{% endif %}

{% with { confirm_button_attr, confirmation } only %}{{ block('action_confirmation_modal') }}{% endwith %}
{% endif %}
{% endfor %}
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton"
data-kreyu--data-table-bundle--dropdown-target="content">

<ul class="dropdown-menu" aria-labelledby="{{ name }}-button">
{% for action in actions %}
<li>{{ data_table_action(action) }}</li>
{% endfor %}
Expand All @@ -756,7 +763,7 @@
{% endblock %}

{% block action_link_dropdown_item_control %}
{% set attr = { 'class': 'dropdown-item'}|merge(attr) %}
{% set attr = { class: 'dropdown-item' }|merge(attr) %}

{% if confirmation %}
{% set attr = {
Expand Down

0 comments on commit 753e28b

Please sign in to comment.