From 536fa6c917d63b14feeb8f876e6a1d62acb577fc Mon Sep 17 00:00:00 2001 From: Sam Light Date: Thu, 12 Feb 2026 00:23:02 +0000 Subject: Created new dropdown toolbar item --- src/Concerns/WithAttributes.php | 30 ---------------------- src/Toolbar/Button.php | 36 ++++++--------------------- src/Toolbar/Concerns/WithAction.php | 33 +++++++++++++++++++++++++ src/Toolbar/Concerns/WithAttributes.php | 36 +++++++++++++++++++++++++++ src/Toolbar/Concerns/WithHref.php | 17 +++++++++++++ src/Toolbar/Concerns/WithSlot.php | 17 +++++++++++++ src/Toolbar/Dropdown.php | 44 +++++++++++++++++++++++++++++++++ src/Toolbar/Dropdown/Button.php | 30 ++++++++++++++++++++++ src/Toolbar/Dropdown/Item.php | 16 ++++++++++++ src/Toolbar/Dropdown/Link.php | 30 ++++++++++++++++++++++ src/Toolbar/SelectFilter.php | 2 +- 11 files changed, 232 insertions(+), 59 deletions(-) delete mode 100644 src/Concerns/WithAttributes.php create mode 100644 src/Toolbar/Concerns/WithAction.php create mode 100644 src/Toolbar/Concerns/WithAttributes.php create mode 100644 src/Toolbar/Concerns/WithHref.php create mode 100644 src/Toolbar/Concerns/WithSlot.php create mode 100644 src/Toolbar/Dropdown.php create mode 100644 src/Toolbar/Dropdown/Button.php create mode 100644 src/Toolbar/Dropdown/Item.php create mode 100644 src/Toolbar/Dropdown/Link.php (limited to 'src') diff --git a/src/Concerns/WithAttributes.php b/src/Concerns/WithAttributes.php deleted file mode 100644 index dceec69..0000000 --- a/src/Concerns/WithAttributes.php +++ /dev/null @@ -1,30 +0,0 @@ - - */ - protected ?array $attributes = null; - - /** - * @param array $attributes - */ - public function attributes(array $attributes): static - { - $this->attributes = $attributes; - - return $this; - } - - protected function getAttributes(): ComponentAttributeBag - { - return new ComponentAttributeBag($this->attributes ?? []); - } -} 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 @@ +} $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 @@ + + */ + protected ?array $attributes = null; + + /** + * @param array $attributes + */ + public function attributes(array $attributes): static + { + $this->attributes = $attributes; + + return $this; + } + + /** + * @param array $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 @@ +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 @@ +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 @@ + + */ + protected Collection $items; + + /** + * @param iterable $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 @@ + $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 @@ + $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 { -- cgit v1.2.3