Skip to content

Commit

Permalink
Change traits to booted (#1603)
Browse files Browse the repository at this point in the history
* Use booted instead of boot for traits

* Update Test Case

* Add test for mount parameters

* Update for NoColumns Test

* Add null mount
---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Dec 26, 2023
1 parent c307360 commit 4e75b3d
Show file tree
Hide file tree
Showing 13 changed files with 372 additions and 18 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED

### Bug Fixes
- Ensure mount() is called prior to bundler() executing by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1603

### New Features
- Add capability to call a new "addAdditionalSelects()" to append select table fields (without impacting setAdditionalSelect)
- Add capability to call a new "addAdditionalSelects()" to append select table fields (without impacting setAdditionalSelect) by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1609

## [v3.1.5] - 2023-12-09
### New Features
Expand Down
4 changes: 2 additions & 2 deletions docs/misc/lifecycle-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ With the migration to Livewire 3, there we are implementing several Lifecycle Ho
You may use these either in your Table Component, or in a trait

## configuring
This is called immediately prior to the configure() method being called
This is called immediately prior to the configure() method being called. This will be overridden by anything you define in configure()

## configured
This is called immediately after the configure() method is called
This is called immediately after the configure() method is called. This will override anything you define in configure()

## settingColumns
This is called prior to setting up the available Columns via the columns() method
Expand Down
2 changes: 1 addition & 1 deletion resources/laravel-livewire-tables.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import './css/laravel-livewire-tables.min.css'
import './js/laravel-livewire-tables.js'
import './js/laravel-livewire-tables.min.js'
2 changes: 1 addition & 1 deletion src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function mountComponentUtilities(): void
/**
* Runs configure() with Lifecycle Hooks on each Lifecycle
*/
public function bootComponentUtilities(): void
public function bootedComponentUtilities(): void
{
// Fire Lifecycle Hooks for configuring
$this->callHook('configuring');
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithColumnSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function queryStringWithColumnSelect(): array
return [];
}

public function bootWithColumnSelect(): void
public function bootedWithColumnSelect(): void
{
$this->setupColumnSelect();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait WithColumns
/**
* Sets up Columns
*/
public function bootWithColumns(): void
public function bootedWithColumns(): void
{
$this->columns = collect();

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait WithData
/**
* Sets up the Builder instance
*/
public function bootWithData(): void
public function bootedWithData(): void
{
//Sets up the Builder Instance
$this->setBuilder($this->builder());
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/WithSecondaryHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ trait WithSecondaryHeader

protected $secondaryHeaderTdAttributesCallback;

public function bootedWithSecondaryHeader(): void
{
$this->setupSecondaryHeader();
}

public function setupSecondaryHeader(): void
{
foreach ($this->getColumns() as $column) {
Expand All @@ -26,9 +31,4 @@ public function setupSecondaryHeader(): void
}
}
}

public function bootWithSecondaryHeader(): void
{
$this->setupSecondaryHeader();
}
}
19 changes: 19 additions & 0 deletions tests/DataTableComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rappasoft\LaravelLivewireTables\Tests;

use Livewire\Livewire;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\NoColumnsTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\NoPrimaryKeyTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;

Expand Down Expand Up @@ -79,4 +80,22 @@ public function default_fingerprint_will_be_url_friendy(): void
// control
$this->assertTrue(filter_var('http://[9/$].dev', FILTER_VALIDATE_URL) === false);
}

/** @test */
public function minimum_one_column_expected(): void
{
$this->expectException(\Rappasoft\LaravelLivewireTables\Exceptions\NoColumnsException::class);
$table = new NoColumnsTable();
$table->boot();
$table->bootedComponentUtilities();
$table->bootedWithData();
$table->bootedWithColumns();
$table->bootedWithColumnSelect();
$table->bootedWithSecondaryHeader();
$table->booted();
$table->renderingWithData($view, []);
$table->renderingWithPagination($view, []);
$table->render();

}
}
96 changes: 96 additions & 0 deletions tests/Http/Livewire/FailingTables/NoColumnsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables;

use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateTimeFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\NumberFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;

class NoColumnsTable extends DataTableComponent
{
public $model = Pet::class;

public function configure(): void
{
$this->setPrimaryKey('id');
}

public function columns(): array
{
return [
];
}

public function filters(): array
{
return [
MultiSelectFilter::make('Breed')
->options(
Breed::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($breed) => $breed->name)
->toArray()
)
->filter(function (Builder $builder, array $values) {
return $builder->whereIn('breed_id', $values);
}),
MultiSelectDropdownFilter::make('Species')
->options(
Species::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($species) => $species->name)
->toArray()
)
->filter(function (Builder $builder, array $values) {
return $builder->whereIn('species_id', $values);
}),
NumberFilter::make('Breed ID', 'breed_id_filter')
->filter(function (Builder $builder, string $value) {
return $builder->where('breed_id', '=', $value);
}),

TextFilter::make('Pet Name', 'pet_name_filter')
->filter(function (Builder $builder, string $value) {
return $builder->where('pets.name', '=', $value);
}),

DateFilter::make('Last Visit After Date', 'last_visit_date_filter')
->filter(function (Builder $builder, string $value) {
return $builder->whereDate('pets.last_visit', '=>', $value);
}),

DateTimeFilter::make('Last Visit Before DateTime', 'last_visit_datetime_filter')
->filter(function (Builder $builder, string $value) {
return $builder->whereDate('pets.last_visit', '<=', $value);
}),

SelectFilter::make('Breed SelectFilter', 'breed_select_filter')
->options(
Breed::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($breed) => $breed->name)
->toArray()
)
->filter(function (Builder $builder, string $value) {
return $builder->where('breed_id', $value);
})
->setCustomFilterLabel('livewire-tables::tests.testFilterLabel')
->setFilterPillBlade('livewire-tables::tests.testFilterPills'),
];
}
}
166 changes: 166 additions & 0 deletions tests/Http/Livewire/PetsTableMount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;

use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateTimeFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectDropdownFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\NumberFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;

class PetsTableMount extends DataTableComponent
{
public $model = Pet::class;

public ?int $mountBreed = null;

public function mount(?int $mountBreed = null)
{
if (isset($mountBreed)) {
$this->mountBreed = $mountBreed;
}
}

public function configure(): void
{
$this->setPrimaryKey('id');
}

public function columns(): array
{
return [
Column::make('ID', 'id')
->sortable()
->setSortingPillTitle('Key')
->setSortingPillDirections('0-9', '9-0'),
Column::make('Sort')
->sortable()
->excludeFromColumnSelect(),
Column::make('Name')
->sortable()
->secondaryHeader($this->getFilterByKey('pet_name_filter'))
->footerFilter('pet_name_filter')
->searchable(),

Column::make('Age'),

Column::make('Breed', 'breed.name')
->secondaryHeaderFilter('breed')
->footer($this->getFilterByKey('breed'))
->sortable(
fn (Builder $query, string $direction) => $query->orderBy('pets.id', $direction)
)
->searchable(
fn (Builder $query, $searchTerm) => $query->orWhere('breed.name', $searchTerm)
),

Column::make('Other')
->label(function ($row, Column $column) {
return 'Other';
})
->footer(function ($rows) {
return 'Count: '.$rows->count();
}),

LinkColumn::make('Link')
->title(fn ($row) => 'Edit')
->location(fn ($row) => 'http://www.google.com')
->attributes(fn ($row) => [
'class' => 'rounded-full',
'alt' => $row->name.' Avatar',
]),
ImageColumn::make('RowImg')
->location(fn ($row) => 'test'.$row->id)
->attributes(fn ($row) => [
'class' => 'rounded-full',
'alt' => $row->name.' Avatar',
]),
Column::make('Last Visit', 'last_visit')
->sortable()
->deselected(),
];
}

public function filters(): array
{
return [
MultiSelectFilter::make('Breed')
->options(
Breed::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($breed) => $breed->name)
->toArray()
)
->filter(function (Builder $builder, array $values) {
return $builder->whereIn('breed_id', $values);
}),
MultiSelectDropdownFilter::make('Species')
->options(
Species::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($species) => $species->name)
->toArray()
)
->filter(function (Builder $builder, array $values) {
return $builder->whereIn('species_id', $values);
}),
NumberFilter::make('Breed ID', 'breed_id_filter')
->filter(function (Builder $builder, string $value) {
return $builder->where('breed_id', '=', $value);
}),

TextFilter::make('Pet Name', 'pet_name_filter')
->filter(function (Builder $builder, string $value) {
return $builder->where('pets.name', '=', $value);
}),

DateFilter::make('Last Visit After Date', 'last_visit_date_filter')
->filter(function (Builder $builder, string $value) {
return $builder->whereDate('pets.last_visit', '=>', $value);
}),

DateTimeFilter::make('Last Visit Before DateTime', 'last_visit_datetime_filter')
->filter(function (Builder $builder, string $value) {
return $builder->whereDate('pets.last_visit', '<=', $value);
}),

SelectFilter::make('Breed SelectFilter', 'breed_select_filter')
->options(
Breed::query()
->orderBy('name')
->get()
->keyBy('id')
->map(fn ($breed) => $breed->name)
->toArray()
)
->filter(function (Builder $builder, string $value) {
return $builder->where('breed_id', $value);
})
->setCustomFilterLabel('livewire-tables::tests.testFilterLabel')
->setFilterPillBlade('livewire-tables::tests.testFilterPills'),
];
}

public function builder(): Builder
{
if (isset($this->mountBreed)) {
return Pet::where('breed_id', $this->mountBreed);
}

return Pet::query();
}
}
Loading

0 comments on commit 4e75b3d

Please sign in to comment.