From 1d1da191971ab79f076e40e2625c315e7438f07a Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Thu, 31 Aug 2023 22:37:13 +0100 Subject: [PATCH] Beta Fixes - Publishing Views/Localizations. Beta Improvements - Search Options (#1324) * Update setSearchDebounce, add setSearchThrottle/setSearchBlur * Remove Lazy Test * Remove Lazy Tests - Update Docs * Update Views Publish Path * Add Translations Publish Option * Update ChangeLog * Add Reusable Columns * Add Tests for Prepend/Append Cols --------- Co-authored-by: lrljoe --- CHANGELOG.md | 12 ++++- docs/columns/reusable-columns.md | 38 +++++++++++++++ src/DataTableComponent.php | 2 +- src/LaravelLivewireTablesServiceProvider.php | 8 ++-- .../Configuration/ColumnConfiguration.php | 46 +++++++++++++++++++ src/Traits/Helpers/ColumnHelpers.php | 45 +++++++++++++++++- src/Traits/WithColumns.php | 22 +++++++++ src/Views/Columns/ComponentColumn.php | 4 +- src/Views/Columns/ImageColumn.php | 4 +- src/Views/Columns/LinkColumn.php | 4 +- src/Views/Columns/LivewireComponentColumn.php | 4 +- tests/Traits/Helpers/ColumnHelpersTest.php | 29 ++++++++++++ 12 files changed, 202 insertions(+), 16 deletions(-) create mode 100644 docs/columns/reusable-columns.md create mode 100644 src/Traits/Configuration/ColumnConfiguration.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc217255..ecf0de1bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,16 @@ All notable changes to `laravel-livewire-tables` will be documented in this file -## [Unreleased] - 3.x +## [Unreleased] - 3.x - Beta Fixes (beta-1) +- Removal of setSearchLazy +- Fix for setSearchDebounce +- Add setSearchBlur() +- Add setSearchThrottle() +- Fix publishing of views +- Add publish translations +- Add prependColumns() and appendColumns() functions + +## [Unreleased] - 3.x (beta-0) - Requirements Change - Requires LiveWire 3.x - Requires PHP 8.1+ @@ -10,7 +19,6 @@ All notable changes to `laravel-livewire-tables` will be documented in this file - Core Changes - - Move sorts, search, selectedColumns out of the traditional __$this->{$this->getTableName()}['sorts']__ and instead place it directly within the component. This: - Improves the query string behaviour - Reduces the need to repeatedly set up that main array diff --git a/docs/columns/reusable-columns.md b/docs/columns/reusable-columns.md new file mode 100644 index 000000000..53b5865b2 --- /dev/null +++ b/docs/columns/reusable-columns.md @@ -0,0 +1,38 @@ +--- +title: Reusable Columns +weight: 8 +--- + +Often you will want to re-use the same column across multiple tables. For example a "Created At" and "Used At" column. + +To mitigate the pain of maintaining this, two new methods have been introduced. + +These methods both function in exactly the same way as your standard columns(), and expect an array of columns. + +Any columns defined in prependColumns() will be the first columns in your list of columns. +``` +public function prependColumns(): array +{ + return []; +} +``` + +Any columns defined in appendColumns() will be the last columns in your list of columns. +``` +public function appendColumns(): array +{ + return []; +} +``` + +You can call these in your trait, and they will be automatically appended/prepended to tables. + +For example, to append a Column for Updated At +``` +public function appendColumns(): array +{ + return [ + Column::make('Updated At', 'updated_at'), + ]; +} +``` diff --git a/src/DataTableComponent.php b/src/DataTableComponent.php index e23530252..7733b00c0 100644 --- a/src/DataTableComponent.php +++ b/src/DataTableComponent.php @@ -32,11 +32,11 @@ abstract class DataTableComponent extends Component WithEvents, WithFilters, WithFooter, - WithSecondaryHeader, WithPagination, WithRefresh, WithReordering, WithSearch, + WithSecondaryHeader, WithSorting; /** @phpstan-ignore-next-line */ diff --git a/src/LaravelLivewireTablesServiceProvider.php b/src/LaravelLivewireTablesServiceProvider.php index a00c597ed..0ff769208 100644 --- a/src/LaravelLivewireTablesServiceProvider.php +++ b/src/LaravelLivewireTablesServiceProvider.php @@ -20,21 +20,21 @@ public function boot(): void __DIR__.'/../config/livewire-tables.php', 'livewire-tables' ); - $this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables'); - $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'livewire-tables'); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables'); + if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/../resources/lang' => $this->app->langPath('livewire-tables'), - ]); + ], 'livewire-tables-translations'); $this->publishes([ __DIR__.'/../config/livewire-tables.php' => config_path('livewire-tables.php'), ], 'livewire-tables-config'); $this->publishes([ - __DIR__.'/../resources/views' => resource_path('views/vendor/rappasoft/livewire-tables'), + __DIR__.'/../resources/views' => resource_path('views/vendor/livewire-tables'), ], 'livewire-tables-views'); $this->publishes([ diff --git a/src/Traits/Configuration/ColumnConfiguration.php b/src/Traits/Configuration/ColumnConfiguration.php new file mode 100644 index 000000000..1e8d111f5 --- /dev/null +++ b/src/Traits/Configuration/ColumnConfiguration.php @@ -0,0 +1,46 @@ +prependedColumns = collect($prependedColumns) + ->filter(fn ($column) => $column instanceof Column) + ->map(function (Column $column) { + $column->setComponent($this); + + if ($column->hasField()) { + if ($column->isBaseColumn()) { + $column->setTable($this->getBuilder()->getModel()->getTable()); + } else { + $column->setTable($this->getTableForColumn($column)); + } + } + + return $column; + }); + } + + public function setAppendedColumns(array $appendedColumns): void + { + $this->appendedColumns = collect($appendedColumns) + ->filter(fn ($column) => $column instanceof Column) + ->map(function (Column $column) { + $column->setComponent($this); + + if ($column->hasField()) { + if ($column->isBaseColumn()) { + $column->setTable($this->getBuilder()->getModel()->getTable()); + } else { + $column->setTable($this->getTableForColumn($column)); + } + } + + return $column; + }); + } +} diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index ab5f05606..8bafdf275 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -12,6 +12,8 @@ trait ColumnHelpers */ public function setColumns(): void { + $this->prependedColumns = $this->getPrependedColumns(); + $columns = collect($this->columns()) ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { @@ -28,7 +30,9 @@ public function setColumns(): void return $column; }); - $this->columns = $columns; + $this->appendedColumns = $this->getAppendedColumns(); + + $this->columns = collect([...$this->prependedColumns, ...$columns, ...$this->appendedColumns]); } public function getColumns(): Collection @@ -175,4 +179,43 @@ public function getColspanCount(): int { return 100; } + + public function getPrependedColumns(): Collection + { + return collect($this->prependedColumns ?? $this->prependColumns()) + ->filter(fn ($column) => $column instanceof Column) + ->map(function (Column $column) { + $column->setComponent($this); + + if ($column->hasField()) { + if ($column->isBaseColumn()) { + $column->setTable($this->getBuilder()->getModel()->getTable()); + } else { + $column->setTable($this->getTableForColumn($column)); + } + } + + return $column; + }); + } + + public function getAppendedColumns(): Collection + { + return collect($this->appendedColumns ?? $this->appendColumns()) + ->filter(fn ($column) => $column instanceof Column) + ->map(function (Column $column) { + $column->setComponent($this); + + if ($column->hasField()) { + if ($column->isBaseColumn()) { + $column->setTable($this->getBuilder()->getModel()->getTable()); + } else { + $column->setTable($this->getTableForColumn($column)); + } + } + + return $column; + }); + + } } diff --git a/src/Traits/WithColumns.php b/src/Traits/WithColumns.php index 9df93cc50..9f0e3890e 100644 --- a/src/Traits/WithColumns.php +++ b/src/Traits/WithColumns.php @@ -3,16 +3,38 @@ namespace Rappasoft\LaravelLivewireTables\Traits; use Illuminate\Support\Collection; +use Rappasoft\LaravelLivewireTables\Traits\Configuration\ColumnConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ColumnHelpers; trait WithColumns { + use ColumnConfiguration; use ColumnHelpers; protected Collection $columns; + protected Collection $prependedColumns; + + protected Collection $appendedColumns; + public function bootWithColumns(): void { $this->columns = collect(); } + + /** + * Prepend columns. + */ + public function prependColumns() + { + return []; + } + + /** + * Append columns. + */ + public function appendColumns(): array + { + return []; + } } diff --git a/src/Views/Columns/ComponentColumn.php b/src/Views/Columns/ComponentColumn.php index b15ddb1e7..2459b1c85 100644 --- a/src/Views/Columns/ComponentColumn.php +++ b/src/Views/Columns/ComponentColumn.php @@ -12,8 +12,8 @@ class ComponentColumn extends Column { - use ComponentColumnHelpers, - ComponentColumnConfiguration; + use ComponentColumnConfiguration, + ComponentColumnHelpers; protected string $componentView; diff --git a/src/Views/Columns/ImageColumn.php b/src/Views/Columns/ImageColumn.php index 9cd5adf74..f2bc48d12 100644 --- a/src/Views/Columns/ImageColumn.php +++ b/src/Views/Columns/ImageColumn.php @@ -10,8 +10,8 @@ class ImageColumn extends Column { - use ImageColumnHelpers, - ImageColumnConfiguration; + use ImageColumnConfiguration, + ImageColumnHelpers; protected string $view = 'livewire-tables::includes.columns.image'; diff --git a/src/Views/Columns/LinkColumn.php b/src/Views/Columns/LinkColumn.php index 92e7ad3f5..b62330b30 100644 --- a/src/Views/Columns/LinkColumn.php +++ b/src/Views/Columns/LinkColumn.php @@ -10,8 +10,8 @@ class LinkColumn extends Column { - use LinkColumnHelpers, - LinkColumnConfiguration; + use LinkColumnConfiguration, + LinkColumnHelpers; protected string $view = 'livewire-tables::includes.columns.link'; diff --git a/src/Views/Columns/LivewireComponentColumn.php b/src/Views/Columns/LivewireComponentColumn.php index 4842cb8b3..ce10d4268 100644 --- a/src/Views/Columns/LivewireComponentColumn.php +++ b/src/Views/Columns/LivewireComponentColumn.php @@ -13,8 +13,8 @@ class LivewireComponentColumn extends Column { - use LivewireComponentColumnHelpers, - LivewireComponentColumnConfiguration; + use LivewireComponentColumnConfiguration, + LivewireComponentColumnHelpers; protected string $livewireComponent; diff --git a/tests/Traits/Helpers/ColumnHelpersTest.php b/tests/Traits/Helpers/ColumnHelpersTest.php index c925c78ae..e1fb9379b 100644 --- a/tests/Traits/Helpers/ColumnHelpersTest.php +++ b/tests/Traits/Helpers/ColumnHelpersTest.php @@ -15,6 +15,35 @@ public function can_get_column_list(): void $this->assertCount(9, $this->basicTable->getColumns()->toArray()); } + /** @test */ + public function can_append_column(): void + { + $this->assertCount(9, $this->basicTable->getColumns()->toArray()); + + $this->basicTable->setAppendedColumns([Column::make('IDLabel')->label(function ($row) { + return 'Test'; + })]); + + $this->basicTable->setColumns(); + + $this->assertCount(10, $this->basicTable->getColumns()->toArray()); + + } + + /** @test */ + public function can_prepend_column(): void + { + $this->assertCount(9, $this->basicTable->getColumns()->toArray()); + + $this->basicTable->setPrependedColumns([Column::make('IDLabel')->label(function ($row) { + return 'Test'; + })]); + + $this->basicTable->setColumns(); + + $this->assertCount(10, $this->basicTable->getColumns()->toArray()); + } + /** @test */ public function can_get_column_by_column(): void {