diff options
| author | Sam Light <samlight1994@gmail.com> | 2025-03-27 10:53:34 +0000 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2025-03-27 10:53:34 +0000 | 
| commit | 1f81ce361cb454b1655d6e2a7ac031bc1e3b2ede (patch) | |
| tree | 1307ebad02e2a9644613d496e3d3ec87ed2cdfb9 /src/TableComponent.php | |
| parent | 56a180f139699b3ee1eec566b6a98105576f804a (diff) | |
Seperating toolbar into its own class and views
Diffstat (limited to 'src/TableComponent.php')
| -rw-r--r-- | src/TableComponent.php | 78 | 
1 files changed, 49 insertions, 29 deletions
| diff --git a/src/TableComponent.php b/src/TableComponent.php index b8fae6a..0a55d5e 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -9,6 +9,7 @@ use Livewire\Attributes\Url;  use Illuminate\Pagination\Paginator;  use Illuminate\Database\Eloquent\Builder;  use Illuminate\Support\Str; +use Illuminate\Support\Collection;  use Exception; @@ -18,35 +19,65 @@ abstract class TableComponent extends Component      // Config      protected $paginationTheme = 'bootstrap'; -    protected bool $searchable = true; -    protected int $searchMinLength = 2; -    protected int $searchDebounce = 250; -    protected bool $showPageSizeSelect = true; -    protected bool $showColumnSelect = true; - -    protected array $pageSizes = [10, 25, 50]; -      protected $model = null; +    protected int $defaultPageSize = 10;      // Properties +    #[Url]      public string $search = '';      #[Url] -    public int $pageSize = 0; +    public int $pageSize;      public array $activeColumns = []; -    public function __construct() +    public function mount()      { -        $this->pageSize = $this->pageSizes[0] ?? 0; +        $this->setDefaultActiveColumns(); +        $this->setDefaultPageSize();      } -    public function mount() +    protected function setDefaultPageSize(): void +    { +        if (!isset($this->pageSize)) { +            $this->pageSize = $this->defaultPageSize; +        } +    } + +    protected function setDefaultActiveColumns(): void      {          foreach($this->getColumns() as $column) {              $this->activeColumns[] = $column->name;          }      } +    protected function isSearching(): bool +    { +        $search = $this->getToolbar()->getSearch(); +        return $search !== null && Str::length($this->search) >= $search->getMinLength(); +    } + +    public function updatedSearch(): void +    { +        if ($this->isSearching()) { +            $this->resetPage(); +        } +    } + +    protected function toolbar(): ?Toolbar +    { +        return null; +    } + +    private ?Toolbar $toolbarCache; + +    protected function getToolbar(): ?Toolbar +    { +        if (!isset($this->_toolbar)) { +            $this->toolbar = $this->toolbar(); +        } +        return $this->toolbar; +    } +      protected function query() : Builder      {          if($this->model === null) { @@ -60,25 +91,19 @@ abstract class TableComponent extends Component      protected function search(Builder $builder, string $search) : void {} -    protected function filters() : array -    { -        return []; -    } -      protected function buildQuery() : Builder      {          $query = $this->query(); -        if($this->searchable && (Str::length($this->search) >= $this->searchMinLength)) { +        if ($this->isSearching()) {              $this->search($query, $this->search);          }          return $query;      } -    private $columnsCache = null; - -    protected function getColumns() +    private ?Collection $columnsCache = null; +    public function getColumns()      {          if($this->columnsCache === null) {              $this->columnsCache = collect($this->columns())->each( @@ -94,17 +119,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();          Paginator::defaultView('laralight-tables::pagination'); -        return view('laralight-tables::table', [ -            'searchable' => $this->searchable, -            'searchDebounce' => $this->searchDebounce, -            'showPageSizeSelect' => $this->showPageSizeSelect, -            'showColumnSelect' => $this->showColumnSelect, -            'pageSizes' => $this->pageSizes, -        ] + compact( -            'data', 'allColumns', 'columns' +        return view('laralight-tables::table', compact( +            'data', 'allColumns', 'columns', 'toolbar',          ));      }  } | 
