Skip to content

Commit

Permalink
Beta Fixes - Publishing Views/Localizations. Beta Improvements - Sear…
Browse files Browse the repository at this point in the history
…ch 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 <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Aug 31, 2023
1 parent 23e73c2 commit 1d1da19
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 16 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

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+
- Requires Laravel 10+


- 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
Expand Down
38 changes: 38 additions & 0 deletions docs/columns/reusable-columns.md
Original file line number Diff line number Diff line change
@@ -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'),
];
}
```
2 changes: 1 addition & 1 deletion src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ abstract class DataTableComponent extends Component
WithEvents,
WithFilters,
WithFooter,
WithSecondaryHeader,
WithPagination,
WithRefresh,
WithReordering,
WithSearch,
WithSecondaryHeader,
WithSorting;

/** @phpstan-ignore-next-line */
Expand Down
8 changes: 4 additions & 4 deletions src/LaravelLivewireTablesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
46 changes: 46 additions & 0 deletions src/Traits/Configuration/ColumnConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

use Rappasoft\LaravelLivewireTables\Views\Column;

trait ColumnConfiguration
{
public function setPrependedColumns(array $prependedColumns): void
{
$this->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());

Check warning on line 18 in src/Traits/Configuration/ColumnConfiguration.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Configuration/ColumnConfiguration.php#L17-L18

Added lines #L17 - L18 were not covered by tests
} else {
$column->setTable($this->getTableForColumn($column));

Check warning on line 20 in src/Traits/Configuration/ColumnConfiguration.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Configuration/ColumnConfiguration.php#L20

Added line #L20 was not covered by tests
}
}

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());

Check warning on line 37 in src/Traits/Configuration/ColumnConfiguration.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Configuration/ColumnConfiguration.php#L36-L37

Added lines #L36 - L37 were not covered by tests
} else {
$column->setTable($this->getTableForColumn($column));

Check warning on line 39 in src/Traits/Configuration/ColumnConfiguration.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Configuration/ColumnConfiguration.php#L39

Added line #L39 was not covered by tests
}
}

return $column;
});
}
}
45 changes: 44 additions & 1 deletion src/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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());

Check warning on line 192 in src/Traits/Helpers/ColumnHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/ColumnHelpers.php#L191-L192

Added lines #L191 - L192 were not covered by tests
} else {
$column->setTable($this->getTableForColumn($column));

Check warning on line 194 in src/Traits/Helpers/ColumnHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/ColumnHelpers.php#L194

Added line #L194 was not covered by tests
}
}

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());

Check warning on line 211 in src/Traits/Helpers/ColumnHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/ColumnHelpers.php#L210-L211

Added lines #L210 - L211 were not covered by tests
} else {
$column->setTable($this->getTableForColumn($column));

Check warning on line 213 in src/Traits/Helpers/ColumnHelpers.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/Helpers/ColumnHelpers.php#L213

Added line #L213 was not covered by tests
}
}

return $column;
});

}
}
22 changes: 22 additions & 0 deletions src/Traits/WithColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}
4 changes: 2 additions & 2 deletions src/Views/Columns/ComponentColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

class ComponentColumn extends Column
{
use ComponentColumnHelpers,
ComponentColumnConfiguration;
use ComponentColumnConfiguration,
ComponentColumnHelpers;

protected string $componentView;

Expand Down
4 changes: 2 additions & 2 deletions src/Views/Columns/ImageColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class ImageColumn extends Column
{
use ImageColumnHelpers,
ImageColumnConfiguration;
use ImageColumnConfiguration,
ImageColumnHelpers;

protected string $view = 'livewire-tables::includes.columns.image';

Expand Down
4 changes: 2 additions & 2 deletions src/Views/Columns/LinkColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class LinkColumn extends Column
{
use LinkColumnHelpers,
LinkColumnConfiguration;
use LinkColumnConfiguration,
LinkColumnHelpers;

protected string $view = 'livewire-tables::includes.columns.link';

Expand Down
4 changes: 2 additions & 2 deletions src/Views/Columns/LivewireComponentColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class LivewireComponentColumn extends Column
{
use LivewireComponentColumnHelpers,
LivewireComponentColumnConfiguration;
use LivewireComponentColumnConfiguration,
LivewireComponentColumnHelpers;

protected string $livewireComponent;

Expand Down
29 changes: 29 additions & 0 deletions tests/Traits/Helpers/ColumnHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 1d1da19

Please sign in to comment.