Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List/Show: be able to add actions to a dropdown 2le/crudit#421 #455

Merged
merged 12 commits into from
Aug 7, 2024
24 changes: 0 additions & 24 deletions assets/sb-admin/css/components/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,3 @@ a {
}
}
}

.btn-group {
.btn-wrapper {
&:not(:first-of-type):not(:last-of-type) {
.btn {
border-radius: 0;
}
}

&:first-of-type {
.btn {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
}

&:last-of-type {
.btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
}
}
}
5 changes: 3 additions & 2 deletions src/Brick/LinksBrick/LinksConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Lle\CruditBundle\Brick\LinksBrick;

use Lle\CruditBundle\Brick\AbstractBrickConfig;
use Lle\CruditBundle\Dto\Action\DropdownAction;
use Lle\CruditBundle\Dto\Action\ItemAction;
use Lle\CruditBundle\Dto\Action\ListAction;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -51,8 +52,8 @@ public function getActions(): array

public function setActions(array $actions): self
{
$this->actions = array_filter($actions, function (ItemAction|ListAction $a) {
return !$a->isBatch();
$this->actions = array_filter($actions, function (ItemAction|ListAction|DropdownAction $a) {
return $a instanceof DropdownAction || !$a->isBatch();
});

return $this;
Expand Down
5 changes: 3 additions & 2 deletions src/Brick/ListBrick/ListConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Lle\CruditBundle\Contracts\CrudConfigInterface;
use Lle\CruditBundle\Contracts\DatasourceInterface;
use Lle\CruditBundle\Datasource\DatasourceParams;
use Lle\CruditBundle\Dto\Action\DropdownAction;
use Lle\CruditBundle\Dto\Action\ItemAction;
use Lle\CruditBundle\Dto\Action\ListAction;
use Lle\CruditBundle\Dto\Field\Field;
Expand Down Expand Up @@ -110,8 +111,8 @@ public function setActions(array $actions): self

public function setBatchActions(array $actions): self
{
$this->batch_actions = array_filter($actions, function (ItemAction|ListAction $a) {
return $a->isBatch();
$this->batch_actions = array_filter($actions, function (ItemAction|ListAction|DropdownAction $a) {
return !$a instanceof DropdownAction && $a->isBatch();
});

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

interface ActionInterface
{
public function __construct(string $label, Path $path);
public function __construct(string $label);

public function getId(): string;

Expand Down
12 changes: 6 additions & 6 deletions src/Crud/AbstractCrudConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ public function getItemActions(): array
$this->getPath(CrudConfigInterface::SHOW),
Icon::new('search')
)
->setCssClass('btn btn-primary btn-sm mr-1')
->setCssClass('btn btn-primary btn-sm')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::SHOW));

$actions[CrudConfigInterface::ACTION_EDIT] = EditAction::new(
'action.edit',
$this->getPath(CrudConfigInterface::EDIT),
Icon::new('edit')
)
->setCssClass('btn btn-secondary btn-sm mr-1')
->setCssClass('btn btn-secondary btn-sm')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::EDIT));

$actions[CrudConfigInterface::ACTION_DELETE] = DeleteAction::new(
'action.delete',
$this->getPath(CrudConfigInterface::DELETE),
Icon::new('trash-alt')
)
->setCssClass('btn btn-danger btn-sm mr-1')
->setCssClass('btn btn-danger btn-sm')
->setConfirmModal(true)
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::DELETE));

Expand All @@ -135,23 +135,23 @@ public function getShowActions(): array
$this->getPath(CrudConfigInterface::INDEX),
Icon::new('list')
)
->setCssClass('btn btn-secondary btn-sm mr-1')
->setCssClass('btn btn-secondary btn-sm ms-1')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::INDEX));

$actions[CrudConfigInterface::ACTION_EDIT] = EditAction::new(
'action.edit',
$this->getPath(CrudConfigInterface::EDIT),
Icon::new('edit')
)
->setCssClass('btn btn-secondary btn-sm mr-1')
->setCssClass('btn btn-secondary btn-sm ms-1')
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::EDIT));

$actions[CrudConfigInterface::ACTION_DELETE] = DeleteAction::new(
'action.delete',
$this->getPath(CrudConfigInterface::DELETE),
Icon::new('trash-alt')
)
->setCssClass('btn btn-danger btn-sm mr-1')
->setCssClass('btn btn-danger btn-sm ms-1')
->setConfirmModal(true)
->setRole(sprintf('ROLE_%s_%s', $this->getName(), CrudConfigInterface::DELETE));

Expand Down
27 changes: 23 additions & 4 deletions src/Dto/Action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class AbstractAction implements ActionInterface

protected ?Icon $icon;

protected ?string $url;
protected ?string $url = null;

protected ?string $cssClass = null;

Expand All @@ -34,11 +34,11 @@ abstract class AbstractAction implements ActionInterface

protected ?bool $hideIfDisabled = null;

public function __construct(string $label, Path $path)
protected bool $dropdown = false;

public function __construct(string $label)
{
$this->label = $label;
$this->path = $path;
$this->url = null;
}

public function getId(): string
Expand All @@ -61,6 +61,13 @@ public function getPath(): Path
return $this->path;
}

public function setPath(Path $path): static
{
$this->path = $path;

return $this;
}

public function getIcon(): ?Icon
{
return $this->icon;
Expand Down Expand Up @@ -188,4 +195,16 @@ public function setHideIfDisabled(?bool $hideIfDisabled): static

return $this;
}

public function isDropdown(): bool
{
return $this->dropdown;
}

public function setDropdown(bool $dropdown): self
{
$this->dropdown = $dropdown;

return $this;
}
}
6 changes: 2 additions & 4 deletions src/Dto/Action/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Lle\CruditBundle\Dto\Action;

use Lle\CruditBundle\Dto\Path;

class DeleteAction extends ItemAction
{
public const CALLBACK = "canDelete";

public function __construct(string $label, Path $path)
public function __construct(string $label)
{
parent::__construct($label, $path);
parent::__construct($label);
}

public function getTitle(): string
Expand Down
39 changes: 39 additions & 0 deletions src/Dto/Action/DropdownAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Lle\CruditBundle\Dto\Action;

use Lle\CruditBundle\Dto\Icon;

class DropdownAction extends AbstractAction
{
protected array $actions;

/**
* @param array<AbstractAction[]> $actions
*/
public static function new(string $label, array $actions, ?Icon $icon = null): static
{
return (new static($label))
->setActions($actions)
->setIcon($icon);
}

public function __construct(string $label)
{
$this->label = $label;
}

public function getActions(): array
{
return $this->actions;
}

public function setActions(array $actions): static
{
$this->actions = $actions;

return $this;
}
}
16 changes: 2 additions & 14 deletions src/Dto/Action/ItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
class ItemAction extends AbstractAction
{
protected ?object $resource = null;
protected bool $dropdown = false;
protected ?string $title = null;
protected bool $disabled = false;

public static function new(string $label, Path $path, ?Icon $icon = null): static
{
return (new static($label, $path))
return (new static($label))
->setPath($path)
->setIcon($icon)
->setHideLabel(false);
}
Expand All @@ -34,18 +34,6 @@ public function setResource(?object $resource): self
return $this;
}

public function isDropdown(): bool
{
return $this->dropdown;
}

public function setDropdown(bool $dropdown): self
{
$this->dropdown = $dropdown;

return $this;
}

public function isDisabled(): bool
{
return $this->disabled;
Expand Down
3 changes: 2 additions & 1 deletion src/Dto/Action/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ListAction extends AbstractAction

public static function new(string $label, Path $path, ?Icon $icon = null): static
{
return (new static($label, $path))
return (new static($label))
->setPath($path)
->setIcon($icon)
->setHideLabel(false);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ services:
Lle\CruditBundle\Service\SanitizerService: ~
Lle\CruditBundle\Contracts\SanitizerInterface: '@Lle\CruditBundle\Service\SanitizerService'

Lle\CruditBundle\Twig\CruditActionExtension:
tags: [twig.extension]

when@dev:
services:
Lle\CruditBundle\Service\EasyAdminConverter\Converter:
Expand Down
Loading
Loading