Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 Restore setTrAttributes #1378

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## [Unreleased] - 3.x setTrAttributes
- Adding capabilities & tests for setTrAttributes

## [Unreleased] - 3.x Fixes for reordering striping
- Force calculation of even/odd only once in reorder mode
- Call internal method for reordering, and pass to configured method to process
Expand Down
16 changes: 9 additions & 7 deletions resources/views/components/table/tr.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
:draggable="currentlyReorderingStatus"
wire:key="{{ $tableName }}-tablerow-tr-{{ $row->{$this->getPrimaryKey()} }}"
loopType="{{ ($rowIndex % 2 === 0) ? 'even' : 'odd' }}"
{{
$attributes->merge($customAttributes)
->class(['bg-white dark:bg-gray-700 dark:text-white rappasoft-striped-row' => ($component->isTailwind() && ($customAttributes['default'] ?? true) && $rowIndex % 2 === 0)])
->class(['bg-gray-50 dark:bg-gray-800 dark:text-white rappasoft-striped-row' => ($component->isTailwind() && ($customAttributes['default'] ?? true) && $rowIndex % 2 !== 0)])
->class(['cursor-pointer' => ($component->isTailwind() && $component->hasTableRowUrl() && ($customAttributes['default'] ?? true))])
->class(['bg-light rappasoft-striped-row' => ($component->isBootstrap() && $rowIndex % 2 === 0 && ($customAttributes['default'] ?? true))])
->class(['bg-white rappasoft-striped-row' => ($component->isBootstrap() && $rowIndex % 2 !== 0 && ($customAttributes['default'] ?? true))])
->except(['default'])
}}

@class([
'bg-white dark:bg-gray-700 dark:text-white rappasoft-striped-row' => ($component->isTailwind() && ($customAttributes['default'] ?? true) && $rowIndex % 2 === 0),
'bg-gray-50 dark:bg-gray-800 dark:text-white rappasoft-striped-row' => ($component->isTailwind() && ($customAttributes['default'] ?? true) && $rowIndex % 2 !== 0),
'cursor-pointer' => ($component->isTailwind() && $component->hasTableRowUrl()),
'bg-light rappasoft-striped-row' => ($component->isBootstrap() && $rowIndex % 2 === 0),
'bg-white rappasoft-striped-row' => ($component->isBootstrap() && $rowIndex % 2 !== 0),
])
>
{{ $slot }}
</tr>
171 changes: 171 additions & 0 deletions tests/Http/Livewire/PetsTableAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?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 PetsTableAttributes extends DataTableComponent
{
public $model = Pet::class;

public function configure(): void
{
$this->setPrimaryKey('id')
->setTrAttributes(function ($row, $index) {
if ($index === 0) {
return [
'testTrAttribute' => 'testTrAttributeValueForTestSuiteIndex0',
'default' => false,
];
}
if ($index === 1) {
return [
'testTrAttribute' => 'testTrAttributeValueForTestSuiteIndex1',
'default' => false,
];
}
if ($index === 500) {
return [
'testTrAttribute' => 'testTrAttributeValueForTestSuiteNotSeen',
'default' => false,
];
}

return [];
});

}

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'),
];
}
}
17 changes: 17 additions & 0 deletions tests/Traits/Visuals/ComponentVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\NoBuildMethodTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\NoPrimaryKeyTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTableAttributes;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

class ComponentVisualsTest extends TestCase
Expand Down Expand Up @@ -94,4 +95,20 @@ public function fails_when_table_has_no_model_or_builder(): void
$this->fail('Did Not Throw Error - Missing Model/Builder');
}
}

/** @test */
public function can_see_valid_tr_attributes_html(): void
{
Livewire::test(PetsTableAttributes::class)
->assertSeeHtml('testTrAttribute="testTrAttributeValueForTestSuiteIndex0"')
->assertSeeHtml('testTrAttribute="testTrAttributeValueForTestSuiteIndex1"');
}

/** @test */
public function cannot_see_invalid_tr_attributes_html(): void
{
Livewire::test(PetsTableAttributes::class)
->assertSeeHtml('testTrAttribute="testTrAttributeValueForTestSuiteIndex0"')
->assertDontSeeHtml('testTrAttribute="testTrAttributeValueForTestSuiteNotSeen"');
}
}