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

Implemented ability to hide toolbar and entire data table when empty #1060

Closed
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
4 changes: 2 additions & 2 deletions resources/views/components/tools.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
@endphp

@if ($theme === 'tailwind')
<div class="flex-col">
<div class="flex-col tools">
{{ $slot }}
</div>
@elseif ($theme === 'bootstrap-4' || $theme === 'bootstrap-5')
<div class="d-flex flex-column">
<div class="d-flex flex-column tools">
{{ $slot }}
</div>
@endif
7 changes: 7 additions & 0 deletions resources/views/datatable.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
@if (!$this->getHideOnEmptyStatus() || ($this->getHideOnEmptyStatus() && !empty($rows)))
@isset($beforeComponentView)
@include($beforeComponentView)
@endisset
<x-livewire-tables::wrapper :component="$this">
@if ($this->hasConfigurableAreaFor('before-tools'))
@include($this->getConfigurableAreaFor('before-tools'), $this->getParametersForConfigurableArea('before-tools'))
@endif

@if ($this->getToolbarStatusIsEnabled())
<x-livewire-tables::tools>
<x-livewire-tables::tools.sorting-pills />
<x-livewire-tables::tools.filter-pills />
<x-livewire-tables::tools.toolbar />
</x-livewire-tables::tools>
@endif

<x-livewire-tables::table>
<x-slot name="thead">
Expand Down Expand Up @@ -69,3 +75,4 @@
@include($customView)
@endisset
</x-livewire-tables::wrapper>
@endif
Empty file.
11 changes: 11 additions & 0 deletions src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public function customView(): string
{
return 'livewire-tables::stubs.custom';
}

/**
* The view to add any modals for the table, could also be used for any non-visible html
*
* @return string
*/
public function beforeComponentView(): string
{
return 'livewire-tables::stubs.before';
}

/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
Expand All @@ -139,6 +149,7 @@ public function render()
'columns' => $this->getColumns(),
'rows' => $this->getRows(),
'customView' => $this->customView(),
'before' => $this->beforeComponentView(),
]);
}
}
2 changes: 2 additions & 0 deletions src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ trait ComponentUtilities
protected string $emptyMessage = 'No items found. Try to broaden your search.';
protected array $additionalSelects = [];
protected bool $hideConfigurableAreasWhenReorderingStatus = true;
protected bool $toolbarStatus = true;
protected bool $hideOnEmptyStatus = false;
protected array $configurableAreas = [
'before-tools' => null,
'toolbar-left-start' => null,
Expand Down
56 changes: 56 additions & 0 deletions src/Traits/Configuration/ComponentConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,60 @@ public function setQueryStringAlias(string $queryStringAlias): self

return $this;
}

/**
* @param $status
*
* @return $this
*/
public function setToolbarStatus(bool $status): self
{
$this->toolbarStatus = $status;

return $this;
}

/**
* @return $this
*/
public function setToolbarDisabled(): self
{
return $this->setToolbarStatus(false);
}

/**
* @return $this
*/
public function setToolbarEnabled(): self
{
return $this->setToolbarStatus(true);
}

/**
* @param $status
*
* @return $this
*/
public function setHideOnEmptyStatus(bool $status): self
{
$this->hideOnEmptyStatus = $status;

return $this;
}

/**
* @return $this
*/
public function setHideOnEmptyDisabled(): self
{
return $this->setHideOnEmptyStatus(false);
}

/**
* @return $this
*/
public function setHideOnEmptyEnabled(): self
{
return $this->setHideOnEmptyStatus(true);
}
}
48 changes: 48 additions & 0 deletions src/Traits/Helpers/ComponentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,52 @@ public function hideConfigurableAreasWhenReorderingIsDisabled(): bool
{
return $this->getHideConfigurableAreasWhenReorderingStatus() === false;
}

/**
* @return bool
*/
public function getToolbarStatus(): bool
{
return $this->toolbarStatus;
}

/**
* @return bool
*/
public function getToolbarStatusIsEnabled(): bool
{
return $this->getToolbarStatus() === true;
}

/**
* @return bool
*/
public function getToolbarStatusIsDisabled(): bool
{
return $this->getToolbarStatus() === false;
}

/**
* @return bool
*/
public function getHideOnEmptyStatus(): bool
{
return $this->hideOnEmptyStatus;
}

/**
* @return bool
*/
public function getHideOnEmptyStatusIsEnabled(): bool
{
return $this->getHideOnEmptyStatus() === true;
}

/**
* @return bool
*/
public function getHideOnEmptyStatusIsDisabled(): bool
{
return $this->getHideOnEmptyStatus() === false;
}
}
36 changes: 36 additions & 0 deletions tests/Traits/Helpers/ComponentHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,40 @@ public function can_get_query_string_alias_and_it_will_be_the_same_as_table_name
{
$this->assertSame($this->basicTable->getTableName(), $this->basicTable->getQueryStringAlias());
}

/** @test */
public function can_get_toolbar_status(): void
{
$this->assertTrue($this->basicTable->getToolbarStatus());

$this->basicTable->setToolbarDisabled();

$this->assertSame(false, $this->basicTable->getToolbarStatus());

$this->basicTable->setToolbarEnabled();

$this->assertTrue($this->basicTable->getToolbarStatus());

$bool = boolval(mt_rand(0,1));

$this->basicTable->setToolbarStatus($bool);

$this->assertSame($bool, $this->basicTable->getToolbarStatus());
}

/** @test */
public function can_get_hide_on_empty_status(): void
{
$this->assertFalse($this->basicTable->getHideOnEmptyStatus());

$this->basicTable->setHideOnEmptyEnabled();

$this->assertSame(true, $this->basicTable->getHideOnEmptyStatus());

$bool = boolval(mt_rand(0,1));

$this->basicTable->setHideOnEmptyStatus($bool);

$this->assertSame($bool, $this->basicTable->getHideOnEmptyStatus());
}
}
17 changes: 17 additions & 0 deletions tests/Traits/Visuals/ComponentVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public function empty_message_shows_with_no_results(): void
->assertSee('No items found. Try to broaden your search.');
}

/** @test */
public function disable_when_hide_on_empty_enabled_with_no_results(): void
{
Livewire::test(PetsTable::class)
->call('setHideOnEmptyEnabled')
->set('table.search', 'sdfsdfsdf')
->assertDontSeeText('datatable');
}

/** @test */
public function visible_when_hide_on_empty_enabled_with_results(): void
{
Livewire::test(PetsTable::class)
->call('setHideOnEmptyEnabled')
->assertSee('Cartman');
}

/** @test */
public function debugging_shows_when_enabled(): void
{
Expand Down
26 changes: 26 additions & 0 deletions tests/Traits/Visuals/ToolbarVisualsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Visuals;

use Livewire\Livewire;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTableNoFilters;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

class ToolbarVisualsTest extends TestCase
{
/** @test */
public function toolbar_shows_when_enabled(): void
{
Livewire::test(PetsTable::class)
->assertSee('flex-col tools');
}

/** @test */
public function toolbar_doesnt_show_when_disabled(): void
{
Livewire::test(PetsTable::class)
->call('setToolbarDisabled')
->assertDontSee('flex-col tools');
}
}