summaryrefslogtreecommitdiff
path: root/src/Toolbar
diff options
context:
space:
mode:
Diffstat (limited to 'src/Toolbar')
-rw-r--r--src/Toolbar/Button.php36
-rw-r--r--src/Toolbar/Concerns/WithAction.php33
-rw-r--r--src/Toolbar/Concerns/WithAttributes.php36
-rw-r--r--src/Toolbar/Concerns/WithHref.php17
-rw-r--r--src/Toolbar/Concerns/WithSlot.php17
-rw-r--r--src/Toolbar/Dropdown.php44
-rw-r--r--src/Toolbar/Dropdown/Button.php30
-rw-r--r--src/Toolbar/Dropdown/Item.php16
-rw-r--r--src/Toolbar/Dropdown/Link.php30
-rw-r--r--src/Toolbar/SelectFilter.php2
10 files changed, 232 insertions, 29 deletions
diff --git a/src/Toolbar/Button.php b/src/Toolbar/Button.php
index 92b041f..1ac686a 100644
--- a/src/Toolbar/Button.php
+++ b/src/Toolbar/Button.php
@@ -4,38 +4,16 @@ declare(strict_types=1);
namespace Lightscale\LaralightTables\Toolbar;
-use Illuminate\View\ComponentAttributeBag;
use Illuminate\View\View;
-use Lightscale\LaralightTables\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAction;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithSlot;
class Button extends Item
{
+ use WithAction;
use WithAttributes;
-
- private ?string $action = null;
-
- private string $slot = '';
-
- public function action(string $action): static
- {
- $this->action = $action;
-
- return $this;
- }
-
- public function slot(string $slot): static
- {
- $this->slot = $slot;
-
- return $this;
- }
-
- private function makeAttributes(): ComponentAttributeBag
- {
- return $this->getAttributes()->merge([
- 'wire:click' => $this->action,
- ]);
- }
+ use WithSlot;
public function render(): View
{
@@ -43,7 +21,9 @@ class Button extends Item
'laralight-tables::toolbar.button',
[
'action' => $this->action,
- 'attributes' => $this->makeAttributes(),
+ 'attributes' => $this->getAttributes([
+ 'wire:click' => $this->action,
+ ]),
'slot' => $this->slot,
]
);
diff --git a/src/Toolbar/Concerns/WithAction.php b/src/Toolbar/Concerns/WithAction.php
new file mode 100644
index 0000000..a1c33f2
--- /dev/null
+++ b/src/Toolbar/Concerns/WithAction.php
@@ -0,0 +1,33 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Concerns;
+
+trait WithAction
+{
+ protected ?string $action = null;
+
+ /**
+ * @param string|array{0: string, ...<int, string|numeric|bool|null>} $action
+ */
+ public function action(string|array $action): static
+ {
+ if (is_array($action)) {
+ $fn = array_shift($action);
+ $params = collect($action)->map(
+ fn ($v) => match (strtolower(gettype($v))) {
+ 'string' => "'{$v}'",
+ 'boolean' => $v ? 'true' : 'false',
+ 'null' => 'null',
+ default => (string) $v,
+ }
+ )->join(', ');
+ $this->action = "{$fn}({$params})";
+ } else {
+ $this->action = $action;
+ }
+
+ return $this;
+ }
+}
diff --git a/src/Toolbar/Concerns/WithAttributes.php b/src/Toolbar/Concerns/WithAttributes.php
new file mode 100644
index 0000000..3a37282
--- /dev/null
+++ b/src/Toolbar/Concerns/WithAttributes.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Concerns;
+
+use Illuminate\View\ComponentAttributeBag;
+
+trait WithAttributes
+{
+ /**
+ * @var array<string, string>
+ */
+ protected ?array $attributes = null;
+
+ /**
+ * @param array<string, string> $attributes
+ */
+ public function attributes(array $attributes): static
+ {
+ $this->attributes = $attributes;
+
+ return $this;
+ }
+
+ /**
+ * @param array<string, string|null> $merge
+ */
+ protected function getAttributes(array $merge = []): ComponentAttributeBag
+ {
+ return new ComponentAttributeBag([
+ ...$merge,
+ ...($this->attributes ?? []),
+ ]);
+ }
+}
diff --git a/src/Toolbar/Concerns/WithHref.php b/src/Toolbar/Concerns/WithHref.php
new file mode 100644
index 0000000..50d4c81
--- /dev/null
+++ b/src/Toolbar/Concerns/WithHref.php
@@ -0,0 +1,17 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Concerns;
+
+trait WithHref
+{
+ protected string $href;
+
+ public function href(string $href): static
+ {
+ $this->href = $href;
+
+ return $this;
+ }
+}
diff --git a/src/Toolbar/Concerns/WithSlot.php b/src/Toolbar/Concerns/WithSlot.php
new file mode 100644
index 0000000..2279f1d
--- /dev/null
+++ b/src/Toolbar/Concerns/WithSlot.php
@@ -0,0 +1,17 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Concerns;
+
+trait WithSlot
+{
+ protected string $slot = '';
+
+ public function slot(string $slot): static
+ {
+ $this->slot = $slot;
+
+ return $this;
+ }
+}
diff --git a/src/Toolbar/Dropdown.php b/src/Toolbar/Dropdown.php
new file mode 100644
index 0000000..45bdbee
--- /dev/null
+++ b/src/Toolbar/Dropdown.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar;
+
+use Illuminate\Support\Collection;
+use Illuminate\View\View;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithSlot;
+use Lightscale\LaralightTables\Toolbar\Dropdown\Item as DropdownItem;
+
+class Dropdown extends Item
+{
+ use WithAttributes;
+ use WithSlot;
+
+ /**
+ * @var Collection<int, DropdownItem>
+ */
+ protected Collection $items;
+
+ /**
+ * @param iterable<int, DropdownItem> $items
+ */
+ public function items(iterable $items): static
+ {
+ $this->items = Collection::wrap($items);
+
+ return $this;
+ }
+
+ public function render(): View
+ {
+ return view(
+ 'laralight-tables::toolbar.dropdown',
+ [
+ 'attributes' => $this->getAttributes(),
+ 'slot' => $this->slot,
+ 'items' => $this->items,
+ ]
+ );
+ }
+}
diff --git a/src/Toolbar/Dropdown/Button.php b/src/Toolbar/Dropdown/Button.php
new file mode 100644
index 0000000..83aefbf
--- /dev/null
+++ b/src/Toolbar/Dropdown/Button.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Dropdown;
+
+use Illuminate\View\View;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAction;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithSlot;
+
+class Button extends Item
+{
+ use WithAction;
+ use WithAttributes;
+ use WithSlot;
+
+ public function render(): View
+ {
+ return view(
+ 'laralight-tables::dropdown.button',
+ [
+ 'attributes' => $this->getAttributes([
+ 'wire:click' => $this->action,
+ ]),
+ 'slot' => $this->slot,
+ ]
+ );
+ }
+}
diff --git a/src/Toolbar/Dropdown/Item.php b/src/Toolbar/Dropdown/Item.php
new file mode 100644
index 0000000..c10c8f3
--- /dev/null
+++ b/src/Toolbar/Dropdown/Item.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Dropdown;
+
+use Illuminate\Support\HtmlString;
+use Illuminate\View\View;
+use Lightscale\LaralightTables\Concerns\Makable;
+
+abstract class Item
+{
+ use Makable;
+
+ abstract public function render(): View|HtmlString|string|null;
+}
diff --git a/src/Toolbar/Dropdown/Link.php b/src/Toolbar/Dropdown/Link.php
new file mode 100644
index 0000000..2602f41
--- /dev/null
+++ b/src/Toolbar/Dropdown/Link.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\LaralightTables\Toolbar\Dropdown;
+
+use Illuminate\View\View;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithHref;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithSlot;
+
+class Link extends Item
+{
+ use WithAttributes;
+ use WithHref;
+ use WithSlot;
+
+ public function render(): View
+ {
+ return view(
+ 'laralight-tables::dropdown.link',
+ [
+ 'attributes' => $this->getAttributes([
+ 'href' => $this->href,
+ ]),
+ 'slot' => $this->slot,
+ ]
+ );
+ }
+}
diff --git a/src/Toolbar/SelectFilter.php b/src/Toolbar/SelectFilter.php
index fa5b893..dd0286a 100644
--- a/src/Toolbar/SelectFilter.php
+++ b/src/Toolbar/SelectFilter.php
@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Lightscale\LaralightTables\Toolbar;
use Illuminate\View\View;
-use Lightscale\LaralightTables\Concerns\WithAttributes;
+use Lightscale\LaralightTables\Toolbar\Concerns\WithAttributes;
class SelectFilter extends Filter
{