summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--resources/views/dropdown/button.blade.php3
-rw-r--r--resources/views/dropdown/link.blade.php3
-rw-r--r--resources/views/toolbar/dropdown.blade.php12
-rw-r--r--src/Toolbar/Button.php36
-rw-r--r--src/Toolbar/Concerns/WithAction.php33
-rw-r--r--src/Toolbar/Concerns/WithAttributes.php (renamed from src/Concerns/WithAttributes.php)12
-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
-rw-r--r--workbench/app/Livewire/OrdersTable.php28
-rw-r--r--workbench/app/Livewire/ProductsTable.php4
15 files changed, 253 insertions, 34 deletions
diff --git a/resources/views/dropdown/button.blade.php b/resources/views/dropdown/button.blade.php
new file mode 100644
index 0000000..08c658a
--- /dev/null
+++ b/resources/views/dropdown/button.blade.php
@@ -0,0 +1,3 @@
+<button {{ $attributes->class('dropdown-item') }}>
+ {{ $slot }}
+</button>
diff --git a/resources/views/dropdown/link.blade.php b/resources/views/dropdown/link.blade.php
new file mode 100644
index 0000000..271c541
--- /dev/null
+++ b/resources/views/dropdown/link.blade.php
@@ -0,0 +1,3 @@
+<a {{ $attributes->class('dropdown-item') }}>
+ {{ $slot }}
+</a>
diff --git a/resources/views/toolbar/dropdown.blade.php b/resources/views/toolbar/dropdown.blade.php
new file mode 100644
index 0000000..e4cdd8a
--- /dev/null
+++ b/resources/views/toolbar/dropdown.blade.php
@@ -0,0 +1,12 @@
+<div class="dropdown">
+ <button type="button" data-bs-toggle="dropdown" {{ $attributes->class('btn') }}>
+ {{ $slot }}
+ </button>
+ @if ($items->isNotEmpty())
+ <ul class="dropdown-menu">
+ @foreach ($items as $item)
+ <li>{{ $item->render() }}</li>
+ @endforeach
+ </ul>
+ @endif
+</div>
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/Concerns/WithAttributes.php b/src/Toolbar/Concerns/WithAttributes.php
index dceec69..3a37282 100644
--- a/src/Concerns/WithAttributes.php
+++ b/src/Toolbar/Concerns/WithAttributes.php
@@ -2,7 +2,7 @@
declare(strict_types=1);
-namespace Lightscale\LaralightTables\Concerns;
+namespace Lightscale\LaralightTables\Toolbar\Concerns;
use Illuminate\View\ComponentAttributeBag;
@@ -23,8 +23,14 @@ trait WithAttributes
return $this;
}
- protected function getAttributes(): ComponentAttributeBag
+ /**
+ * @param array<string, string|null> $merge
+ */
+ protected function getAttributes(array $merge = []): ComponentAttributeBag
{
- return new ComponentAttributeBag($this->attributes ?? []);
+ 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
{
diff --git a/workbench/app/Livewire/OrdersTable.php b/workbench/app/Livewire/OrdersTable.php
index 1b15214..431e8a8 100644
--- a/workbench/app/Livewire/OrdersTable.php
+++ b/workbench/app/Livewire/OrdersTable.php
@@ -5,6 +5,10 @@ namespace Workbench\App\Livewire;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Lightscale\LaralightTables\Columns\Column;
+use Lightscale\LaralightTables\Toolbar;
+use Lightscale\LaralightTables\Toolbar\Dropdown;
+use Lightscale\LaralightTables\Toolbar\Dropdown\Button;
+use Lightscale\LaralightTables\Toolbar\Dropdown\Link;
use Workbench\App\Models\Order;
/**
@@ -22,6 +26,30 @@ class OrdersTable extends Table
return Order::withCount('products');
}
+ public function toolbars(): array
+ {
+ return [
+ Toolbar::make($this)
+ ->appendEnd(Dropdown::make()
+ ->attributes(['class' => 'btn-primary'])
+ ->slot('Dropdown')
+ ->items([
+ Button::make()
+ ->slot('Item button 1')
+ ->action(['dropdownAction', 'World']),
+ Link::make()
+ ->slot('Products')
+ ->href(route('products')),
+ ])
+ ),
+ ];
+ }
+
+ public function dropdownAction(string $val): void
+ {
+ $this->js("alert('hello {$val}')");
+ }
+
public function columns(): array
{
return [
diff --git a/workbench/app/Livewire/ProductsTable.php b/workbench/app/Livewire/ProductsTable.php
index 5469c7d..f1afce7 100644
--- a/workbench/app/Livewire/ProductsTable.php
+++ b/workbench/app/Livewire/ProductsTable.php
@@ -40,9 +40,9 @@ class ProductsTable extends Table
->appendStart($categoryFilter)
->appendEnd(PageSize::make())
->appendEnd(ColumnSelect::make())
- ->appendEnd( Button::make()
+ ->appendEnd(Button::make()
->attributes([
- 'class'=> 'btn btn-primary'
+ 'class' => 'btn btn-primary',
])
->slot('HELLO')
->action('sayHello()')