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

[FEATURE] Add noDataLabel to customize "no records found" message #1458

Merged
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
1 change: 1 addition & 0 deletions resources/views/components/table/no-data-label.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>{!! $noDataLabel !!}</span>
2 changes: 1 addition & 1 deletion resources/views/components/table/th-empty.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class="{{ data_get($theme, 'table.tdBodyEmptyClass') }}"
style="{{ data_get($theme, 'table.tdBodyEmptyStyle') }}"
colspan="{{ ($checkbox ? 1 : 0) + count($columns) + (data_get($setUp, 'detail.showCollapseIcon') ? 1 : 0) }}"
>
<span>{{ trans('livewire-powergrid::datatable.labels.no_data') }}</span>
{!! $this->processNoDataLabel() !!}
</th>
</tr>
23 changes: 23 additions & 0 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,29 @@ public function fillData(): BaseCollection|LengthAwarePaginator|\Illuminate\Cont
return $this->processDataSourceInstance->get();
}

public function processNoDataLabel(): string
{
$noDataLabel = $this->noDataLabel();

if ($noDataLabel instanceof View) {
return $noDataLabel->with(
[
'noDataLabel' => trans('livewire-powergrid::datatable.labels.no_data'),
'theme' => $this->getTheme(),
'table' => 'livewire-powergrid::components.table',
'data' => [],
]
)->render();
}

return "<span>{$noDataLabel}</span>";
}

public function noDataLabel(): string|View
{
return view('livewire-powergrid::components.table.no-data-label');
}

private function renderView(mixed $data): Application|Factory|View
{
$theme = $this->getTheme();
Expand Down
65 changes: 65 additions & 0 deletions tests/Concerns/Components/NoDataCollectionTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Components;

use Illuminate\Support\Collection;
use PowerComponents\LivewirePowerGrid\{
Column,
PowerGrid,
PowerGridComponent,
PowerGridFields
};

class NoDataCollectionTable extends PowerGridComponent
{
public function datasource(): Collection
{
return collect([]);
}

public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name');
}

public function columns(): array
{
return [
Column::add()
->title(__('ID'))
->field('id')
->searchable()
->sortable(),

Column::add()
->title(__('Name'))
->field('name')
->searchable()
->sortable(),

Column::action('Action'),
];
}

public function actions($row): array
{
return [];
}

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

public function bootstrap()
{
config(['livewire-powergrid.theme' => 'bootstrap']);
}

public function tailwind()
{
config(['livewire-powergrid.theme' => 'tailwind']);
}
}
1 change: 1 addition & 0 deletions tests/Concerns/Fixtures/views/no-data.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><span class="custom">No Data Here!!!</span></div>
42 changes: 42 additions & 0 deletions tests/Feature/NoDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\View\View;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\NoDataCollectionTable;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

$componentCustomMessage = new class () extends NoDataCollectionTable {
public function noDataLabel(): string|View
{
return 'foo bar 1234';
}
};

$componentCustomView = new class () extends NoDataCollectionTable {
public function noDataLabel(): string|View
{
return view('no-data');
}
};

it('shows the Powergrid default "no data" message', function (string $theme) {
livewire(NoDataCollectionTable::class)
->call($theme)
->assertSeeHtml('<span>No records found</span>');
})->with(['bootstrap', 'tailwind']);

it('shows a custom string message', function ($component, $theme) {
livewire($component)
->call($theme)
->assertSeeHtml('<span>foo bar 1234</span>');
})->with(['string' => [$componentCustomMessage::class]])
->with(['bootstrap', 'tailwind']);

it('show a view', function ($component, $theme) {
$this->app['view']->addLocation(fixturePath('views'));

livewire($component)
->call($theme)
->assertSeeHtml('<div><span class="custom">No Data Here!!!</span></div>');
})->with(['view' => [$componentCustomView::class]])
->with(['bootstrap', 'tailwind']);
9 changes: 2 additions & 7 deletions tests/Feature/Support/PowerGridStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
});

it('can list all variables in stub file', function () {
expect(PowerGridStub::make(_fixtureStubPath('demo.stub')))
expect(PowerGridStub::make(fixturePath('Stubs/demo.stub')))
->listStubVars()
->toArray()
->toBe([
Expand All @@ -37,7 +37,7 @@
});

it('properly replaces legacy variables', function () {
$stub = PowerGridStub::make(_fixtureStubPath('legacy.stub'))
$stub = PowerGridStub::make(fixturePath('Stubs/legacy.stub'))
->setVar('namespace', 'App\Livewire')
->setVar('model', 'User')
->setVar('modelFqn', 'App\Models\User')
Expand All @@ -53,11 +53,6 @@
->toContain("return DB::table('users');");
});

function _fixtureStubPath(string $filename): string
{
return str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/../../Concerns/Fixtures/Stubs/' . $filename);
}

it('throws FileNotFoundException if template does not exist', function (string $invalidTemplate) {
PowerGridStub::make($invalidTemplate);
})
Expand Down
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ function requiresOpenSpout()

return test();
}

function fixturePath(string $filepath): string
{
return str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/Concerns/Fixtures/' . ltrim($filepath, '/'));
}
Loading