From 40607777a5752523f6750312675db09665726a5f Mon Sep 17 00:00:00 2001
From: Sam Light <samlight1994@gmail.com>
Date: Wed, 2 Apr 2025 23:18:08 +0100
Subject: Switch to allow multiple toolbars

---
 src/TableComponent.php | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

(limited to 'src')

diff --git a/src/TableComponent.php b/src/TableComponent.php
index 7186b1f..14d631b 100644
--- a/src/TableComponent.php
+++ b/src/TableComponent.php
@@ -68,7 +68,10 @@ abstract class TableComponent extends Component
 
     protected function isSearching(): bool
     {
-        $search = $this->getToolbar()?->getSearch();
+        $search = $this
+            ->getToolbars()
+            ->first(fn(Toolbar $tb) => $tb->getSearch() !== null)
+            ?->getSearch();
         return $search !== null && Str::length($this->search) >= $search->getMinLength();
     }
 
@@ -101,19 +104,19 @@ abstract class TableComponent extends Component
         }
     }
 
-    protected function toolbar(): ?Toolbar
+    protected function toolbars(): array
     {
-        return null;
+        return [];
     }
 
-    private ?Toolbar $toolbarCache;
+    private Collection $toolbarsCache;
 
-    protected function getToolbar(): ?Toolbar
+    protected function getToolbars(): Collection
     {
-        if (!isset($this->_toolbar)) {
-            $this->toolbar = $this->toolbar();
+        if (!isset($this->toolbarsCache)) {
+            $this->toolbarsCache = collect($this->toolbars());
         }
-        return $this->toolbar;
+        return $this->toolbarsCache;
     }
 
     protected function query() : Builder
@@ -131,7 +134,9 @@ abstract class TableComponent extends Component
 
     protected function getFilters(): Collection
     {
-        return $this->getToolbar()?->getFilters() ?? collect();
+        return $this->getToolbars()
+            ->map(fn(Toolbar $toolbar) => $toolbar->getFilters())
+            ->flatten();
     }
 
 
@@ -182,12 +187,12 @@ abstract class TableComponent extends Component
         $data = $this->buildQuery()->paginate($this->pageSize);
         $allColumns = $this->getColumns();
         $columns = $allColumns->filter(fn($c) => in_array($c->name,$this->activeColumns));
-        $toolbar = $this->toolbar();
+        $toolbars = $this->getToolbars();
 
         Paginator::defaultView('laralight-tables::pagination');
 
         return view('laralight-tables::table', compact(
-            'data', 'allColumns', 'columns', 'toolbar',
+            'data', 'allColumns', 'columns', 'toolbars',
         ));
     }
 }
-- 
cgit v1.2.3